Django 'Page not found' error when setting up URLs - python

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

Related

Django FirstApp

I am following the django tutorial (found here):
views.py code:
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the poll index.")
urls.py code (this is inside polls app urls.py file):
from django.urls import path, include
from django.conf import settings
from . import views
urlpatterns = [path(r'^$', views.index, name='index'), ]
urls.py code ( this is root urls.py file code):
from django.contrib import admin
from django.urls import include, path
urlpatterns = [path('polls/', include('polls.urls')),path('admin/',admin.site.urls), ]
Here is my run command : python manage.py runserver 8080
I tried to run it today, but I am getting the following error:
Page not found (404)
Request Method: GET
Request URL: http://35527a91f40c4e228d6c464d8a8c8487.vfs.cloud9.eu-west-1.amazonaws.com/
Using the URLconf defined in PollApp.urls, Django tried these URL patterns, in this order:
poll/
admin/
The empty path didn't match any of these.
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
You are using regex-syntax with path, you should use the empty string instead:
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
Furthermore, you use a poll/ prefix here, so that means you either need to visit localhost:8000/poll/, or change this to an empty prefix:
urlpatterns = [
path('', include('polls.urls')),
path('admin/', admin.site.urls),
]
Djano errors are self-explanatory, follow its instructions, and are good to go.Also is there the Indentation in your first return ?
from . import views
urlpatterns = [
path('', views.index, name='index'),
]

Django page not found, help would be appreciated

I was taking a youtube tutorial on making a basic website using django and I got this error when coding:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
app/
admin/
The empty path didn't match any of these.
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
This is my code:
For urls.py/mysite:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('app/', include('myapp.urls')),
path('admin/', admin.site.urls),
]
For views.py:
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world!")
For urls.py/myapp:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
You need to add root level path to access the path you specified http://127.0.0.1:8000/:
urls.py/mysite
urlpatterns = [
path('', views.index, name='home'),
path('app/', include('myapp.urls')),
path('admin/', admin.site.urls),
]
views.py/mysite
def index(request):
return HttpResponse('This is home page')
Since you specified:
path('app/', include('myapp.urls')),
It means all the paths in myapp.urls are prefixed with app/. So you can access the index view with: http://127.0.0.1:8000/app/. Or if you want to access the index view with http://127.0.0.1:8000/ you rewrite the path to:
path('', include('myapp.urls')),

Keep getting 404 errors with django 2.2

I am following this tutorial https://docs.djangoproject.com/en/2.2/intro/tutorial01/
and I am getting 404 errors when I try the first test saying it cannot find "polls/" below is some of my code
in mysite/urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
in polls/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
I tried searching but I can't find anything for django 2.2
Below is a screenshot of the error message
So the problem was my directory structure. it was mysite/urls.py when it should've been mysite/mysite/urls.py in the directory with settings.py not in the directory with manage.py

Django Url Pattern Not Being Found

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.

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