Django: Page Not Found - python

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..

Related

Using the URLconf defined in lec3.urls, Django tried these URL patterns, in this order: admin/ hello/ The empty path didn't match any of these

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.

can not import patterns in django 1.11.4

My project urls.py is as follows:
from django.conf.urls import include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^myapp/', include('myapp.urls')),
]
My app urls.py is as follows:
from django.conf.urls import patterns, include, url
urlpatterns = patterns('', url(r'^hello/', 'myapp.views.hello', name = 'hello'),)
Now , as soon as I try to run it , it gives me the following error:
from django.conf.urls import patterns, include, url
ImportError: cannot import name 'patterns'
Django doesn't require you to use patterns anymore, you can just make urlpatterns a list of urls
urlpatterns = [url(r'^hello/', 'myapp.views.hello', name = 'hello'),]
Django 1.11 loads that Python module and looks for the variable urlpatterns. This should be a Python list of django.conf.urls.url() instances.
And it runs through each URL pattern, in order, and stops at the first one that matches the requested URL
Dont require patterns more.
urlpatterns should be a Python list of url() instances.

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

Django - URL routing issues (cannot import name 'urls')

I am following the Django tutorial on https://docs.djangoproject.com/en/1.7/intro/tutorial03/, and am trying to get the index view to show up. I've tried the code specified on the page verbatim but keep on getting errors.
polls/urls.py:
from django.conf.urls import patterns, urls
from polls import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
)
mysite/urls.py:
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
url(r'^polls/', include('polls.urls')),
url(r'^admin/', include(admin.site.urls)),
)
and finally, the index method in views.py:
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
return HttpResponse("<h1>Hello world!</h1>");
I'm not sure what I'm doing wrong. I keep getting an error that says "cannot import name 'urls'." any help would be appreciated!
The problem is in your import statement - there is no urls function in django.conf.urls package.
Replace:
from django.conf.urls import patterns, urls
with:
from django.conf.urls import patterns, url

Django urlpattern "didn't match"

I have the following code in my urls.py:
urlpatterns = patterns('',
(r'^news/', include('news.urls')),
)
When I try to open
http://localhost/news
or
http://localhost/news/
in the browser django shows me 404 page:
Using the URLconf defined in python.urls, Django tried these URL patterns, in this order:
^news/
The current URL, , didn't match any of these.
UPD:
news/urls.py:
from django.conf.urls.defaults import *
from django.views.generic.simple import direct_to_template
urlpatterns = patterns('news.views',
(r'^$', 'news'),
)
There is views.py in news directory and it contains news function.
And news module is added to INSTALLED_APPS.
Why it cannot find news pattern? Any suggestions?
Looks like you forgot the url() function:
Try this instead:
from django.conf.urls import url
urlpatterns = patterns('',
url(r'^news/', include('news.urls')),
)
and news/urls.py:
from django.conf.urls.defaults import *
from django.views.generic.simple import direct_to_template
from django.conf.urls import url
urlpatterns = patterns('news.views',
url(r'^$', 'news'),
)
In which port your application launches? When I see your tries http://locahost/news it might be that you forgot to use the right port.
Try to visit http://localhost:8000/ and http://localhost:8000/news.
I think it may be a problem with your views.py this url on the django docs tells you how to dispatch the urls properley
https://docs.djangoproject.com/en/dev/topics/http/urls/

Categories

Resources