Django Url Pattern Not Being Found - python

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.

Related

Routing with Django and React

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.

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'),
]
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

Django 'Page not found' error when setting up URLs

I'm following this tutorial: https://www.youtube.com/watch?v=Z4D3M-NSN58
This is the point where I get the 404 error: https://www.youtube.com/watch?v=Z4D3M-NSN58&t=1044s
What I did so far:
Set up a new project,
Created a virtualenv,
Set up views.py,
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(response):
return HttpResponse("<h3>MySite</h3>")
Created urls.py inside my main folder,
from django.urls import path
from . import views
urlpatterns = [
path("", views.index, name="index"),
]
Modified the urls.py inside project folder
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('home/', include("main.urls")),
]
Upon connecting to http://127.0.0.1:8000/ I receive the 404 error.
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/
Using the URLconf defined in project.urls, Django tried these URL patterns, in this order:
admin/
home/
The empty path didn't match any of these.
I am unable to proceed.
Solution:
urls.py inside the project folder has two paths, one for admin/ and one for home/.
urlpatterns = [
path('admin/', admin.site.urls),
path('home/', include("main.urls")),
]
Trying to connect to http://127.0.0.1:8000/home or http://127.0.0.1:8000/admin works, but leaving an empty path does not.
This is because if you type /home/ it goes inside the main/urls.py and looks for a /home/ path. The path is there and it displays what's inside the views.py. In order to display the desired output without entering a path at the end of the URL I had to change the /home/ path to an 'empty' path inside of the project/urls.py
urlpatterns = [
path("admin/", admin.site.urls),
path("", include("main.urls")),
]
Now if no path is entered: http://127.0.0.1:8000/ It will go and look inside the main/urls.py for an empty path. It will find that an empty path leads to project/views.index and that is what it displays.
Add in your urls.py
urlpatterns = [
path("", views.index, name="index"),
]
and write a view to display message.
as you have put /home in your main urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('home/', include("main.urls")),
]
so you will get a 404 error if you go to localhost:8000/
you have to go to localhost:8000/home to get the index page

Django 2.0: Url path to homepage/index

I'm trying to set my django homepage, linked to the root of my website: i.e. http://127.0.0.1:8000/index/ but instead, I keep getting errors where Django is searching for myapp as the homepage: http://127.0.0.1:8000/myapp/. I would just like to land on a homepage with "index" in the url instead of "myapp/"
The error is as follows:
Using the URLconf defined in Main.urls, Django tried these URL patterns, in this order:
admin/
[name='index']
[name='myapp_index']
publications/ [name='publications']
^static\/(?P<path>.*)$
The current path, myapp/, didn't match any of these.
Views.py
def index(request):
return render(request, 'index.html')
Main/urls
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('admin/', admin.site.urls),
path('', include('myapp.urls')),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
myapp/urls
from django.urls import path
from logbook import views
urlpatterns = [
path('', views.index, name='index'),
path('', views.myapp_index, name='myapp_index'),
path('publications/', views.publications, name='publications'),
]
Now, if I change the path in Main/urls.py to path('myapp/', include('myapp.urls')), I land on the appropriate homepage, except, I would prefer that there is "index/" listed in the URL instead of "myapp/".
I may be missing something very simple here. This is what I think is my MWE, happy to add more.
I was having the same problem and I was cracking my head finding a solution.
The solution was to erase browser's cache and history
I realized that because when I tried with incognito mode worked perfectly!
This should be in the main urls.py
path('', views.index, name='index')
instead of my_app/urls.py
path('myapp/', include('myapp.urls'))
path('index/', include('myapp.urls'))
That might get the desired result.
Did you try following:
Main/urls
...
path(r'^', include('myapp.urls')),
...
myapp/urls
...
path(r'^$', views.index, name='index'),
# path('', views.myapp_index, name='myapp_index'), ## REMOVE THIS ROW ##
...
first you must choose one of below lines in myapp.urls and delete other
you can not set two views for one url patterns
path('', views.index, name='index'),
path('', views.myapp_index, name='myapp_index'),
you have two solution for get index/ as main url
1.change Main.urls like below
path('', include('myapp.urls')),
to
path('index/', include('myapp.urls')),
2.change myapp.urls like below
path('', views.index, name='index'),
to
path('index/', views.index, name='index'),
you actually changed name of urls that using in python codes not path of urls
edit
Do you mean index/ like the following?
www.example.com/
so u must do that as above but delete "index/"
so u cant do it as below :
change Main.urls like below
path('', include('myapp.urls')),
to
path('^$', include('myapp.urls')),
or
change myapp.urls like below
path('', views.index, name='index'),
to
path('^$', views.index, name='index'),
This is what worked for me (Django2) - pointing to index action in 'static_pages' app's views.py as home page:
main urls.py:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('static_pages.urls')), #included urls from static_pages app
]
static_pages/urls.py:
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name="index"),
]
static_pages/views.py
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("First Django2 Message!")
Note that 'static_pages' app is added to INSTALLED_APPS in settings.py.
Now, calling http://127.0.0.1:8000/ I got: "First Django2 Message!".

Django V2.1 reverse is not a valid view function or pattern name

I am very new to Django and I have created two apps in my project one is login & server_status. I have used the namespace method to access my login and server_status across both apps. It is working fine when I tried to access index but when I tried to access dashboard in server_status it is not working.
Reverse for 'server_status' not found. 'server_status' is not a valid view function or pattern name
# server_status URL
from django.urls import path
from . import views
urlpatterns = [
path('', include('server_status.urls', namespace='server_status')),
path('', include('login.urls', namespace='login')),
path('admin/', admin.site.urls),
]
# login app URL
app_name = 'login'
urlpatterns = [
path('login', views.index, name='index')
]
# project URL
from django.contrib import admin
from django.urls import include, path
app_name = 'server_status'
urlpatterns = [
path('', views.index, name='index'),
path('dashboard/', views.dashboard, name='dashboard'),
path('server_status/', views.server_status, name='server_status'),
path('request_access/', views.request_access, name='request_access'),
]
In server_status/templates/server_status/index.html
Login
I know this is very simple one, but it makes me very complicated.
In the posted code, you define urlpatterns twice, so the first set of urlpatterns is overridden by the second. Since server_status is defined in the first set, it is not present in memory, since you obliterate it in the second definition. index is the only pattern that survives. I think what you meant to do in the second stanza was to add to the urlpatterns with:
urlpatterns += [...]
To declare namespace you should do something like this:
# project URL
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('', include('server_status.urls', namespace='server_status')),
path('', include('login.urls', namespace='login')),
path('admin/', admin.site.urls),
]
then you can use
Login
or
Server status index

Categories

Resources