Removing # from url of angularJS for SEO with backend as Django

妖精的绣舞 提交于 2019-12-17 20:38:35

问题


I know removing hash from AngularJS is pretty straightforward but the problem is that the backend is in Django.

So, the app works unless the page is refreshed using "F5".

so, http://127.0.0.1:8000/account works if the button is clicked but refreshing the page gives me Page not found as the server searches for it in urls.py file

Can someone please suggest me any fix for this ?


回答1:


Everything is right. When you refresh the page, firstly request gets processed on the server (and goes to django router). So server should know that it should return your angular page for this URL.

Let's assume that your page that contains Angular application lives on view called index. Then just point this url to it:

urlpatterns = [
    url(r'^account/$', index), 
]

or point all urls to your view (in case you don't need any other url's to be processed without angular):

//something like this, not really sure about the regex
urlpatterns = [
    url(r'^.*$', index), 
]

or something like

urlpatterns = [
    url(r'^/account/.*$', index), 
]

You're not alone with this issue: see this and this. So as you can see, it's not Django-specific trouble, but some general client-server workflow.




回答2:


Use Locationprovider instead of routeProvider and enable html5 to true. https://docs.angularjs.org/api/ng/provider/$locationProvider



来源:https://stackoverflow.com/questions/47373927/removing-from-url-of-angularjs-for-seo-with-backend-as-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!