I have problem Page not found (404) Django - python

import jobs.views
urlpatterns = [
path('admin/', admin.site.urls),
path('nick', jobs.views.nick, name='nick'),
]
enter code here
def nick(request):
return render(request, 'jobs/nick.html')
I got:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/nick/
Using the URLconf defined in FadhelPro.urls, Django tried these URL patterns, in this order:
admin/
nick/ [name='nick']
The empty path didn't match any of these.

Instead of:
path('nick', jobs.views.nick, name='nick')
Try:
path(r'^nick/', jobs.views.nick, name='nick'),

If python -V is 3.x or above then:
urlpatterns = [
path('admin/', admin.site.urls),
path('nick/', jobs.views.nick, name='nick'),
]
def nick(request):
return render(request, 'jobs/nick.html')
If python -V is 2.x or above then:
urlpatterns = [
path(r'^admin/', admin.site.urls),
path(r'^nick/', jobs.views.nick, name='nick'),
]
def nick(request):
return render(request, 'jobs/nick.html')
Html templates could not be rendered without an app but you can write the code of the html in a http response!
If you create an app instead that will be more hacky and good.If this doesn't work then you go for an app and Get some tutorials of django before you mess up!!

Related

How to avoid Page 404 Error on my first app

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

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")
]

Why am I getting a 'Page Not Found' error in Django's URL patterns?

I'm new to Django and I'm just trying to create a small application to output 'Hello World'. When I run my code, it says 'Page not found' and gives me the following reason:
'Using the URLconf defined in helloworld_project.urls, Django tried
these URL patterns, in this order:
admin/
home/
The empty path didn't match any of these.'
I'm a bit confused as to why this is happening. I've included a few samples of my code. Any assistance would be greatly appreciated!
urls.py
from django.contrib import admin
from django.urls import path
from my_app.views import HomeView
urlpatterns = [
path('admin/', admin.site.urls),
path('home/', HomeView.as_view())
]
views.py
from __future__ import unicode_literals
from django.shortcuts import render
from django.views.generic import TemplateView
class HomeView(TemplateView):
template_name = 'index.html'
index.html
<html>
<head><title>Home Page</title></head>
<body>
Hello world
</body>
</html>
If you're running Django in the port for example 8000, the url:
localhost:8000/
is the empty pattern url.
according to your URLs definitions you've to:
localhost:8000/home/
or change your url to:
urlpatterns = [
path('admin/', admin.site.urls),
path('^$', HomeView.as_view()) # Point the empty pattern to your view.
]
You need to change your url like:
urlpatterns = [
path('', someview),
path('admin/', include(admin.site.urls) ),
path('home/', HomeView.as_view(), name='somename')
]
The error is obtained because when you run your project, it looks for an empty url like localhost:8000/ instead of localhost:8000/home/.

Django returns Page Not Found for mapped URL

I'm trying to render a basic html/css page in Django and I can't get it to work. It's set up seemingly the same as my index page which does work correctly and the debug explanation from the 404 response seems to show that it's pointing to the right url. Any thoughts on why this isn't working?
*I'm using django 2.1 so I'm using path instead of the old regex url mapping
*The html file exists and is located in templates\base_app\our-story.html
From views.py:
def OurStory(request):
return render(request, 'base_app/our-story.html')
From urls.py:
from base_app import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index, name='index'),
path('our-story', views.OurStory, name='our-story')
]
From settings.py:
TEMPLATE_DIR = os.path.join(BASE_DIR, 'templates')
INSTALLED_APPS = [
(standard django apps)
'base_app'
]
From debug message:
Request Method: GET
Request URL: http://127.0.0.1:8000/our-story.html
Using the URLconf defined in base_app.urls, Django tried these URL
patterns, in this order:
admin/
[name='index']
our-story [name='our-story']
The current path, our-story.html, didn't match any of these.
URL should be - http://127.0.0.1:8000/our-story
We cannot use our-story.html, As we are using framework and already have a route.
Use like this, You will definitely get rid of this error.
I guess path of html template would be only our-story.html If Your project name is base_app
In views.py you should return:
return render(request, template, context)
where
template = 'our-story.html'
and
context = { _your_context_dictionary }
also it is a good practice to use lower case names in the views:
def our_story

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