处理单页应用程序URL和Django URL

django Django

Itachi小宇宙

2020-03-13

我在Vue.js中创建了一个单页应用程序,该应用程序使用HTML5历史记录模式进行路由,并且html文件与Django一起提供。

django urls.py就像这样:

urlpatterns = [
    url(r'^$', views.home),
    url(r'^admin/', admin.site.urls),
    url(r'^api-token-auth/', obtain_jwt_token),
]

views.home

def home(request):
    return render(request, 'index.html')

请考虑以下情形:

  1. 用户访问主页(即/

由于首页响应单页Vuejs应用程序所需的index.html,因此其工作原理与预期的一样。

  1. 用户从那里导航到“关于”页面(即/username/12)。

在使用Vue路由器导航时,它仍然可以正常工作。

  1. 现在,用户刷新页面。

由于没有/username/12urls.py模式,它会显示找不到网页(404)

Now, I could provide another pattern in urls.py to catch all pattern in the last order as this:

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^api-token-auth/', obtain_jwt_token),
    url(r'^.*$', views.home),
]

But other urls like the media or static urls will also point to the same catch all pattern regex. How can I solve this problem?

第1467篇《处理单页应用程序URL和Django URL》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

0个回答

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android