271👍
Django 1.10 no longer allows you to specify views as a string (e.g. 'myapp.views.home'
) in your URL patterns.
The solution is to update your urls.py
to include the view callable. This means that you have to import the view in your urls.py
. If your URL patterns don’t have names, then now is a good time to add one, because reversing with the dotted python path no longer works.
from django.conf.urls import include, url
from django.contrib.auth.views import login
from myapp.views import home, contact
urlpatterns = [
url(r'^$', home, name='home'),
url(r'^contact/$', contact, name='contact'),
url(r'^login/$', login, name='login'),
]
If there are many views, then importing them individually can be inconvenient. An alternative is to import the views module from your app.
from django.conf.urls import include, url
from django.contrib.auth import views as auth_views
from myapp import views as myapp_views
urlpatterns = [
url(r'^$', myapp_views.home, name='home'),
url(r'^contact/$', myapp_views.contact, name='contact'),
url(r'^login/$', auth_views.login, name='login'),
]
Note that we have used as myapp_views
and as auth_views
, which allows us to import the views.py
from multiple apps without them clashing.
See the Django URL dispatcher docs for more information about urlpatterns
.
3👍
This error just means that myapp.views.home
is not something that can be called, like a function. It is a string in fact. While your solution works in django 1.9, nevertheless it throws a warning saying this will deprecate from version 1.10 onwards, which is exactly what has happened. The previous solution by @Alasdair imports the necessary view functions into the script through either
from myapp import views as myapp_views
or
from myapp.views import home, contact
- [Django]-Django: Display Choice Value
- [Django]-Django – accessing the RequestContext from within a custom filter
- [Django]-Set up a scheduled job?
2👍
You may also get this error if you have a name clash of a view and a module. I’ve got the error when i distribute my view files under views folder, /views/view1.py, /views/view2.py
and imported some model named table.py in view2.py which happened to be a name of a view in view1.py. So naming the view functions as v_table(request,id)
helped.
- [Django]-In django, how do I sort a model on a field and then get the last item?
- [Django]-How to query as GROUP BY in Django?
- [Django]-How to use refresh token to obtain new access token on django-oauth-toolkit?
1👍
Just in case you got the error on your terminal, it is possible that if you stop the server and then run it again, it would work.
On Windows:
ctrl+c
to stop the server
then run the server again:
python manage.py runserver
Cheers.
- [Django]-Django. A good tutorial for Class Based Views
- [Django]-PyCharm: DJANGO_SETTINGS_MODULE is undefined
- [Django]-Why am I getting this error in Django?
0👍
Your code is
urlpatterns = [
url(r'^$', 'myapp.views.home'),
url(r'^contact/$', 'myapp.views.contact'),
url(r'^login/$', 'django.contrib.auth.views.login'),
]
change it to following as you’re importing include()
function :
urlpatterns = [
url(r'^$', views.home),
url(r'^contact/$', views.contact),
url(r'^login/$', views.login),
]
- [Django]-Redirect to Next after login in Django
- [Django]-Do we need to upload virtual env on github too?
- [Django]-How do I integrate Ajax with Django applications?
0👍
change
register = template.Library()
to
registerr = template.Library()
resovled my issue
- [Django]-Django: Display Choice Value
- [Django]-Django proxy model and ForeignKey
- [Django]-How to get the currently logged in user's id in Django?