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')),
Related
I followed a tutorial by a youtuber known as Mosh, I followed along with his Django tutorial but whenever I enter the URL pattern, it gives me error 404, page not found. This is a screen shot of the explorer tab.
I only edited the views.py, products.urls.py and pyshop.urls.py files.
views.py:
from django.http import HttpResponse
from django.shortcuts import render
def index(request):
return HttpResponse("Welcome to the new products page!")
def new(reqest):
return HttpResponse("New Products")
products.urls.py:
from django.urls import path
from . import views
urlpatterns = [
path("", views.index),
path("new", views.new)
]
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"))
]
the way you have it should work if you have the url as
localhost:8000/products
#or
localhost:8000/products/new
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"))
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
these are my main codes i wrote to create wepapp but i get this 404 error so pleas help
my hello urls.py
from django.urls import path
from hello import views
urlpatterns = [
path("", views.index, name="index")
]
my urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('hello/', include("hello.urls")),
]
my views.py
from django.http import HttpResponse
from django.shortcuts import render
# Create your views here.
def index(request):
return HttpResponse("hello, orld")
It looks like you are trying to access index with
http://127.0.0.1:8000/. This will not work, because your index is included in hello's urls.py, and hello's url starts with hello/.
Try
http://127.0.0.1:8000/hello
instead.
I'm getting a 404 from Django and none of the previous posts on the subject (of which there are many) seem to have helped.
views.py
from django.views.generic.detail import DetailView, SingleObjectMixin
from app.models import MyModel
class MyDetails(DetailView, SingleObjectMixin):
template_name = "app/my_view.html"
model = MyModel
urls.py
from django.conf.urls import include, url
from django.contrib import admin
from app.views import MainList, post_form_upload, MyDetails
urlpatterns = [
url(r'^$', MainList.as_view(), name="main_list"),
url(r'^add_something$', post_form_upload, name="add_something"),
url(r'^my_details/(?P<pk>\d+)$', MyDetails.as_view(), name="my_details"),
]
app/urls.py
from django.conf.urls import url
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
(r'^$', include('app.urls')),
url(r'^admin/', include(admin.site.urls)),
]
when I enter the URL: http://localhost:8000/my_details, I get the following error:
Using the URLconf defined in app.urls, Django tried these URL patterns, in this order:
^$ [name='main_list']
^add_something$ [name='add_something']
^my_details/(?P<pk>\d+)$ [name='my_details']
The current URL, my_details, didn't match any of these.
The other two URLs (/ and /add_something) work fine.
First of all, not sure how you are not running into this issue, but in your app/urls.py
(r'^$', include('app.urls')),
should be
(r'^/', include('app.urls')),
$ indicates the end of regex pattern, and anything inside include() would not be included.
Secondly, None of your URL patterns match my_details/ They only match my_details/<id>
A little more on the documentation of URL regex, etc..