简体   繁体   English

如何在完成AJAX帖子时更改URL,而无需重新加载页面

[英]How to change the url when an AJAX post is done, without page reload

I am building a blog application in Django and in that any user registered on my site can write a blog. 我正在Django中构建一个博客应用程序,因为在我的网站上注册的任何用户都可以写博客。 When I was building the model, I choose to represent each blog by the combination of user.id and title as slug field. 当我构建模型时,我选择通过user.id和title作为slug字段的组合来表示每个博客。 So a blog titled My Blog of a user with id 8909 would be as 因此,一个名为My Blog的用户ID为8909的博客就是如此

/blogs/8909/My-Blog/

A problem comes in when I try to provide a preview button to the user. 当我尝试向用户提供preview按钮时出现问题。 The function of preview is to update the blog in the database and open the blog in a new tab. 预览功能是更新数据库中的博客并在新选项卡中打开博客。 However since I represent a blog by it's title, if the user alters the title, then I need to alter the current url as well, otherwise the url becomes non-existent at the back-end. 但是,由于我用它的标题代表博客,如果用户改变标题,那么我也需要改变当前的URL,否则url在后端变得不存在。

I have not come across a way I can edit the url, I have only come across articles that tell I can edit the hash field, but nothing more than that. 我没有遇到过我可以编辑网址的方法,我只看到了可以编辑哈希字段的文章,但仅此而已。

What can be the solution to my problem. 什么可以解决我的问题。

Give each blog an id that is invariant and use this in your routing eg: 为每个博客提供一个不变的ID,并在您的路由中使用它,例如:

/blogs/{userId}/{blogId}

This way your resource identifiers are not tied to anything that a user can change. 这样,您的资源标识符就不会绑定到用户可以更改的任何内容。

If however, there is no way to do the above, you may consider storing a list of recently changed titles and issuing a redirect response if any 'out of date' resources are requested. 但是,如果无法执行上述操作,您可以考虑存储最近更改的标题列表,并在请求任何“过期”资源时发出重定向响应。

If you are doing it asynchronously and you dont want to postback the page you can try window.history.pushState, it can change the URL without post back, but remember at the backend you also have to change the database where you are maintaining actual URL. 如果您是异步执行并且不想回发页面,可以尝试使用window.history.pushState,它可以在不回发的情况下更改URL,但请记住在后端还需要更改数据库,以便维护实际的URL 。 You can find "How to change URL withought page postback : " here some details, hope it works for you 你可以在这里找到“如何更改URL withought page backback:”这里的一些细节,希望它对你有用

http://arbtech.co.in/search_blog.aspx?q=How-to-change-URL-withought-page-postback- : http://arbtech.co.in/search_blog.aspx?q=How-to-change-URL-withought-page-postback-

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM