Django page not found error - django

Hello I am trying to solve the error which keeps on occurring;
my current code on mysite/urls.py is
from django.contrib import admin
from django.urls import path
from django.conf.urls import url
from polls import views
admin.autodiscover()
urlpatterns = [
url(r'^polls/$', views.index, name='index'),
url(r'^polls/(?P<question_id>\d+)/$', views.detail, name='detail'),
url(r'^polls/(?P<question_id>\d+)/vote/$', views.vote, name='vote'),
url(r'^polls/(?P<question_id>\d+)/results/$', views.results, name='results'),
url('admin/', admin.site.urls),
]
and I am getting error when I try to access :8001/polls
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8001/
Using the URLconf defined in mysite.urls,
Django tried these URL patterns, in this order:
^polls/$ [name='index']
^polls/(?P<question_id>\d+)/$ [name='detail']
^polls/(?P<question_id>\d+)/vote/$ [name='vote']
^polls/(?P<question_id>\d+)/results/$ [name='results']
admin/
The empty path didn't match any of these.
Please help me to solve this error which makes me cannot access to the website.

Related

Django url can't work after i move it down from the list

I am making a blog app using Django. In my blog app, i have 6 paths. This is my app urls.py
from . import views
from .views import AddPostView
from django.urls import path
from .feeds import LatestPostsFeed
urlpatterns = [
path('', views.PostList.as_view(), name='home'),
# path('<slug:slug>/', views.PostDetail.as_view(), name='post_detail'),
path('upload/', views.image_upload_view, name="upload"),
path('add_post/', AddPostView.as_view(), name='add_post'),
path('<slug:slug>/', views.post_detail, name='post_detail'),
path("feed/rss", LatestPostsFeed(), name="post_feed"),
]
The app is working perfectly, but whenever i try to move the path('<slug:slug>/', views.post_detail, name='post_detail') up from the add_post url (code below) and try to open add_post
from . import views
from .views import AddPostView
from django.urls import path
from .feeds import LatestPostsFeed
urlpatterns = [
path('', views.PostList.as_view(), name='home'),
# path('<slug:slug>/', views.PostDetail.as_view(), name='post_detail'),
path('upload/', views.image_upload_view, name="upload"),
path('<slug:slug>/', views.post_detail, name='post_detail'),
path('add_post/', AddPostView.as_view(), name='add_post'),
path("feed/rss", LatestPostsFeed(), name="post_feed"),
]
suddenly i have an error saying :
Page not found (404)
No Post matches the given query.
Request Method: GET Request URL: http://localhost:8000/add_post/
Raised by: blog.views.post_detail
Using the URLconf defined in mysite.urls, Django tried these URL
patterns, in this order:
admin/
[name='home']
upload/ [name='upload']
<slug:slug>/ [name='post_detail']
The current path, add_post/, matched the last one.
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.
I don't understand why it happens and i am curious and want to know what has just happened. maybe there are some concepts about django urls that i don't know. Can you explain to me what happen in this error?

Django -Page not found (404) Request Method: GET Method

I am very new to Django ,I am trying to run a URL ,however I get this error of page not found.I went through almost 90% of the posts here but nothing worked for me.
Here are three .py files
views.py.....
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse('Hello world....')
**products\urls.py****
from django.urls import path
from . import views #.means current folder ,we are importing a module
urlpatterns=[
path(' ', views.index),
]
pyshop\urls.py.......
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('products/',include('products.urls'))
]
Error I get.....
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/products/
Using the URLconf defined in pyshop.urls, Django tried these URL patterns, in this order:
admin/
products/
The current path, products/, didn't match any of these.
I think you have to delete the space on the path :
urlpatterns=[
path(' ', views.index),
]
Like :
urlpatterns=[
path('', views.index),
]

I tried to create an django hello world program then I got this error

from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('love/', include('love.urls')),
path('admin/', admin.site.urls),
]
I tried this code by seeing n online documentation and I see the code as same as the one in the documentation but I see the error and I can not start practicing django.
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('love/', include('love.urls')),
path('admin/', admin.site.urls),
]
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/
Using the URLconf defined in ad.urls, Django tried these URL patterns, in this order:
^love/
^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.
It is being shown on the web
you must define empty path to use 127.0.0.1:8000
path('', include('love.urls')),

The current path, index, didn't match any of these. http://127.0.0.1:8001/index

I setup webpage via cmd for Django.However, i had problem when accessing index page of 127.0.0.1:8001. I tried to update urls.py but still have problem.
Browser page error:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8001/index
Using the URLconf defined in barry.urls, Django tried these URL patterns, in this order:
admin/
The current path, index, didn't match any of these.
urls.py file:
"""{{ project_name }} URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/{{ docs_version }}/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from django.conf.urls import url
from sign import views
urlpatterns = [
path('admin/', admin.site.urls),
path('index/', views.index),
]
views.py
from django.shortcuts import render
# Create your views here.
def index(request):
return HttpResponse("Hello Django!")
You are mistaking in path pattern. Confusing with path, re_path and url.
Have a look at documentation Django Urls - Path.
urlpatterns = [
path('admin/', admin.site.urls),
path('index/', views.index),
]

Routing error: trying to route a defined route in urls.py

I encountered a strange behavior of Django urls. Although forecast/upload url is defined in the urls.py, Django is saying
Page not found (404)
Request Method: POST
Request URL: http://localhost:8000/forecast/upload/
Using the URLconf defined in myproject.urls, Django tried these URL patterns, in this order:
polls/
admin/
forecast/ [name='index']
forecast/ **forecast/upload/** [name='upload']
forecast/ help.html [name='help']
The current path, **forecast/upload/**, didn't match any of these.
Project url.py:
from django.contrib import admin
from django.urls import include, path
from django.conf.urls import url
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
path('forecast/', include('forecast.urls')),
]
Application urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
#url(r'^$', views.home, name="home"),
path('forecast/upload/', views.upload, name="upload"),
path('help.html', views.help, name="help"),
]
You have specified "forecast" twice; once at project level and once in the app. So your URL would be "forecast/forecast/upload".
Presumably, you don't want that, in which case you should remove the "forecast" from the pattern in the app urls.