I recently upgraded my Django project to version 1.9.
When I try to run migrate, I am getting the following two errors:
Support for string view arguments to url() is deprecated and will be removed in Django 1.10 (got app.views.about). Pass the callable instead.
django.conf.urls.patterns() is deprecated and will be removed in Django 1.10. Update your urlpatterns to be a list of django.conf.urls.url() instances instead.
Could someone please show me the proper syntax of how to do this? A brief sample of my urls.py is below:
urlpatterns = patterns('',
url(r'^about/$', 'app.views.about',
name='about'),
)
urlpatterns += patterns('accounts.views',
url(r'^signin/$', 'auth_login',
name='login'),
)
Thank you!
Import your views directly, or your views modules:
from apps.views import about
from accounts import views as account_views
Do not use patterns at all, just use a list or tuple:
urlpatterns = [
url(r'^about/$', about,
name='about'),
]
urlpatterns += [
url(r'^signin/$', account_views.auth_login,
name='login'),
]
You should remove the quotes around views name.
So your code will be like that
urlpatterns = patterns('',
url(r'^about/$', app.views.about, #without quote!
name='about'),
)
Point 2, use lists, so your code will transform to
urlpatterns = [
url(r'^about/$', app.views.about, #without quote!
name='about'),
]
Related
I'm trying to learn Django and I'm following Corey Shafer's tutorials (https://www.youtube.com/watch?v=a48xeeo5Vnk), but when I try to make two different pages, I get automatically directed to the one with an "empty address":
In his:
/Blog
/urls.py
it looks like this:
from django.conf.urls import path
from . import views
urlpatterns = [
path('', views.home, name='blog-home'),
path('about/', views.about, name='blog-about'),
]
and when he goes to localhost:8000/blog/about, the page displays correctly
When I try to imitate his code for blog/urls.py:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'', views.home, name='blog-home'),
url(r'^about/', views.about, name='blog-about'),
]
the result of the localhost:8000/blog/about is the content of views.home, and not views.about.
The following works correctly, when I write a name instead of an empty string:
urlpatterns = [
url(r'^home', views.home, name='blog-home'),
url(r'^about/', views.about, name='blog-about'),
]
But I don't understand why it worked in a previous version, why it won't work now, and what could fix it
A url matches if it can find a substring that matches, the empty string r'' thus matches every string.
You should make use of anchors to specify the start (^) and end ($) of the string:
urlpatterns = [
# ↓ ↓ anchors
url(r'^/$', views.home, name='blog-home'),
url(r'^about/', views.about, name='blog-about'),
]
Note: As of django-3.1, url(…) [Django-doc] is
deprecated in favor of re_path(…) [Django-doc].
Furthermore a new syntax for paths has been introduced with path converters: you
use path(…) [Django-doc] for that.
I always get this error "The empty path didn't match any of these." When I try to access the page through this url:
url('^about/$',views.AboutView.as_view(),name = 'about')
and when I remove "^about/$" part, then it works:
url('',views.AboutView.as_view(),name = 'about')
How could I resolve it?
This is link for call:
<li><a class="navbar-brand" href="{% url 'about'%}">About</a></li>
this is view.py
class AboutView(TemplateView):
template_name = 'about.html'
and, this urlpatterns
urlpatterns = [
url('^about/$',views.AboutView.as_view(),name = 'about')
]
from django.conf.urls import url
from blog import views
urlpatterns = [ url('about',views.AboutView.as_view(),name='about') ]
instead of this
from django.urls import path
from blog import views
urlpatterns = [
path('about/', views.AboutView.as_view(),name='about'),
use this pattern same as your main url
path('about/', views.AboutView, name='about'),
It's not good to follow 2 ways of creating urls, Since django==2.0 they have introduced very nice and easy way to declare urls.
In the old way...
from django.conf.urls import url
urlpatterns = [
url(r'^about/$', AboutView.as_view(), name="about")
]
But In the new way it's lot more cleaner...
from django.urls import path
urlpatterns = [
path('about/', view.AboutView.as_view())
]
But if you want to stick with the regular expressions, Use re_path() instead of path().
urlpatterns = [
re_path(r'about/$', view.AboutView.as_view())
]
In my it's better stay with one pattern, old or new but not both. It makes your code look more cleaner.
From django.conf.urls import url
urlpatterns = [
'chat.views',
url(r'^$', 'about'),
url(r'^new/$', 'new_room'),
url(r'^(?P<label>[\w-]{,50})/$', 'chat_room'),
]
can any one please help me
try below code
from chat import views
from django.conf.urls import url
urlpatterns = [
url(r'^$', views.about, name='about'),
url(r'^new/$', views.new_room, name='new_room'),
url(r'^(?P<label>[\w-]{,50})/$', views.chat_room, name='chat_room'),
]
You should not use strings in the view arguments. Try this instead:
from chat.views import about, new_room, chat_room
from django.conf.urls import url
urlpatterns = [
url(r'^$', about),
url(r'^new/$', new_room),
url(r'^(?P<label>[\w-]{,50})/$', chat_room),
]
I would like to translate URL prefix and also URL slug using django-modeltranslation where slug is saved inside database table. After switching the language i would like to stay on the same page and just change the language. I'm using form language switcher as described here:
http://docs.djangoproject.com/en/dev/topics/i18n/translation/#the-set-language-redirect-view
Problem is that the language is just switching on homepage. The other pages are just refreshed without language and URL change.
Is there any way how can i get current url in other language?
In root project urls.py i have following:
urlpatterns = patterns('',
# Examples:
(r'^i18n/', include('django.conf.urls.i18n')),
url(r'^$', 'portfolio.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
)
urlpatterns += i18n_patterns('',
url(_(r'^projects/'), include('projects.urls', namespace='projects')),
)
in app called projects i have urls :
urlpatterns = patterns('',
url(r'^$', all_projects, name='projects'),
url(r'^(?P<slug>[\w-]+)/$', project_detail, name='project_detail'),
)
If this is not a copy-paste-problem you're missing url function name in your main urls.py. Change line 3 of your provided code above to:
urlpatterns = patterns('',
...
# The following line need to be changed from
# (r'^i18n/', include('django.conf.urls.i18n')),
# to
url(r'^i18n/', include('django.conf.urls.i18n')),
...
)
I'm new to django, and one of the things that I'm still learning is url_patterns. I set up a pages app to handle the root path (http://www.mysite.com) as well as some static pages such as the about page. I figured out how to set up the url pattern for the root path, but I can't get the site to direct the path '/about' to the pages "about" view.
Here is my main urls.py
from django.conf.urls import patterns, include, url
from django.conf import settings
urlpatterns = patterns('',
url(r'^polls/', include('polls.urls')),
url(r'^$', 'pages.views.root'),
url(r'^/', include('pages.urls')),
)
here is my pages urls.py
from django.conf.urls import patterns, include, url
urlpatterns = patterns('pages.views',
url(r'^about', 'about'),
)
Here is my pages views.py
# Create your views here.
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.http import HttpResponse, HttpResponseRedirect
from django.core.urlresolvers import reverse
def root(request):
return render_to_response('pages/root.html',context_instance=RequestContext(request))
def about(request):
return render_to_response('pages/about.html',context_instance=RequestContext(request))
If I change the main urls.py file to have r'^a/', include('pages.urls') then the path '/a/about' directs to the about action.. so I think it has to be an issue in the way i'm writing the url pattern in this file. But, I can't figure out how to change it. Can anyone help?
Figured out what the issue is. The proper url_pattern on the project level is:
urlpatterns = patterns('',
url(r'^polls/', include('polls.urls')),
url(r'^$', 'pages.views.root'),
url(r'', include('pages.urls')),
)
When this is in place, '/about' and other simple paths direct properly.
Thanks everyone!
Try this, for url.py on the project level:
urlpatterns = patterns('',
# Examples:
url(r'^$', 'apps_name.views.home', name='home'),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
(r'^about/', include('about.urls')),
)
and then the url.py for the app about
urlpatterns = patterns('',
url(r'^$', direct_to_template, {"template": "about/about.html"}, name="about"),
)
Take into account that regular expression are evaluated from top to bottom, then if the path fits the regexp it will enter. To learn more about regexp google it or try the great book from Zed Shaw about regexps
Note that from Django version 2.0 the URL pattern has changed to use django.urls.path() Check Example here: link
from django.urls import path
from . import views
urlpatterns = [
# ex: /polls/
path('', views.index, name='index'),
# ex: /polls/5/
path('<int:question_id>/', views.detail, name='detail'),
# ex: /polls/5/results/
path('<int:question_id>/results/', views.results, name='results'),
# ex: /polls/5/vote/
path('<int:question_id>/vote/', views.vote, name='vote'),
]
About the url method:
url(r'^$', 'pages.views.root')
url is deprecated in Django 3.1, it's good to use re_path instead.
https://docs.djangoproject.com/en/3.1/ref/urls/#s-url
https://docs.djangoproject.com/en/3.1/ref/urls/#re-path
Note: The r'^$'pattern WON'T work with path function, and will give you a misleading error that the route could not be found.
You have to use re_path(r'^$', [etc]) every time you use a regular expression instead of a simple string in pattern.