Django urls giving 404 error - python

Django is giving me a 404 error whenever I try to access "blog/" on my site, but I've defined the URLs I want and they should be matching that.
Main urls.py:
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
from blog import views
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'mySiteProject.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
)
blog.urls.py:
from django.conf.urls import patterns,url
from blog import views
urlpatterns = patterns(
url(r'^$',views.index,name='index')
)
404 page:
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/blog/
Using the URLconf defined in mySiteProject.urls, Django tried these URL patterns, in this order:
^admin/
The current URL, blog/, didn't match any of these.
Site structure:
mySiteProject
blog
admin.py
models.py
tests.py
views.py
urls.py
__init__.py
mySiteProject
wsgi.py
settings.py
urls.py
__init__.py
manage.py
db.sqlite3
Installed apps:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog'
)

patterns requires a prefix as its first argument followed by zero or more arguments. So this:
urlpatterns = patterns(url(r'^$',views.index,name='index')) # won't work
in blog.urls.py should look like this:
urlpatterns = patterns('', url(r'^$', views.index, name='index')) # now has a prefix as first argument
In its present state, the patterns function in blog.urls.py will return an empty pattern_list, which means that url(r'^blog/', include('blog.urls')) will return no patterns.

Related

I followed the django example exactly. But it doesn't work

**
I followed the django example exactly. But it doesn't work. Web page just show the rocket to me.**
my project name: web
app name: main
web.settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'main',
]
web.urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('main/', include('main.urls')),
path('admin/', admin.site.urls),
]
main.urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
main.views.py
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
I hope web page shows "Hello, world. You're at the polls index." to me.
In your main urls file you have set:
urlpatterns = [
path('main/', include('main.urls')),
path('admin/', admin.site.urls),
]
Which actually means, that you need to go for localhost:8000/main for your urls in "main" app. If you want to do it without specifying main in the url in your browser, then change it to:
urlpatterns = [
path('', include('main.urls')),
path('admin/', admin.site.urls),
]

Making a portfolio website with Django and got Page not found (404)

Hello I'm making a portfolio website with Django, it was going fine but in the last steps following a guide I got this error
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/
Using the URLconf defined in personal_portfolio.urls, Django tried these URL patterns, in this order:
admin/
projects/
The empty path didn't match any of these.
This is my project folder
Here is personal_portfolio - urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path("admin/", admin.site.urls),
path("projects/", include("projects.urls")),
]
And projects - urls.py
from django.urls import path
from . import views
urlpatterns = [
path("", views.project_index, name="project_index"),
path("int:pk>/", views.project_detail, name="project_detail"),
]
Also the section in settings.py
> INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'projects',
]
Here is the link of the repo of the guide I'm following https://github.com/realpython/materials/tree/master/rp-portfolio
I downloaded the repo and tried to run the server and got the same error, how?
Also I searched here people with the same error even someone who was following the same guide but their solution didn't help mine and it was the same project
A request to the http://localhost:8000/ endpoint will indeed not trigger a view, since the project_index view is located under the http://localhost:8000/projects/ endpoint.
If you want to change this, you should alter the path to the projects.url with:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('projects.urls')),
]
There is also a typo in the projects/urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('', views.project_index, name='project_index'),
path('<int:pk>/', views.project_detail, name='project_detail'),
]

Page not found (404) in Django view

I am trying to wire up a view based on the Django tutorial (1.8), and am for some reason not getting a basic url to work:
Page not found (404)
Settings
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'events',
)
In the main folder, I have these_events/these_events/urls.py
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^/', include('events.urls')),
]
In the events app, I have these_events/events/urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r"^$", views.search_db, name='search-db')
]
these_events/events/views.py:
from django.shortcuts import render
from django.http import HttpResponse
def search_db(request):
return HttpResponse("hello, world")
This has me befuddled as I followed the example, and this is the way I remember using Django in the past.
In these_events/these_events/urls.py
try changing
url(r'^/', include('events.urls')),
to
url(r'', include('events.urls')),

Django cant import my app in urls.py

I have issues importing my app in url patterns, but everything seems in place. I get errors in from fairy_app import views and an error name 'News' is not defined
directory:
fairy-
fairy-
_init_.py
settings.py
urls.py
wsgi.py
fairy_app-
_init_.py
admin.py
models.py
tests.py
views.py
db.fairy
manage.py
views.py
from django.shortcuts import render
from django.views.generic import TemplateView
# Create your views here.
class News(TemplateView):
template_name = 'news.html'
urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
from fairy_app.views import News
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'fairy.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^news/', News.as_view()),
)
setting.py
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'fairy_app',
)
In your urls.py you reference the News view without importing it. You import the views file as module.
So you can either do:
views.News.as_view()
or:
from fairy_app.views import News
2nd way is shorter but gets inconvenient if you have many view classes, so I prefer the first one.
I found the same problem,but got sorted it out.
Create a new python file in app-level for urls
use include function in fairy-/urls file to include all other urls of the app and import views in the new file
for example;
#in url.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^greet/',include('greetings.url')),
]
#created a new file(url.py) in greetings app
#in greetings/url.py
from . import views
urlpatterns = [
url(r'^$',views.greet),
]
#greet is the function in my views`
I had the same problem. I worked around it by giving the full path
from fairy.fairy_app.views
alternatively
from ..fairy_app.views

Page not found (404) django

Can anyone help me with this issue. When I try to access I get the following error.
Request Method: GET
Request URL:
Using the URLconf defined in myproject.urls, Django tried these URL patterns, in this order:
^admin/
^myproject/$ [name='home']
The current URL, , 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.
urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
#url(r'^$', 'myproject.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^booking/$', 'booking.views.home', name ='home'),
)
views.py
from django.shortcuts import render
#..
# Create your views here.
def index(request):
return render("Hello, guesthouse!!")
settings.py
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'booking',
)
ROOT_URLCONF = 'myproject.urls'
WSGI_APPLICATION = 'myproject.wsgi.application'
admin.py
from django.contrib import admin
from booking.models import Bookings
# Register your models here.
admin.site.register(Bookings)
Thank you,
Rads
You are getting 404 because 'home' doesn't exist in views.py or by mistake, you have named the view wrong.
below i have changed the name of view to 'home' that matches the view specified in your urls.py
views.py
from django.shortcuts import render
def home(request):
"""
home view
"""
return render("Hello, guesthouse!!")
You don't appear to have a home function in booking.views.
Expanded answer: Each URL spec has a couple of parts that tell Django how to route the request. In your post you have:
url(r'^booking/$', 'booking.views.home', name ='home'),
In order for the mapping to work you need to have the function:
booking.views.home
You have three options here, the first is to implement another view function, the second is that you can rename booking.views.index to booking.views.home, and the third is that you can change the url spec to the following:
url(r'^booking/$', 'booking.views.index', name ='home'),
All three of these options assume that you have a directory structure like:
|-<project_root_dir>
| |
| |- <project>
| |- booking
| | |- <other files>
| | |- views.py
Your error is in urls.py. Your last pattern is incorrect. I think it should be
url(r'^booking/$', 'booking.views.index'),

Categories

Resources