I have a Django backend, VueJS frontend combination, where I serve a REST API via Django and a single page application with VueJS and vue-router.
From this question I got the tip to use the following urls in my main urls.py:
urlpatterns = [
re_path(r'^(?P<filename>(robots.txt)|(humans.txt))$', views.home_files, name='home-files'),
path('api/', include('backend.urls', namespace='api')),
path('auth/', include('auth.urls')),
path('admin/', admin.site.urls),
re_path(r'^.*$', views.vue), # VueJS frontend
]
So I want URLs to behave like this:
{baseDomain}/api/users/1/ -> go to backend.urls
{baseDomain}/auth/login/ -> go to auth.urls
{baseDomain}/admin/ -> go to admin page
{baseDomain}/de/home -> vue-router takes over
Now these URLs work perfectly fine, however I would expect that {baseDomain}/api/users/1 (without trailing slash) would still go to backend.urls, however what happens is that I land on the Vue page.
Adding APPEND_SLASH = True in settings.py does not help either, since it only appends a slash if it didn't find a page to load. But since the regex for my frontend matches anything it always redirects to Vue.
My attempt was to fix it by adding:
re_path(r'.*(?<!/)$', views.redirect_with_slash)
with the following code:
def redirect_with_slash(request):
'''Redirects a requested url with a slash at the end'''
if request.path == '/':
return render(request, 'frontend/index.html')
return redirect(request.path + '/')
But it isn't a very elegant one. Also mind the if request.path == '/':
. Weirdly enough, Django would match '/' with the regex r'.*(?<!/)$'
and then redirect to '//', which is an invalid URL and show an error page, so I had to include this if-statement.
Does anyone have a solution for this? In the referenced question this did not seem to be an issue, so I wonder why it is in my project.
EDIT: backend urls.py
"""
backend urls.py
"""
from django.urls import include, path
from rest_framework_nested import routers
from auth.views import UserViewSet, GroupViewSet, ProjectViewSet
from .views import IfcViewSet, IfcFileViewSet
app_name = 'api'
router = routers.DefaultRouter() #pylint: disable=C0103
router.register(r'users', UserViewSet)
router.register(r'groups', GroupViewSet)
router.register(r'projects', ProjectViewSet)
projects_router = routers.NestedSimpleRouter(router, r'projects', lookup='project')
projects_router.register(r'models', IfcFileViewSet, base_name='projects-models')
urlpatterns = [
path('', include(router.urls)),
path('', include(projects_router.urls))
]
"""
auth urls.py
"""
from django.urls import path, include
from rest_framework import routers
from rest_framework_jwt.views import obtain_jwt_token, refresh_jwt_token
from .views import RegistrationViewSet
app_name = 'authentication'
router = routers.DefaultRouter()
router.register('register', RegistrationViewSet)
urlpatterns = [
path('', include(router.urls)),
path('', include('rest_auth.urls')),
path('refresh_token/', refresh_jwt_token),
]