Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass callable in Django 1.9

Hi i am new in Python and Django and I follow the django workshop guide. I just installed Python 3.5 and Django 1.9 and get a lot of error messages ... Just now I found a lot of dokumentations but now stuck. I want to add views and and so I added following code in urls.py:

from django.conf.urls import include, url

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = [
    # Uncomment the admin/doc line below to enable admin documentation:
    #url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
    url(r'^admin/', include(admin.site.urls)),
    url(r'^rezept/(?P<slug>[-\w]+)/$', 'recipes.views.detail'),
    url(r'^$', 'recipes.views.index'),
]

and every time get the error message:

Support for string view arguments to url() is deprecated and will be removed in Django 1.10 (got recipes.views.index). Pass the callable instead.
  url(r'^$', 'recipes.views.index'),

But I couldn't find how to pass them. The documentations only tell "pass them" but no example how to...

like image 573
Pompi Avatar asked Dec 08 '15 14:12

Pompi


1 Answers

This is a deprecation warning, which means the code would still run for now. But to address this, just change

url(r'^$', 'recipes.views.index'),

to this:

#First of all explicitly import the view
from recipes import views as recipes_views #this is to avoid conflicts with other view imports

and in the URL patterns,

url(r'^rezept/(?P<slug>[-\w]+)/$', recipes_views.detail),
url(r'^$', recipes_views.index),

More documentation and the reasoning can be found here

In the modern era, we have updated the tutorial to instead recommend importing your views module and referencing your view functions (or classes) directly. This has a number of advantages, all deriving from the fact that we are using normal Python in place of “Django String Magic”: the errors when you mistype a view name are less obscure, IDEs can help with autocompletion of view names, etc.

like image 102
karthikr Avatar answered Oct 11 '22 05:10

karthikr