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'),
]
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
Related
I can't get my urls to work for django no matter what I try
from django.contrib import admin
from django.urls import include, re_path
from django.conf.urls import url
from catalog import views
urlpatterns = [
re_path(r'^admin/', admin.site.urls),
re_path(r'^fruit/?$', views.fruit_catalogue, name='fruit_catalogue'),
re_path(r'^fruit/?$', views.fruit_details),
re_path(r'^fruit/(?P<fruit_id>[1-7]{1})/?$', views.fruit_details),
]
There are 2 url.py files that you should check for the correct functioning of the URLs.
First the one, that is in your project, that should look something like this:
from django.contrib import admin
from django.urls import include, path
from django.conf import settings
urlpatterns = [
path('admin/', admin.site.urls),
path("", include("NAMEOFAPP.urls")),
]
and the other is inside your app. and a simple version of it should look something like this:
from django.urls import path
from . import views
urlpatterns = [
path("home", views.index, name="index"),
path("login", views.login_view, name="login"),
path("register", views.register, name="register")
]
Try to use this same setup and you should be perfectly fine.
I'm very new to working with Django and I've been relying on some tutorials to link this one to React, but the problem is that initially (when I open 127.0.0.1:8000) React loads the routes perfectly, then when I reload the page Django tries to interpret the path from urls.py and obviously can't find it.
The error is:
Page not found (404) Using the URLconf defined in memberstack.urls, Django tried these URL patterns, in this order:
admin/
api/token-auth/
core/
I hope you can help me, thanks in advance
my_project/urls.py
from django.contrib import admin
from django.urls import path, include
from rest_framework_jwt.views import obtain_jwt_token
from frontend.views import index
urlpatterns = [
path('', include('frontend.urls')),
path('admin/', admin.site.urls),
path('api/token-auth/', obtain_jwt_token),
path('core/', include('core.urls')),
]
frontend/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index),
]
frontend/views.py
from django.shortcuts import render
def index(request):
return render(request, 'frontend/index.html')
For this, you'll have to use a catch-all in order for React to handle all the routing instead of django there.
from django.urls import path, re_path
from . import views
urlpatterns = [
path('', include('frontend.urls')),
path('admin/', admin.site.urls),
path('api/token-auth/', obtain_jwt_token),
path('core/', include('core.urls')),
re_path(r'^(?:.*)/?$', include('frontend.urls')),
]
Or
urlpatterns = [
path('', views.index),
re_path(r'^(?:.*)/?$', views.index)
]
I think the better practice would be to implement Django-Rest-Framework and build them separately.
Everything works fine on development server but in production server I can only access views.home at https://www.host.com/ and everything else such as
https://www.host.com/b/ or https://www.host.com/tab or https://www.host.com/ext returns status_code 500.
I'm using django 2.1 in python 3.7. Cheers!
Here's the project level urls.py
from django.contrib import admin
from django.urls import path, include
from django.contrib.auth import views as auth_views
from ext.forms import CustomAuthForm
from mysite import settings
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = [
path('admin/', admin.site.urls),
path('login/', auth_views.LoginView.as_view(authentication_form = CustomAuthForm), name='login'),
path('logout/', auth_views.LogoutView.as_view(next_page= settings.LOGOUT_REDIRECT_URL), name='logout'),
path('', include('ext.urls')),
]
urlpatterns += staticfiles_urlpatterns()
And app level urls.py looks like this
from django.urls import path, re_path
from django.conf.urls import url
from ext import views
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
app_name = 'ext'
urlpatterns = [
path('', views.home, name='home'),
path('int/', views.get_int_status, name='home-load-int'),
path('ext/', views.get_ext_status, name='home-load-ext'),
path('tab/', views.get_tab_status, name='home-load-tab'),
path('b/', views.uzair, name='home-uzair'),
]
I was missing RewriteEngine On in my .httaccess file, what a shame xD
I'm trying to reach the Django admin page but when I type in http://localhost:8000/admin/ it pops up with my web application instead.
For more context I have two url.py's in separate folders.
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('', views.project_list, name='list'),
path('<slug:project_slug>/', views.project_detail, name='detail')
]
and...
from django.urls import path, include
urlpatterns = [
path('', include('budget.urls')),
path('admin/', admin.site.urls),
I'm not sure why http://localhost:8000/admin/ doesn't go to Django's admin page but if anyone has any suggestions that would be very much appreciated.
In your project level urls.py place admin url in top.
Like this
urlpatterns = [
path('admin/',admin.site.urls),
path('',include('budget.urls'))
]
Change urlpatterns order here:
from django.urls import path, include
urlpatterns = [
path('', include('budget.urls')),
path('admin/', admin.site.urls),
like this:
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('budget.urls')),
In your code pattern path('<slug:project_slug>/', views.project_detail, name='detail') resolve before path('admin',admin.site.urls),.
See Django documentation
I followed a tutorial to allow users to register an account but the url path does not seem to be found. It allows me to access 127.0.0.1:8000/accounts/signup but not 127.0.0.1:8000/signup when I have set the name for it.
I tried changing the urlpatterns from a path to a url method but that didn't solve the problem.
my file structure is as follows:
App:
accounts:
urls.py
views.py
...
Server:
settings.py
urls.py
...
templates:
registration:
login.html
base.html
home.html
signup.html
manage.py
In my Server\urls.py I have
from django.contrib import admin
from django.urls import path, include
from django.views.generic.base import TemplateView
urlpatterns = [
path('admin/', admin.site.urls),
path('accounts/', include('accounts.urls')),
path('accounts/', include('django.contrib.auth.urls')),
path('', TemplateView.as_view(template_name='home.html'), name='home'),
]
In my accounts\urls.py I have
from django.urls import path
from . import views
urlpatterns = [
path('signup/', views.SignUp.as_view(), name='signup'),
]
The error I get is
Using the URLconf defined in Server.urls, Django tried these URL patterns, in this order:
admin/
accounts/
accounts/
[name='home']
The current path, signup, didn't match any of these.
I'm not too sure what is going on since I'm still sort of new to Django and busy learning as I go along.
path('accounts/', include('accounts.urls')),
means that all urls from accounts.urls are supposed to be prefixed with accounts/.
If that's not what you want - edit this to
path('/', include('accounts.urls')),
and accounts.urls will be treated as top-level urls.