Romeo
Romeo

Reputation: 788

How do I create multiple urls.py in django app?

I want to split the django app urls into two urls with different names, urls.py and reset_urls.py. First one, urls.py works as expected, but the second does not. I tried the obvious approach but it seems not to be working. I want the reset_urls.py to be an endpoint for a password reset, but after creating, it seems not to be working. I know django allows for renaming and having multiple urls.py in single app, but I'm not sure exactly how it should be done, even after checking the docs(not sure I checked the right one)

Here the source code for the urls:

URLconf:

from django.contrib import admin
from django.urls import path, include

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path('', include('blog.urls', namespace='blog')),
    path('accounts/', include('users.urls', namespace='account')),
    path('accounts/reset', include('users.reset_urls')),
    path('admin/', admin.site.urls),
]

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

urls.py

Works as expected. no issue here.

from django.urls import path

from users import views
from django.contrib.auth import views as auth_views

app_name = 'account'
urlpatterns = [
    path('profile', views.profile, name='profile'),
    path('register', views.register, name='register'),
    path('login', auth_views.LoginView.as_view(template_name='users/login.html'), name='login'),
    path('logout', auth_views.LogoutView.as_view(template_name='users/logout.html'), name='logout'),
]

reset_urls.py

Does not work as expected

from django.urls import path
from django.contrib.auth import views as auth_views


urlpatterns = [
    path('password_reset/', auth_views.PasswordResetView.as_view(template_name='users/account/password_reset.html'), 
        name='password_reset'),
    path('password_reset/done/', auth_views.PasswordResetDoneView.as_view(template_name='users/account/password_reset_done.html'), 
        name='password_reset_done'),
    path('password_reset_confirm/<uidb64>/<token>/', auth_views.PasswordResetView.as_view(template_name='users/account/password_reset.html'), 
        name='password_reset_confirm'),
    path('password_reset_complete/', auth_views.PasswordResetView.as_view(template_name='users/account/password_reset.html'), 
        name='password_reset_complete'),
]

Upvotes: 2

Views: 2363

Answers (1)

Create an urls folder with __init__.py in it, in app directory. Then seperate your urls as you like into several files in the urls folder.

In __init__.py import your urls like:

from .reset_urls import urlpatterns_reset
from .normal_urls import urlpatterns_normal

urlpatterns = urlpatterns_normal + urlpatterns_reset

Upvotes: 3

Related Questions