How to avoid Page 404 Error on my first app - python

I am following this tutorial.
https://www.youtube.com/watch?v=a48xeeo5Vnk
Here is my code for views:
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def home(request):
return HttpResponse('<h1>This is our blog</h1>')
def about(request):
return HttpResponse('<h1>This is our About Page</h1>')
def next(request):
return HttpResponse('<h1>This is our next Page</h1>')
This is my App urls page code
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='blog-home'),
path('about/', views.about, name='blog-about'),
path('next/', views.next, name='blog-NEXT'),
]
This is my main project URL code
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('firstapp', include('firstapp.urls')),
path('admin/', admin.site.urls),
]
Now when I try this with only single view i.e default page '', it works alone, with no more pages mentioned in code, however when I add more views it gives me a 404 page error. I believe the code is fine and it shoud work, but somehow it chose not to.
Tried different browser, tried forums but nothing.
Here is the error.
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/firstapp/
Using the URLconf defined in first.urls, Django tried these URL patterns, in this order:
firstapp [name='blog-home']
firstapp about/ [name='blog-about']
firstapp next/ [name='blog-NEXT']
admin/
The current path, firstapp/, didn't match any of these.
Any assistance would be appreciated.

Do below changes
from:
path('firstapp', include('firstapp.urls')),
to:
path('firstapp/', include('firstapp.urls')),

Related

Page Not Found (404) Error in simple Django 2.2 application

I used the official Django documentation to create this code and I consistently get a 404 error when I put localhost:server/polls/ in the search bar. I followed the tutorial pretty much identically but it still doesn't seem to work. Anyone know a solution to get text displayed at localhost:server/polls/ or at localhost:server?
polls/views.py:
from django.shortcuts import render
from django.http import HttpResponse
def index(response):
return HttpResponse('Polls')
polls/urls.py:
from django.urls import path
from . import views
urlpatterns =[
path('', views.index, name='index'),
]
mysite/urls.py:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('polls/', include('polls.urls')),
]
Error message:
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order: admin/
The current path, polls/, didn't match any of these.
I have looked at lots of different potential solutions and none of them seem to work. Please let me know if you need more info/code, thanks

python: Django Page not found (404)

app urls.py
from django.urls import path
from . import views
urlpatterns = [
path('hello', views.index, name='index')
]
project urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('hello/', include("hello.urls"))
]
views.py
from django.http import HttpResponse
from django.shortcuts import render
# Create your views here.
def index(request):
return HttpResponse("Hello, World!")
I've re-watched the tutorial several times and still can't find the error.
Since your project's urls.py includes hello using the prefix 'hello/' in
path('hello/', include("hello.urls"))
and your app's urls.py also has hello as the path in
path('hello', views.index, name='index')
you will need to access /hello/hello in your browser to get to a view that doesn't 404.
Django's technical 404 page (the one with the yellow background) should show the available paths -- if you don't get the technical 404 page, ensure you have the DEBUG setting turned on.

How to Change Django Default Page to your Design?

I uploaded my site to the live server, but still, I am seeing Django default page, I updated my urls.py file, but still, it's not working. and it's showing me this output. Please let me know where I am Mistaking.
I am trying to access this mydomain.com/dashboard
Using the URLconf defined in yoursite.urls, Django tried these URL patterns, in this order:
admin/
The current path, dashboard, didn't match any of these.
here is my urls.py file...
urlpatterns = [
path('admin/', admin.site.urls),
url(r'^dashboard/', include('dashboard.urls')),
url(r'^accounts/', include('accounts.urls')),
url('', include('frontpanel.urls')),
path('', views.index, name='index'),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
And here is my views.py file...
def index(request):
category = Category.objects.all().order_by('created_at')
return render(request, "base.html", {'category':category})
What version of Django are you using?
Try changing url to re_path (remember to import it first). I think url has been deprecated.
After Analyzing the question u never mentioned ur app urls.py you only mentioned project urls.py . As u have url(r'^dashboard/', include('dashboard.urls')), in ur project urls.py, there must be a file in your app also named urls.py which handles urls having prefix /dashboard/ , by default django doesnt make that file you need to manually make it add ur function names to redirect . For example this is my main urls.py file
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('display_report/', include("display_report.urls"))
]
then u have to make a file named urls.py in ur app also to handle the requests and redirect the the approaite functions , in my display_report app i made a urls.py that looks like this
from django.contrib import admin
from django.urls import path, include
from display_report import views
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [path('', views.index)]
And then it will redirect to function named index in ur views.py file inside ur app
from django.shortcuts import render, redirect, HttpResponse
# from .models import Employee, Tasks, Report, Fileupload, Fileuploadnext
from django.views.decorators.csrf import csrf_exempt
# Create your views here.
def index(request):
return render(request."index.html")
Here my ur will be mydomain.com/display_report and my index.html file will be inside the template folder

I'm going through a django tutorial but currently stucked in an error i.e.. "Page Not Found" on admin page

Page not found (404)
Request Method:
GET
Request URL:
http://127.0.0.1:8000/
Using the URLconf defined in just.urls, Django tried these URL patterns, in this order:
1. admin/
2. shop/
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.
Here is my code:
Main ecom\urls.py:------------>>>>
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('shop/', include('shop.urls'))
]
Now shop\urls.py:-------------->>>>
from django.urls import path
from . import views
urlpatterns = [
path("",views.index, name="ShopHome")
]
And shop\views.py:-------------->>>>
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("Shop Index")
Please help me guys
The only pages that should be working for you right now is /shop/ and /admin/. There is no root page because you have an include, which always start with /shop/ and checks the shop/urls. Not sure what you want, but if you want the root page to give you the index view, you can change ecom/urls shop url to:
path('', views.index, name="ShopName)
Dont forget to import the views of course
You have not defined blank URL. In main URLs.py, you have used path('shop/', include('shop.urls')), and in shop urls.py path("",views.index, name="ShopHome"). this path will redirect to http://127.0.0.1:8000/shop/ not http://127.0.0.1:8000/.
If you want blank url then do this:
ecom\urls.py:
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('shop.urls'))
]
shop\urls.py
urlpatterns = [
path("",views.index, name="ShopHome")
]
remember whatever you add in path('', include('shop.urls')), it will be added in all URLs written inside shop app.
If you want a blank URL and also "shop" added in rest of the URLs, do this:
From shop import views
urlpatterns = [
path('admin/', admin.site.urls),
path('shop/', include('shop.urls'))
path('',views.index, name="ShopHome")
]

Still getting 404 page not found on Django using correct route files and settings

I'm going through the polls Django tutorial and have searched for answers to this. So far:
I made sure I'm using the right directories. The main urls.py is in mysite/mysite/, the polls urls.py is in mysite/polls/urls.py.
Tried adding 'polls' to INSTALLED_APPS in the settings.py of mysite/mysite.
Am making sure that I am requesting 127.0.0.1:8000/polls and not 127.0.0.1:8000
I am using Python 3.4 and Django 1.9, same as the tutorial.
I am still receiving this message:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/polls
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^polls/
^admin/
The current URL, polls, didn't match any of these.
mysite/mysite/urls.py
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^polls/', include('polls.urls')),
url(r'^admin/', admin.site.urls),
]
mysite/polls/urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^%', views.index, name='index'),
]
mysite/polls/views.py
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world")
In your polls/urls.py
change url(r'^%', views.index, name='index') to url(r'^$', views.index, name='index').
Also in your urls.py, you have route for 127.0.0.1:8000/polls/ and you request for 127.0.0.1:8000/polls . Notice the trailing slash.
So you should request to 127.0.0.1:8000/polls/ .
Add APPEND_SLASH = True in your settings.py file so that it would redirect 127.0.0.1:8000/polls to 127.0.0.1:8000/polls/ .
So i tried your Index view with your urls.py.
It works, the only thing different is in your "Request URL, you must request for - 127.0.0.1:8000/polls/%
changing your urls to
url('^/%', index) - polls URLs.py
url('^polls', include('polls.urls')) - project URLs
This will work.

Categories