Page not found (404) in Django view - python

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

Related

Using the URLconf defined in lecture3.urls, Django tried these URL patterns, in this order: admin/ The current path, hello/, didn’t match any of thes

I am new to using Django and following a tutorial (https://video.cs50.io/w8q0C-C1js4?screen=gytX_rSwZRQ&start=1160).
So far this course has been great! However, now I am stuck at successfully creating a new app using Django.
Here is the result of my efforts: [Lecture3 Page not found
Here is what the result should be: Hello, World page when found tutorial
As far as I know I've done everything correctly, perhaps I am missing something?
Below is the code I am using, which results in Django returning the error in the title:
urls.py lecture3:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('hello/', include("hello.urls"))
]
urls.py hello:
from django.urls import path
from . import views
urlpatterns = [
path("", views.index, name="index")
]
views.py:
from django.http import HttpResponse
from django.shortcuts import render
# Create your views here.
def index(request):
return HttpResponse("Hello, World!")
settings.py:
INSTALLED_APPS = [
'hello',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
I am currently using: Python 3.9.4, and Django 3.2.3
Any and all help that can be provided will be greatly appreciated.
Thanks in advance, Richardson
The issue is coming from ‘attempttwo.urls’ so revise your settings file.
the path you provided is path('hello/', include("hello.urls")) you need to add "/hello" at the end of the url in the browser, when you run python3 manage.py runserver.

Not Found: /hello/

from django.http import HttpResponse
from django.shortcuts import render
# Create your views here.
def index(request):
return HttpResponse("Hellow, World !")
from django.urls import path
from . import views
urlspattern=[
path("",views.index,name="index")
]
from django.contrib import admin
from django.urls import include,path
urlpatterns = [
path('admin/', admin.site.urls),
path("hello/", include("hello.urls"))
]
INSTALLED_APPS = [
'hello',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
Error I am gettting is
Using the URLconf defined in helloproject.urls, Django tried these URL patterns, in this order:
admin/
The current path, hello/, didn't match any of these.
you have a typo error on urlpattern
urlspattern=[
path("",views.index,name="index")
]
you have to change that to
urlpatterns=[
path("",views.index,name="index")
]
In this line change urlpattern to urlpatterns
urlpatterns=[
path("",views.index,name="index")
]

Importing Errors

I am getting errors in almost all imports please help.
i'm using django 2.2.5, python 3.7, but getting errors in unresolved import'django.contrib' ,unresolved import'django.urls',unresolved import'django.shortcuts' etc
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path("", include("hello.urls")),
path('admin/', admin.site.urls),
]
from django.shortcuts import render
from django.http import HttpResponse
def home(request):
return HttpResponse("Hello, Django!")
Message=No module named 'django'
File "C:\Users\Sharf\Desktop\python projects\p1\calc\urls.py", line 1,
in <module>
from django.urls import path
Can you post your settings.py file ? I think the problem comes from here ...
INSTALLED_APPS = [
'mainApp.apps.nameApp',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'channels'
]
ROOT_URLCONF = 'app.urls'

Adding new Django app - Getting 404 page not found

I have created a directory called vsdk which includes service_development (a voice application) and my newly created application called dashboard. Unfortunately I can’t seem to get the main page of dashboard to work. I get a 404 error when trying to access 127.0.0.1:8000/dashboard/, while doing everything similar to what I’ve done with service_development.
Would someone be able to help me?
Vsdk\settings.py
INSTALLED_APPS = [
'vsdk.service_development.apps.ServiceDevelopmentConfig',
'vsdk.dashboard.apps.DashboardConfig',
'storages',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
Vsdk\dashboard\apps.py
from django.apps import AppConfig
from django.utils.translation import ugettext_lazy as _
class DashboardConfig(AppConfig):
name = 'vsdk.dashboard'
verbose_name = _("Dashboards")
Vsdk\urls.py
from django.conf.urls import url, include
from django.contrib import admin
from django.conf.urls.static import static
from django.utils.translation import ugettext_lazy as _
from django.conf import settings
from django.urls import include, path
admin.site.site_header = _("Petrichor Rain Service")
urlpatterns = [
url(r'^', admin.site.urls),
url(r'^vxml/', include('vsdk.service_development.urls')),
url(r'^vxml/', include('vsdk.dashboard.urls')),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Vsdk\dashboard\urls.py
from django.shortcuts import render
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]

Django urls giving 404 error

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.

Categories

Resources