Django runserver - python

When I run my django server this occurs:
Page not found (404)
Request Method:GETRequest URL:http://127.0.0.1:8000/myapp/
Using the URLconf defined in djproject.urls, Django tried these URL patterns, in this order:
admin/
The current path, myapp/, 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.
This is my djproject urls.py file:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [path('admin/', admin.site.urls), path('myapp/',
include('myapp.urls'))]
this is myapp urls.py:
from django.urls import path
from . import views
urlpatterns = [path('', views.home, name='my app-home')]
my app views.py:
from django.shortcuts import render
from django.http import HttpResponse
def home(request):
return (HttpResponse('<h1>Hello</h1>'), )

You have to remove the comma from the end of the line HttpResponse

Related

Django 404 error even though I've created the path and view

I'm just beginning to learn Django.
I've created a simple web sub-app called 'flavo' inside of another one called 'djangoTest'
When I run http://127.0.0.1:8000/flavo
it correctly displays
Hello, World!
then when I run http://127.0.0.1:8000/flavo/a it should show
Hello, a!
But instead I get:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/flavo/a
Using the URLconf defined in testDjango.urls, Django tried these URL patterns, in this order:
admin/
flavo [name='index0']
flavo a [name='a']
The current path, flavo/a, didn’t match any of these.
in testDjango/hello/views.py I have
from django.http import HttpResponse
from django.shortcuts import render
def index0(request):
return HttpResponse("Hello, world!")
def a(request):
return HttpResponse("Hello, a!")
In testDjango/flavo/url/py I have
from django.urls import path
from . import views
urlpatterns = [
path("", views.index0, name="index0"),
path("a", views.a, name="a"),
]
The only other file I've changed is , testDjango/testDjango/urls.py"
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('flavo', include("flavo.urls")),
]
I'm confused why I can't access http://127.0.0.1:8000/flavo/a
Add '/'.
like this.
# testDjango/testDjango/urls.py
path('flavo/', include("flavo.urls"))

Django tried these URL patterns: The empty path didn’t match any of these

I am getting this error
Request Method: GET Using the URLconf defined in Webwork.urls, Django
tried these URL patterns, in this order:
admin/
^ ^department/$
^ ^department/([0-9]+)$
^ ^employee/$
^ ^employee/([0-9]+)$
The empty path didn’t match any of these.
here is my code:
from django.contrib import admin
from django.urls import path
from django.conf.urls import url,include
urlpatterns = [
path('admin/', admin.site.urls),
url(r'^',include('EmpApp.urls'))
]
and
from django.conf.urls import url
from EmpApp import views
urlpatterns=[
url(r'^department/$',views.departmentApi),
url(r'^department/([0-9]+)$',views.departmentApi),
url(r'^employee/$',views.employeeApi),
url(r'^employee/([0-9]+)$',views.employeeApi),
]
Can anyone please help me solve this error?
you didn't have any error it's you didn't have created any page for http://127.0.0.1:8000 this path
use path instead of url as i shown in my code
from django.contrib import admin
from django.urls import path
from django.conf.urls import include
urlpatterns = [
path('',include('app.urls')),
path('admin/', admin.site.urls),
]
and
from django.urls import path
from app import views
urlpatterns=[
path('',views.index), # added this line
path('departments',views.departmentApi),
path('departments/<slug:id>',views.departmentApi)
]
and add code to EmpAppapp\views.py file
def index(request):
return render(request, './index.html')
now create index.html File in EmpAppapp\templates\index.html and write anything you want to display on the http://127.0.0.1:8000/ page
<h1>hello</h1>
if you get ImportError: cannot import name 'url' from 'django.conf.urls' this error than refer this page
ImportError: cannot import name 'url' from 'django.conf.urls' after upgrading to Django 4.0

how to fix this 404 error in django urlconfig patterns

I'm creating a basic app in my Django project. I mapped the views to url. While running this project it is showing
404 page not found.
in urls.py
from django.urls import path
from . import views
urlpatterns=[
path(' ',views.index,name="index"),
]
in views.py
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
return HttpResponse("hello world");
in project/urls.py
from django.urls import include,path
from django.contrib import admin
urlpatterns = [
path("myapp/ ",include('myapp.urls')),
path('admin/', admin.site.urls),
]
I expect the output to be like hello world
Remove space character form your patters
In urls.py change
path(' ',views.index,name="index") to path('',views.index,name="index")
In project/urls.py change
path("myapp/ ",include('myapp.urls')), to path("myapp/",include('myapp.urls')),

django 404 not found page

I am trying to show a blogpost on the site.
Below is details of urls and view file details but still it showing a 404 not found page
urls.py
from django.urls import path
from . import views
urlpatterns = [
path("", views.index, name="ShopHome"),
path("blogpost/", views.blogpost, name="blogpost")
]
views.py
from django.shortcuts import render
# Create your views here.
from django.http import HttpResponse
def index(request):
return render(request, 'blog/index.html')
def blogpost(request):
return render(request, 'blog/blogpost.html')
Showing:
404 not found page
you should include urls.py of your application to project urls.py
actually if your project name is "mysite" and your application name is "blogspot", you should include mysite/blogspot/urls.py on this file mysite/mysite/urls.py like this:
from django.urls import path, include
urlpatterns = [
path('', include('blogspot.urls')),
]
Actually, i forget to add the slash at the time of registering the url of this app in urls

Why is this Django URL resolution not working

I have the following Django url configuration in /urls.py file
from django.conf.urls import url
from django.conf.urls import include, url
from django.contrib import admin
import core
import core.views as coreviews
urlpatterns = [
#url(r'^admin/', admin.site.urls),
#url(r'^create/$', coreviews.handleFile), #Uncommenting this works
url(r'^create/$', include('core.urls')), #This doesn't work
url(r'^$', include('core.urls')),#Make msg app the the default one
#url(r'^upload/', include('core.urls')),
]
My core/urls.py file is defined as follows
from django.conf.urls import include, url
from django.contrib import admin
import core.views as coreviews
from django.core.urlresolvers import reverse
from django.conf import settings
from django.contrib.auth.decorators import login_required
urlpatterns = [
url(r'my$', coreviews.handleFile),
url(r'^$', coreviews.index),
]
Finally, core/views.py file is defined as follows
def index(request):
return render_to_response('AudioRecorder/index.html', context_instance=RequestContext(request))
def handleFile(request):
return render_to_response('AudioRecorder/index.html', context_instance=RequestContext(request))
show_urls output is as follows
/ core.views.index
/create/ core.views.index
/create/my core.views.handleFile
/my core.views.handleFile
Question:
I am running on localhost. When I send get request to url http://localhost:8000/, I get proper response with 200 code. But when I send get request to url http://localhost:8000/create/my, I get 404 not found error. Why is it happening like this? Shouldn't second URL also return 200 code?
Because you're terminating the including url pattern with a $, which means the end of the pattern. Nothing can match past that. Remove those characters:
url(r'^create/', include('core.urls')),
url(r'', include('core.urls')),

Categories

Resources