Django urlpattern "didn't match" - python

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/

Related

Page not found at /polls

I am a total beginner in "django" so I'm following some tutorials currently I' am watching https://youtu.be/JT80XhYJdBw Clever Programmer's tutorial which he follows django tutorial
Everything was cool until making a polls url
Code of views.py:
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
HttpResponse("Hello World.You're at the polls index")
Code of polls\urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
Code of Mypr\urls.py:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/',admin.site.urls),
path('polls/',include('polls.urls')),
]
I don't get it I did the same thing but I'm getting error not only polls.In one turtorial he decided to make blog,and again the same error:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/polls/
Using the URLconf defined in Mypr.urls, Django tried these URL patterns, in
this order:
admin/
The current path, polls/, didn't match any of these.
Please my seniors help me.
Note:I'm using the latest versions of django,windows and as editor I'm using Pycharm.
Already tried(and did not work):
from django.urls import path
from polls.views import index
urlpatterns = [
path('', index, name='index'),
]
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/polls/
Using the URLconf defined in Mypr.urls, Django tried these URL patterns, in this order:
admin/
The current path, polls/, 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.
try to access polls/ URL in your browser then you will access the page
Because you have accessed the URL of your project, you have to go to this URL to access your app
try by changing your code like this
from django.urls import path
from polls.views import index
urlpatterns = [
path('', index, name='index'),
]
I was facing the same error. Feel kinda dumb after figuring out how to solve this.
Your urls.py content is all right
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
But it must be included in the mysite\mysite\urls.py and not in mysite\urls.py.
That means the inner mysite/ directory is the actual Python package for your project. Its name is the Python package name you’ll need to use to import anything inside it.

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.

Django: Page Not Found

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

Django: Simplest way to create a 2-page site?

I have a site made in PHP that i need to create a django site of. I've stripped out the PHP code temporary (not much code anyways), but i'm having problems understanding how django works and how to create a simple template to display a page.
I know there's thousands of books and guides out there, but most of them go too deep or doesn't do what i need. I just need two simple pages, page1 and page2, which will be accessed through domain.com/page1 and domain.com/page2.
What is the simplest way to achieve that?
This is what i have in my urls.py file so far, is that correct at least?
from django.conf.urls.defaults import patterns, include, url
urlpatterns = patterns('',
url(r'^$', 'mysite.views.page1', name='home'),
url(r'^$page2', 'mysite.views.page2', name='page2'),
)
It obviously doesn't work now cause the views aren't created.
Any help is greatly appreciated,
// qwerty
I recommend you to walk through tutorial, you will find out everything Django beginner should know.
Try this:
from django.conf.urls.defaults import patterns, include, url
from mysite.yourapp import views
urlpatterns = patterns('',
url(r'^$', 'mysite.views.page1', name='home'),
url(r'^page2/$', 'mysite.views.page2', name='page2'),
)
The r'^page1/$ bit is python regex
in your views.py file define your views:
def page1:
#something
This should help you get started http://docs.djangoproject.com/en/1.3/intro/tutorial01/
Well, i found the most straight-forward way is:
1) urls.py
from django.urls import include, path
from . import views
from django.views.generic import TemplateView
urlpatterns = [
path('test.html', TemplateView.as_view(template_name='main/test.html')),
]
2) templates/test.html
Hello world!
Classic 3-steps:
1) urls.py
from django.urls import include, path
from . import views
urlpatterns = [
path('test.html', views.test, name='test'),]
2) views.py
from django.shortcuts import render
def test(request):
return render(request, 'test.html')
3) templates/test.html
Hello world!
Thats more or less what I use:
urlpatterns = patterns('',
(r'^$', 'news.views.page1'),
(r'^page2/$', 'news.views.page2'),
)
just for your understanding: the beginning of a line is expressed as ^, the end as $. So ^$ stands for an empty line. more about regexp: http://docs.python.org/library/re.html
If you are using older django than direct_to_template generic view is what you need.
from django.views.generic.simple import direct_to_template
urlpatterns = patterns('',
(r'^page1/$', direct_to_template, {'template': 'page1.html'}),
(r'^page2/$', direct_to_template, {'template': 'page2.html'}),
)
Or for newer django 1.3 you need to use class based generic views
from django.conf.urls.defaults import *
from django.views.generic import TemplateView
urlpatterns = patterns('',
(r'^page1/', TemplateView.as_view(template_name="page1.html")),
(r'^page2/', TemplateView.as_view(template_name="page2.html")),
)
P.S. Don't forget to create page1.html and page2.html template files.

Django failing to route url (simple question)

I'm doing something stupid, and I'm not sure what it is. I have a the following urls.py in the root of my django project:
from django.conf.urls.defaults import *
from django.conf import settings
urlpatterns = patterns('',
(r'^$', include('preview_signup.urls')),
)
In my preview_signup module (django app) I have the following urls.py file:
from django.conf.urls.defaults import *
urlpatterns = patterns('django.views.generic.simple',
(r'^thanks/$', 'direct_to_template', {'template': 'thankyou.html'})
)
The urls.py above doesn't work when I go to http://localhost:8000/thanks/. But if it's changed to this:
from django.conf.urls.defaults import *
urlpatterns = patterns('django.views.generic.simple',
(r'^$', 'direct_to_template', {'template': 'thankyou.html'})
)
And I go to http://localhost:8000/ it works fine.
What am I doing wrong?
This code should work:
urlpatterns = patterns('',
(r'^', include('preview_signup.urls')),
)
$ (end of line) just removed.
When something goes wrong (or even if it doesn't), thoroughly read the django docs. Here's an excerpt from the aforementioned link:
from django.conf.urls.defaults import *
urlpatterns = patterns('',
(r'^weblog/', include('django_website.apps.blog.urls.blog')),
(r'^documentation/', include('django_website.apps.docs.urls.docs')),
(r'^comments/', include('django.contrib.comments.urls')),
)
Note that the regular expressions in
this example don't have a $
(end-of-string match character) but do
include a trailing slash. Whenever
Django encounters include(), it chops
off whatever part of the URL matched
up to that point and sends the
remaining string to the included
URLconf for further processing.

Categories

Resources