python: Django Page not found (404) - python

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.

Related

cant get hello world in django

I have python version 3.10.1 and django version 4.0
url in project( name = home)`
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('',include('hello.urls')),
path('admin/', admin.site.urls),
]
url in app (name = hello)
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('',include('hello.urls')),
path('admin/', admin.site.urls),
]
views in app
from django.http import HttpResponse
# Create your views here.
def index(request):
return HttpResponse("Hello World")
I tried running server with and without adding 'hello' in setting.py still i get only default page.
stuck from 3 days
You have the same code in urls.py home project as well as in hello app. For Django to use your new view, you need to tell Django the index view is the view you want to display when someone navigates to the site root (home page). So you need to change the urls.py of hello app as:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index),
]
In this case, a request to http://localhost:8000/ would route to the index function in the application’s (hello) views.py file.
In the urls.py file in app write the following code:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]

I'm following the Django documentation 01 but it can't work for me

I'm a beginner at Django.
views.py/my_app
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello World")
urls.py/my_app
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
urls.py/my_project
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('product/', include('product.urls')),
path('admin/', admin.site.urls),
]
Error:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/product
Using the URLconf defined in Zero.urls, Django tried these URL patterns, in this order:
admin/
The current path, product, 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.

How to set url in Django?

I'm a newbie in web development and I'm learning to use Django. Unfortunately I have been stuck for more than 24 hours trying to figure out how to set the URL of a web page. I keep getting status 404 error displayed on the browser after running python server. I have checked python documentation and other documentations online but I still don't see anywhere I'm getting it wrong.
I have the following files in the main Django follow:
urls.py
from django.contrib import admin
from django.urls import path, include
urlspatterns [
path('qbank', include ('qbank.url')),
path ('admin/', admin.site.urls),
]
settings.py
INSTALLED APPS = [
'qbank'
.....
]
In my project folder(which I named qbank) I have the following files:
urls.py
from django.urls import path
from . import views
urlspatterns = [
path ('qbank'), views.index, name = 'index'
]
view.py
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse ('Hello from Qbank')
The way you wrote it now, it will require two qbanks, one for the "root" urls.py, and one for the urls.py in the qbanks, hence localhost:8000/qbankqbank. If you only want to access it with qbank, then you remove the qbank for example from the urls.py of the qbanks app. So then the "root" urls.py looks like:
# project_name/urls.py
from django.contrib import admin
from django.urls import path, include
urlspatterns [
path('qbank/', include('qbank.url')),
path ('admin/', admin.site.urls),
]
and the urls.py of your app:
# qbank/urls.py
from django.urls import path
from . import views
urlspatterns = [
path ('', views.index, name='index')
]

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

Path cannot be found in django

I am working on a django project. The project includes 2 apps namely jobs and blog. The url.py of the main project file is:
from django.contrib import admin
from django.urls import path,include
from django.conf import settings
from django.conf.urls.static import static
import jobs.views
urlpatterns = [
path('admin/', admin.site.urls),
path('', jobs.views.home, name= 'home'),
path('blog/', include('blog.urls'))
] + static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
You can understand that I am calling the output of the jobs in the home. The url.py of the blog is:
from django.urls import path
from . import views
urlpatterns = [
path('', views.allblogs, name= 'allblogs'),
]
and the views.py of the blog is:
from django.shortcuts import render
def allblogs(request):
return render(request, 'blog/allblogs.html')
This gives an error that the blog/ can not be found. I must mention I make a allblogs.html in the address project/blog/templates/blog The webpage shows that it tries to find the page in this ditrectory: ...project\jobs\templates\blog\allblogs.html
The error message is:
TemplateDoesNotExist at /blog/
Request URL: http://localhost:8000/blog/
Exception Type: TemplateDoesNotExist
I dont know why it is trying to find it in jobs where it should search for it in blogs folder.
Can someone help? May be I have done something silly..

Categories