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.
Related
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/
I am getting a 404 error for a few of my URLs in my django project. For example:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/admin
Using the URLconf defined in some_project.urls, Django tried these URL patterns, in this order:
^$ [name='home']
^admin/$
But I haven't changed anything to the file.
urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
url(r'^$', 'views.index',name='home',),
url(r'^admin/$', include(admin.site.urls)),
)
This problem seemed to come after I installed Django Evolve. Not sure if that has something to do with it.
Your pattern is not correct, it should be:
url(r'^admin/', include(admin.site.urls)),
Your request url is http://127.0.0.1:8000/admin, but your urlpattern is url(r'^admin/$', include(admin.site.urls)).
There is an extra / in the urlpattern.
If you want to include other URLConfs, here's an example to include other URLconfs:
from django.conf.urls import include, patterns, url
urlpatterns = patterns('',
# ... snip ...
url(r'^comments/', include('django.contrib.comments.urls')),
url(r'^community/', include('django_website.aggregator.urls')),
url(r'^contact/', include('django_website.contact.urls')),
# ... snip ...
)
As Burhan correctly pointed out, you need to remove the trailing $ so that include could work.
The reason is because in regex trailing $ matches right after the last character in the string.
It was a problem with the settings file. There was a merge problem with what I had and another developer had.
The /$ wasn't related to this problem. I added it when I was trouble shooting.
I have a question about urls.py in Django. I am building a blog from scratch as a way of learning Django myself. In the main urls.py file, I have specified the include path to my app's urls.py.
from django.conf.urls import patterns, include, url
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^', include('myblog.urls')),
)
In the app (called myblog), the urls.py reads as follows:
from django.conf.urls.defaults import *
from models import blogmodel
from django.contrib import admin
urlpatterns = patterns('',
(r'^login/', include(admin.site.urls)),
(r'^$', include('myblog.views.getLatest')),
)
where getLAtest is the function in my views.py. The error says No module named getLatest
Here's my views.py,
from django.shortcuts import render_to_response
from myblog.models import blogdb
def getLatest(request):
post = blogdb.objects.all()
sorted_post = post.order_by('-served_date')
return render_to_response('blogs.html', {'posts':sorted_post})
Any help is appreciated. Thanks in advance..
You are using the wrong directive; include() is used to include another package; Django will look for a urls.py within the package myblog.views.getLatest when you use that directive.
You want to name the view itself instead:
urlpatterns = patterns('',
(r'^login/', include(admin.site.urls)),
(r'^$', 'myblog.views.getLatest'),
)
Note: no include() is being used.
Try updating this:
urlpatterns = patterns('',
(r'^login/', include(admin.site.urls)),
(r'^$', include('myblog.views.getLatest')),
)
to this:
urlpatterns = patterns('',
(r'^getLatest/$', 'myblog.views.getLatest'),
)
include is meant to read in another urls.py file, where you you are wanting to execute a specific view function.
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.
I have the following code in the urls.py in mysite project.
/mysite/urls.py
from django.conf.urls.defaults import *
urlpatterns = patterns('',
(r'^gallery/$', include('mysite.gallery.urls')),
)
This results in a 404 page when I try to access a url set in gallery/urls.py.
/mysite/gallery/urls.py
from django.conf.urls.defaults import *
urlpatterns = patterns('',
(r'^gallery/browse/$', 'mysite.gallery.views.browse'),
(r'^gallery/photo/$', 'mysite.gallery.views.photo'),
)
404 error
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^gallery/$
The current URL, gallery/browse/, didn't match any of these.
Also, the site is hosted on a media temple (dv) server and using mod_wsgi
Remove the $ from the regex of main urls.py
urlpatterns = patterns('',
(r'^gallery/', include('mysite.gallery.urls')),
)
You don't need gallery in the included Urlconf.
urlpatterns = patterns('',
(r'^browse/$', 'mysite.gallery.views.browse'),
(r'^photo/$', 'mysite.gallery.views.photo'),
)
Read the django docs for more information
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.