Django/Python ImportError: No module named admin - python

As a result of attempting to update admin.py of one of my Django apps, I now get a Server Error (500) that has brought my site down. When I import admin from django.contrib, I get the following traceback:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/django/utils/module_loading.py", line 74, in autodiscover_modules
import_module('%s.%s' % (app_config.name, module_to_search))
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
ImportError: No module named admin
Using print_stack() gives me a little deeper insight:
File "/home/ubuntu/gather/src/foodshop/wsgi.py", line 21, in <module>
application = get_wsgi_application()
File "/usr/local/lib/python2.7/dist-packages/django/core/wsgi.py", line 14, in get_wsgi_application
django.setup()
File "/usr/local/lib/python2.7/dist-packages/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/usr/local/lib/python2.7/dist-packages/django/apps/registry.py", line 115, in populate
app_config.ready()
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/apps.py", line 22, in ready
self.module.autodiscover()
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/__init__.py", line 24, in autodiscover
autodiscover_modules('admin', register_to=site)
File "/usr/local/lib/python2.7/dist-packages/django/utils/module_loading.py", line 74, in autodiscover_modules
import_module('%s.%s' % (app_config.name, module_to_search))
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/ubuntu/gather/src/recipes/admin.py", line 6, in <module>
class RecipesAdmin(admin.ModelAdmin):
File "/home/ubuntu/gather/src/recipes/admin.py", line 9, in RecipesAdmin
traceback.print_stack()
I use this module in several other apps within my project, so I don't understand why it's not working for this particular app. You can see my very simple Python code below, which is for an admin.
admin.py
from django.contrib import admin
from .models import Recipes
class RecipesAdmin(admin.ModelAdmin):
list_display = ["__unicode__", "title", "rating", "date_modified"]
prepopulated_fields = {"slug": ("SKU",)}
class Meta:
model = Recipes
admin.site.register(Recipes, RecipesAdmin)
urls.py
from django.conf import settings
from django.conf.urls import include, url
from django.conf.urls.static import static
from django.contrib import admin
from django.conf.urls import patterns, url, include
from djangoratings.views import AddRatingFromModel
urlpatterns = [
url(r'^$', 'customers.views.home', name='home'),
url(r'^home/', 'customers.views.home', name='home'),
url(r'^about/', 'recipes.views.about', name='about'),
url(r'^menu/', 'recipes.views.menu', name='menu'),
url(r'^donate/', 'recipes.views.menu', name='menu'),
# url(r'^contact/', 'recipes.views.contact', name='contact'),
url(r'^admin/', include(admin.site.urls)),
url(r'^about/healthy/', 'recipes.views.healthy', name='healthy'),
url(r'^about/premade/', 'recipes.views.premade', name='premade'),
url(r'^about/exceptional/', 'recipes.views.exceptional', name='exceptional'),
url(r'^accounts/', include('registration.backends.default.urls')),
url(r'^cart/', 'cart.views.get_cart', name='get_cart'),
url(r'^add/', 'recipes.views.menu', name='menu'),
# url(r'^add/(?P<product_id>[-\w]+)/id=(?P<quantity>[-\w]+)/$', 'cart.views.add_to_cart', name='shopping-cart-add'),
url(r'^update/(?P<product_id>[-\w]+)/$', 'cart.views.specific_qty', name='shopping-cart-specify'),
url(r'^remove/(?P<product_id>[-\w]+)/$', 'cart.views.remove_from_cart', name='shopping-cart-remove'),
url(r'^subtract/(?P<product_id>[-\w]+)/$', 'cart.views.subtract_from_cart', name='shopping-cart-subtract'),
url(r'^clear_cart/', 'cart.views.clear_cart', name='shopping-cart-clear'),
url(r'^contact/$','contact.views.email',name='email'),
url(r'^feedback/$','contact.views.feedback',name='feedback'),
url(r'^thanks/$', 'contact.views.thanks',name='thanks'),
url(r'^profile/$', 'cart.views.profile', name='profile'),
url(r'^profile/new/$', 'cart.views.new_profile', name='new_profile'),
url(r'^profile/error/$', 'cart.views.qty_error', name='qty_error'),
url(r'^profile/cancel/$', 'cart.views.cancel_sub', name='cancel_sub'),
url(r'^profile/feedback/$', 'recipes.views.feedback', name='feedback'),
url(r'^rate/(?P<object_id>\d+)/(?P<score>\d+)/', AddRatingFromModel(), {
'app_label': 'recipes',
'model': 'recipes',
'field_name': 'rating',
}),
url(r'^charge/$', 'cart.views.charge', name='charge'),
url(r'^subscription/charge/$', 'cart.views.pay', name='pay'),
url(r'^frequently_asked/$', 'cart.views.frequently_asked', name='frequently_asked'),
url(r'^tender/$', 'cart.views.weekly_orders', name='weekly_orders'),
url(r'^tender_postmates/$', 'cart.views.tender_postmates', name='tender_postmates')
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
I have ensured that I am importing django.contrib.admin in settings.py, and in my urls.py file, I have from django.contrib import admin and url(r'^admin/', include(admin.site.urls)). This just started when I tried changing the admin.py file above; even after reverting back to the original file, the server error occurs. Unfortunately, this has brought my entire site down.
I'm using an Ubuntu machine and an Apache2 server. Any insights would be much appreciated!
Update 1: Even after doing a git reset --hard to origin/master, the error is still occurring.
Update 2: I have removed every .pyc file and restarted my Apache2 server, but the problem persists.
Update 3: In the manage.py shell, when I do >>> from django.contrib import admin as a, >>> a resolves to <module 'django.contrib.admin' from '/usr/local/lib/python2.7/dist-packages/django/contrib/admin/__init__.py'>. This explicitly shows the module exists, but the problem is still occurring.

Related

Python Django | Importing views in urls.py [duplicate]

After upgrading to Django 1.10, I get the error:
TypeError: view must be a callable or a list/tuple in the case of include().
My urls.py is as follows:
from django.conf.urls import include, url
urlpatterns = [
url(r'^$', 'myapp.views.home'),
url(r'^contact/$', 'myapp.views.contact'),
url(r'^login/$', 'django.contrib.auth.views.login'),
]
The full traceback is:
Traceback (most recent call last):
File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 121, in inner_run
self.check(display_num_errors=True)
File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/core/management/base.py", line 385, in check
include_deployment_checks=include_deployment_checks,
File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/core/management/base.py", line 372, in _run_checks
return checks.run_checks(**kwargs)
File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/core/checks/registry.py", line 81, in run_checks
new_errors = check(app_configs=app_configs)
File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/core/checks/urls.py", line 14, in check_url_config
return check_resolver(resolver)
File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/core/checks/urls.py", line 24, in check_resolver
for pattern in resolver.url_patterns:
File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/urls/resolvers.py", line 310, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/urls/resolvers.py", line 303, in urlconf_module
return import_module(self.urlconf_name)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/Users/alasdair/dev/urlproject/urlproject/urls.py", line 28, in <module>
url(r'^$', 'myapp.views.home'),
File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/conf/urls/__init__.py", line 85, in url
raise TypeError('view must be a callable or a list/tuple in the case of include().')
TypeError: view must be a callable or a list/tuple in the case of include().
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.
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
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.
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.
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),
]
change
register = template.Library()
to
registerr = template.Library()
resovled my issue

Django - TypeError: view must be a callable or a list/tuple in the case of include() [duplicate]

After upgrading to Django 1.10, I get the error:
TypeError: view must be a callable or a list/tuple in the case of include().
My urls.py is as follows:
from django.conf.urls import include, url
urlpatterns = [
url(r'^$', 'myapp.views.home'),
url(r'^contact/$', 'myapp.views.contact'),
url(r'^login/$', 'django.contrib.auth.views.login'),
]
The full traceback is:
Traceback (most recent call last):
File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 121, in inner_run
self.check(display_num_errors=True)
File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/core/management/base.py", line 385, in check
include_deployment_checks=include_deployment_checks,
File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/core/management/base.py", line 372, in _run_checks
return checks.run_checks(**kwargs)
File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/core/checks/registry.py", line 81, in run_checks
new_errors = check(app_configs=app_configs)
File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/core/checks/urls.py", line 14, in check_url_config
return check_resolver(resolver)
File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/core/checks/urls.py", line 24, in check_resolver
for pattern in resolver.url_patterns:
File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/urls/resolvers.py", line 310, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/urls/resolvers.py", line 303, in urlconf_module
return import_module(self.urlconf_name)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/Users/alasdair/dev/urlproject/urlproject/urls.py", line 28, in <module>
url(r'^$', 'myapp.views.home'),
File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/conf/urls/__init__.py", line 85, in url
raise TypeError('view must be a callable or a list/tuple in the case of include().')
TypeError: view must be a callable or a list/tuple in the case of include().
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.
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
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.
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.
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),
]
change
register = template.Library()
to
registerr = template.Library()
resovled my issue

Adding a Second URL Pattern in Django Causes Attribute Error [duplicate]

I've been trying to learn Django for the past few days, but recently I've stumbled upon a problem I can't seem to fix. After finishing Django's own tutorial on writing your first app I decided to go through it again. Only now I would replace everything to fit the requirements of the original app I was building.
So, everything went well until I got to part 3. When I try to load http://localhost:8000/lru/ I get the following error message:
AttributeError at /lru/
'module' object has no attribute 'index'
Traceback:
Internal Server Error: /favicon.ico
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py", line 92, in get_response
response = middleware_method(request)
File "/Library/Python/2.7/site-packages/django/middleware/common.py", line 69, in process_request
if (not urlresolvers.is_valid_path(request.path_info, urlconf) and
File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py", line 551, in is_valid_path
resolve(path, urlconf)
File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py", line 440, in resolve
return get_resolver(urlconf).resolve(path)
File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py", line 319, in resolve
for pattern in self.url_patterns:
File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py", line 347, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py", line 342, in urlconf_module
self._urlconf_module = import_module(self.urlconf_name)
File "/Library/Python/2.7/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/Users/oyvindhellenes/Desktop/Sommerjobb 2013/mysite/mysite/urls.py", line 10, in <module>
url(r'^lru/', include('lru.urls', namespace="lru")),
File "/Library/Python/2.7/site-packages/django/conf/urls/__init__.py", line 25, in include
urlconf_module = import_module(urlconf_module)
File "/Library/Python/2.7/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/Users/oyvindhellenes/Desktop/Sommerjobb 2013/mysite/lru/urls.py", line 6, in <module>
url(r'^$', views.index, name='index')
AttributeError: 'module' object has no attribute 'index'
My code:
views.py
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the poll index.")
lru/urls.py
from django.conf.urls import patterns, url
from lru import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index')
)
mysite/urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^polls/', include('polls.urls', namespace="polls")),
url(r'^admin/', include(admin.site.urls)),
url(r'^lru/', include('lru.urls', namespace="lru")),
)
My folder structure looks like this:
mysite/
lru
templates
polls
manage.py
mysite
lru/
templates
urls.py
admin.py
__init__.py
models.py
tests.py
views.py
It's strange because I've done everything exactly as I did in the "polls" example turtorial. Just replacing the names. When I comment out url(r'^lru/', include('lru.urls', namespace="lru")), in mysite/urls.py, then http://localhost:8000/polls/ works fine, but I just can't seem to get /lru to work.
This is really killing me so any form of help would be appreciative!
Edit: Added full traceback
Import the urls.py module into your view. like this;
from django.http import HttpResponse
from . import urls
def index(request):
return HttpResponse("Hello, world. You're at the poll index.")
Either do this :
from lru.views import *
urlpatterns = patterns(
'',
url(r'^$', index, name='index')
)
or
from lru import views
urlpatterns = patterns(
'',
url(r'^$', 'views.index', name='index')
)
I hope this helps.
The second argument of url() should be string, anway I would change the lru/urls.py to:
from django.conf.urls import patterns, url
urlpatterns = patterns('',
url(r'^$', 'lru.views.index', name='lru-index')
)
Hope it helps!
I just found out that Sublime text was mixing .... and tabs to make indentation, depending, I guess, on the copy/paste source.
This is how Django URLs works:
The urls from mysite/mysite/urls includes mysite/polls/urls, which tells that there are polls created inside the mysite project;
The mysite/polls/urls file tells us that the url "polls/", whenever visited, should call the function index() from mysite/polls/views which is what it states when you type the line:
urlpatterns = [
path('', views.index, name = 'index'),
]
And a very important thing to remember is to save each edited file before you run the server, so that files get updated. You can also keep the server running and save the files.

Python Django not running

Firstly I installed Django using pip in my "CentOS" operating system.
After that I performed these steps using terminal.
django-admin startproject mysite
a folder mysite is created with :
manage.py and another subfolder mysite
Then simply I just used these commands :
python manage.py runserver
and the server was running as clicked on the url :
http://localhost:portnumber/
But after that when I made another app to run it is not running, my step are as follows :
python manage.py startapp webapp
Then one new folder created webapp in same mysite directory is created.
After changing settings.py and urls.py of mysite and also changing urls.py and views.py of webapp.
when I run :
python manage.py runserver
error is coming
Performing system checks...
Unhandled exception in thread started by <function wrapper at 0x24a7398>
Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/django/utils/autoreload.py", line
226, in wrapper
fn(*args, **kwargs)
File "/usr/lib/python2.7/site-
packages/django/core/management/commands/runserver.py", line 121, in
inner_run
self.check(display_num_errors=True)
File "/usr/lib/python2.7/site-packages/django/core/management/base.py",
line 374, in check
include_deployment_checks=include_deployment_checks,
File "/usr/lib/python2.7/site-packages/django/core/management/base.py",
line 361, in _run_checks
return checks.run_checks(**kwargs)
File "/usr/lib/python2.7/site-packages/django/core/checks/registry.py",
line 81, in run_checks
new_errors = check(app_configs=app_configs)
File "/usr/lib/python2.7/site-packages/django/core/checks/urls.py", line
14, in check_url_config
return check_resolver(resolver)
File "/usr/lib/python2.7/site-packages/django/core/checks/urls.py", line
28, in check_resolver
warnings.extend(check_resolver(pattern))
File "/usr/lib/python2.7/site-packages/django/core/checks/urls.py", line
24, in check_resolver
for pattern in resolver.url_patterns:
File "/usr/lib/python2.7/site-packages/django/utils/functional.py", line
35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/usr/lib/python2.7/site-packages/django/urls/resolvers.py", line
322, in url_patterns
raise ImproperlyConfigured(msg.format(name=self.urlconf_name))
django.core.exceptions.ImproperlyConfigured: The included URLconf
'<module 'webapp.urls' from
'/home/username/Desktop/prog/django/mysite/webapp/urls.pyc'>' does not
appear to have any patterns in it. If you see valid patterns in the file
then the issue is probably caused by a circular import.
This right here:
'/home/username/Desktop/prog/django/mysite/webapp/urls.pyc'>' does not appear to have any patterns in it.
Check urls.py
I can't tell you exactly what to check without seeing the file, but here is a sample urls.py:
urlpatterns = patterns('',
# Examples:
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^webapp/', include('webapp.urls')),
)
codes are as follows :
urls.py [/mysite/mysite/urls.py]
from django.conf.urls import url,include
from django.contrib import admin
urlpatterns = [url(r'^admin/', admin.site.urls),
url(r'^webapp/', include('webapp.urls'))
]
settings.py[/mysite/mysite/settings.py]
INSTALLED_APPS = [
'webapp',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
urls.py [/mysite/webapp/urls.py]
from django.conf.urls import url
from. import views
urlpatterns = [
url(r'^$', views.index, name='index')
]
views.py [/mysite/webapp/views.py]
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("<h2>Hey!</h2>")

TypeError: view must be a callable or a list/tuple in url of image Django [duplicate]

After upgrading to Django 1.10, I get the error:
TypeError: view must be a callable or a list/tuple in the case of include().
My urls.py is as follows:
from django.conf.urls import include, url
urlpatterns = [
url(r'^$', 'myapp.views.home'),
url(r'^contact/$', 'myapp.views.contact'),
url(r'^login/$', 'django.contrib.auth.views.login'),
]
The full traceback is:
Traceback (most recent call last):
File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 121, in inner_run
self.check(display_num_errors=True)
File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/core/management/base.py", line 385, in check
include_deployment_checks=include_deployment_checks,
File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/core/management/base.py", line 372, in _run_checks
return checks.run_checks(**kwargs)
File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/core/checks/registry.py", line 81, in run_checks
new_errors = check(app_configs=app_configs)
File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/core/checks/urls.py", line 14, in check_url_config
return check_resolver(resolver)
File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/core/checks/urls.py", line 24, in check_resolver
for pattern in resolver.url_patterns:
File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/urls/resolvers.py", line 310, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/urls/resolvers.py", line 303, in urlconf_module
return import_module(self.urlconf_name)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/Users/alasdair/dev/urlproject/urlproject/urls.py", line 28, in <module>
url(r'^$', 'myapp.views.home'),
File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/conf/urls/__init__.py", line 85, in url
raise TypeError('view must be a callable or a list/tuple in the case of include().')
TypeError: view must be a callable or a list/tuple in the case of include().
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.
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
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.
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.
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),
]
change
register = template.Library()
to
registerr = template.Library()
resovled my issue

Categories

Resources