I'm working through the official Django tutorial and adapting it slightly for my own needs using Django version 1.6.1, Python 2.7.6.
I'm at the point where it has me mapping URLs but I keep getting "No module named customers.urls" errors when there is very clearly a module with an aptly named file within, so I'm really at a loss as to what I'm doing wrong.
My initial thought was that I needed to import something customers-related in the root/urls.py but every combination of import resulted in roughly the same error, and the tutorial did not say to do this.
ROOT_URLCONF = 'taco.urls' (taco is the name of the project)
I'm running this using manage.py/runserver so there's no special web server trickery going on that I'm aware of. I've restarted it several times.
The apps are all properly registered, as the traceback can attest.
Any pointers as to something I'm overlooking would be appreciated!
root/urls.py:
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^customers/', include('customers.urls')),
url(r'^admin/', include(admin.site.urls)),
)
customers/urls.py:
from django.conf.urls import patterns, url;
from customers import views;
urlpatterns = ('',
url(r'^$', views.index, name='index')
);
customers/views.py:
from django.shortcuts import render
from django.http import HttpResponse;
def index(request):
return HttpResponse("Hello");
Traceback
Environment:
Request Method: GET
Request URL: http://192.168.3.208:8000/customers/
Django Version: 1.6.1
Python Version: 2.7.6
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'south',
'taco.customers',
'taco.inventory',
'taco.lookups',
'taco.orders')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')
Traceback:
File "/usr/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
99. resolver_match = resolver.resolve(request.path_info)
File "/usr/lib/python2.7/dist-packages/django/core/urlresolvers.py" in resolve
337. for pattern in self.url_patterns:
File "/usr/lib/python2.7/dist-packages/django/core/urlresolvers.py" in url_patterns
365. patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/usr/lib/python2.7/dist-packages/django/core/urlresolvers.py" in urlconf_module
360. self._urlconf_module = import_module(self.urlconf_name)
File "/usr/lib/python2.7/dist-packages/django/utils/importlib.py" in import_module
40. __import__(name)
File "/var/project/taco/taco/urls.py" in <module>
7. url(r'^customers/', include('customers.urls')),
File "/usr/lib/python2.7/dist-packages/django/conf/urls/__init__.py" in include
26. urlconf_module = import_module(urlconf_module)
File "/usr/lib/python2.7/dist-packages/django/utils/importlib.py" in import_module
40. __import__(name)
Exception Type: ImportError at /customers/
Exception Value: No module named customers.urls
In your customers/urls.py:
Change this:
urlpatterns = ('',
url(r'^$', views.index, name='index')
);
For this:
urlpatterns = patterns('',
url(r'^$', views.index, name='index')
);
Also, make sure you have your __init__.py file in package customers. And that INSTALLED_APPS is correctly filled with you app name.
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'south',
'customers',
'inventory',
'lookups',
'orders',
)
If taco is the name of the project check that apps are being referenced correctly so in your installed apps you may need the following:
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'south',
'customers',
'inventory',
'lookups',
'orders')
Related
My Geocode application which is based on Django is no longer serving properly, and I am getting the error detailed as follows:
Environment:
Request Method: GET
Request URL: http://52.36.3.179/
Django Version: 1.6.11
Python Version: 2.7.6
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.admin',
'django.contrib.sitemaps',
'django.contrib.staticfiles',
'django.contrib.messages',
'django.contrib.humanize',
'django.contrib.gis',
'pagination',
'taggit',
'friendlytagloader',
'geoexplorer',
'leaflet',
'django_extensions',
'autocomplete_light',
'mptt',
'modeltranslation',
'djcelery',
'pinax_theme_bootstrap_account',
'pinax_theme_bootstrap',
'django_forms_bootstrap',
'account',
'avatar',
'dialogos',
'agon_ratings',
'announcements',
'actstream',
'user_messages',
'tastypie',
'polymorphic',
'guardian',
'geonode.people',
'geonode.base',
'geonode.layers',
'geonode.maps',
'geonode.proxy',
'geonode.security',
'geonode.social',
'geonode.catalogue',
'geonode.documents',
'geonode.api',
'geonode.groups',
'geonode.services',
'geonode.geoserver',
'geonode.upload',
'geonode.tasks')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.locale.LocaleMiddleware',
'pagination.middleware.PaginationMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')
Traceback:
File "/usr/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
137. response = response.render()
File "/usr/lib/python2.7/dist-packages/django/template/response.py" in render
105. self.content = self.rendered_content
File "/usr/lib/python2.7/dist-packages/django/template/response.py" in rendered_content
81. context = self.resolve_context(self.context_data)
File "/usr/lib/python2.7/dist-packages/django/template/response.py" in resolve_context
159. return RequestContext(self._request, context, current_app=self._current_app)
File "/usr/lib/python2.7/dist-packages/django/template/context.py" in __init__
169. self.update(processor(request))
File "/usr/local/lib/python2.7/dist-packages/geonode/context_processors.py" in resource_urls
28. site = Site.objects.get_current()
File "/usr/lib/python2.7/dist-packages/django/contrib/sites/models.py" in get_current
47. current_site = self.get(pk=sid)
File "/usr/lib/python2.7/dist-packages/django/db/models/manager.py" in get
151. return self.get_queryset().get(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/django/db/models/query.py" in get
310. self.model._meta.object_name)
Exception Type: DoesNotExist at /
Exception Value: Site matching query does not exist.
I can provide more configuration settings if need be. Please don't hesitate to ask. I am trying to not have to reinstall the application. This problem happened after I reloaded the server in order to update the correct URL.
I am trying to do the django 1.8 tutorial, I am on part 3, and I am getting a Exception Value: 'module' object has no attribute 'index' error. It seems like it is not correctly importing the views.py. Any Help? Thanks!
Here is my urls.py:
from django.conf.urls import patterns, url
from polls import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
)
Here is my views.py:
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
return HttpResponse("Hello, World. You're at the polls index")
Here is my error output:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/polls
Django Version: 1.8.3
Python Version: 2.7.6
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'polls')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')
Traceback:
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/core/handlers/base.py" in get_response
108. response = middleware_method(request)
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/middleware/common.py" in process_request
74. if (not urlresolvers.is_valid_path(request.path_info, urlconf) and
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/core/urlresolvers.py" in is_valid_path
647. resolve(path, urlconf)
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/core/urlresolvers.py" in resolve
522. return get_resolver(urlconf).resolve(path)
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/core/urlresolvers.py" in resolve
366. for pattern in self.url_patterns:
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/core/urlresolvers.py" in url_patterns
402. patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/core/urlresolvers.py" in urlconf_module
396. self._urlconf_module = import_module(self.urlconf_name)
File "/usr/lib/python2.7/importlib/__init__.py" in import_module
37. __import__(name)
File "/home/polsen/scripts_i_wrote/python/mysite/mysite/urls.py" in <module>
11. url(r'^polls/', include('polls.urls')),
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/conf/urls/__init__.py" in include
33. urlconf_module = import_module(urlconf_module)
File "/usr/lib/python2.7/importlib/__init__.py" in import_module
37. __import__(name)
File "/home/polsen/scripts_i_wrote/python/mysite/polls/urls.py" in <module>
6. url(r'^$', views.index, name='index'),
Exception Type: AttributeError at /polls
Exception Value: 'module' object has no attribute 'index'
Folder Structure:
mysites/.
./polls
./polls/views.py
./polls/__init__.pyc
./polls/admin.py
./polls/admin.pyc
./polls/urls.py
./polls/models.pyc
./polls/migrations
./polls/migrations/__init__.pyc
./polls/migrations/0001_initial.pyc
./polls/migrations/0001_initial.py
./polls/migrations/__init__.py
./polls/tests.py
./polls/views.pyc
./polls/urls.pyc
./polls/models.py
./polls/__init__.py
./mysite
./mysite/__init__.pyc
./mysite/wsgi.py
./mysite/settings.py
./mysite/urls.py
./mysite/settings.pyc
./mysite/urls.pyc
./mysite/__init__.py
./mysite/wsgi.pyc
./manage.py
If you are following the tutorial closely, you’ll see that in urls.py, your
from poll import views
is in fact
from . import views
Try:
from . import views # relative import
The actual code in the tutorial is:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
Also, in Django 1.8 urlpatterns should be a list of django.conf.urls.url() instances.
As per Django 1.8 docs:
urlpatterns should be a Python list of django.conf.urls.url()
instances.
In Django 1.7, urlpatterns variable used to be a Python list, in the format returned by the function django.conf.urls.patterns().
It's an import error, try:
from polls.views import index
And in the url:
url(r'^$', index, name='index'),
EDIT
Not only for the tutorial:
If urls.py and views.py are at the same level, use:
from . import views
And in url:
url(r'^$', views.index, name='index'),
I'm making a Django website and I downloaded the Tastypie API. But when I try to run my site on (localhost)/articles/api/article, I get this error:
ImportError at /articles/api/article: No module named api
Here is my api.py file:
from tastypie.resources import ModelResource
from tastypie.constants import ALL
from models import Article
class ArticleResource(ModelResource):
class Meta:
queryset = Article.objects.all()
resource_name = 'article'
and my urls.py file:
from django.conf.urls import patterns, include, url
from django.contrib import admin
from api import ArticleResource
article_resource = ArticleResource()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^accounts/login/$', 'django_test.views.login'),
url(r'^accounts/auth/$', 'django_test.views.auth_view'),
url(r'^accounts/loggedin/$', 'django_test.views.loggedin'),
url(r'^accounts/invalid/$', 'django_test.views.invalid_login'),
url(r'^accounts/logout/$', 'django_test.views.logout'),
url(r'^accounts/register/$', 'django_test.views.register_user'),
url(r'^accounts/register_success/$', 'django_test.views.register_success'),
url(r'^articles/all/$', 'article.views.articles'),
url(r'^articles/create/$', 'article.views.create'),
url(r'^articles/get/(?P<article_id>\d+)/$', 'article.views.article'),
url(r'^articles/like/(?P<article_id>\d+)/$', 'article.views.like_article'),
url(r'^articles/add_comment/(?P<article_id>\d+)/$', 'article.views.add_comment'),
url(r'^articles/search/', 'article.views.search_titles'),
url(r'^articles/api/article', include(article_resource.urls)),
)
This is my traceback:
Traceback:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/articles/api/article/
Django Version: 1.7.4
Python Version: 2.7.8
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.comments',
'article',
'tastypie')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
Traceback:
File "/home/deanna/django-deanna/local/lib/python2.7/site-packages/Django-1.7.4-py2.7.egg/django/core/handlers/base.py" in get_response
98. resolver_match = resolver.resolve(request.path_info)
File "/home/deanna/django-deanna/local/lib/python2.7/site-packages/Django-1.7.4-py2.7.egg/django/core/urlresolvers.py" in resolve
343. for pattern in self.url_patterns:
File "/home/deanna/django-deanna/local/lib/python2.7/site-packages/Django-1.7.4-py2.7.egg/django/core/urlresolvers.py" in url_patterns
372. patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/home/deanna/django-deanna/local/lib/python2.7/site-packages/Django-1.7.4-py2.7.egg/django/core/urlresolvers.py" in urlconf_module
366. self._urlconf_module = import_module(self.urlconf_name)
File "/usr/lib/python2.7/importlib/__init__.py" in import_module
37. __import__(name)
File "/home/deanna/django_test/django_test/urls.py" in <module>
3. from api import ArticleResource
Exception Type: ImportError at /articles/api/article/
Exception Value: No module named api
I don't know what I'm doing that's getting this error. I'd appreciate any help I can get. Thank You.
I have a very simple Django application I'm starting. I've run through the tutorial on djangoproject.com and got it working, now I'm building my site. However, I'm having trouble getting my url routing to work. My directory structure is
/dartagnan
--/menu
__init__.py
admin.py
models.py
urls.py
views.py
__init__.py
settings.py
urls.py
My models are working, and I have (had) the admin up and running. I added the menu/urls with
from django.conf.urls import patterns, url
from dartagnan.menu import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index')
)
My installed apps includes bot 'dartagnan' and 'dartagnan.menu'
and I updated the top level urls.py to have
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^menu/', include('menu.urls')),
)
Now when I try to visit, say localhost/menu I get the error:
ImportError at /menu
No module named menu.urls
I've tried 'dartagnan.menu.urls', and a bunch of other variations. I've tried to find a similar question to learn from, but if someone is having the same problem I'm too new at python and django to realize they are the same.
Edit: per request, the error trace
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/menu
Django Version: 1.6.5
Python Version: 2.7.5
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'dartagnan',
'dartagnan.menu')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
Traceback:
File "/Users/Christopher/dartagnan-base-env/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
88. response = middleware_method(request)
File "/Users/Christopher/dartagnan-base-env/lib/python2.7/site-packages/django/middleware/common.py" in process_request
71. if (not urlresolvers.is_valid_path(request.path_info, urlconf) and
File "/Users/Christopher/dartagnan-base-env/lib/python2.7/site-packages/django/core/urlresolvers.py" in is_valid_path
596. resolve(path, urlconf)
File "/Users/Christopher/dartagnan-base-env/lib/python2.7/site-packages/django/core/urlresolvers.py" in resolve
476. return get_resolver(urlconf).resolve(path)
File "/Users/Christopher/dartagnan-base-env/lib/python2.7/site-packages/django/core/urlresolvers.py" in resolve
337. for pattern in self.url_patterns:
File "/Users/Christopher/dartagnan-base-env/lib/python2.7/site-packages/django/core/urlresolvers.py" in url_patterns
365. patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/Users/Christopher/dartagnan-base-env/lib/python2.7/site-packages/django/core/urlresolvers.py" in urlconf_module
360. self._urlconf_module = import_module(self.urlconf_name)
File "/Users/Christopher/dartagnan-base-env/lib/python2.7/site-packages/django/utils/importlib.py" in import_module
40. __import__(name)
File "/Users/Christopher/Sourcecode/dartagnan/dartagnan/urls.py" in <module>
19. url(r'^menu/', include('menu.urls')),
File "/Users/Christopher/dartagnan-base-env/lib/python2.7/site-packages/django/conf/urls/__init__.py" in include
26. urlconf_module = import_module(urlconf_module)
File "/Users/Christopher/dartagnan-base-env/lib/python2.7/site-packages/django/utils/importlib.py" in import_module
40. __import__(name)
Exception Type: ImportError at /menu
Exception Value: No module named menu.urls
Django Attribute Error: 'function' object has no attribute 'is_usable'
I am setting up a Django blog. I followed these instructions and I am getting the following error:
Environment:
Request Method: GET
Request URL: http://url.com/
Django Version: 1.6.2
Python Version: 2.7.3
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'south',
'divinare',
'rest_framework',
'django_facebook',
'django.contrib.auth')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')
Traceback:
File "/home/spencer/webapps/hello_django/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
139. response = response.render()
File "/home/spencer/webapps/hello_django/local/lib/python2.7/site-packages/django/template/response.py" in render
105. self.content = self.rendered_content
File "/home/spencer/webapps/hello_django/local/lib/python2.7/site-packages/django/template/response.py" in rendered_content
80. template = self.resolve_template(self.template_name)
File "/home/spencer/webapps/hello_django/local/lib/python2.7/site-packages/django/template/response.py" in resolve_template
56. return loader.select_template(template)
File "/home/spencer/webapps/hello_django/local/lib/python2.7/site-packages/django/template/loader.py" in select_template
180. return get_template(template_name)
File "/home/spencer/webapps/hello_django/local/lib/python2.7/site-packages/django/template/loader.py" in get_template
138. template, origin = find_template(template_name)
File "/home/spencer/webapps/hello_django/local/lib/python2.7/site-packages/django/template/loader.py" in find_template
121. loader = find_template_loader(loader_name)
File "/home/spencer/webapps/hello_django/local/lib/python2.7/site-packages/django/template/loader.py" in find_template_loader
104. if not func.is_usable:
Exception Type: AttributeError at /
Exception Value: 'function' object has no attribute 'is_usable'
Middleware:
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
#'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
Template Loaders:
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
'django.contrib.auth.context_processors.auth',
# 'django.template.loaders.eggs.Loader',
)
Any insights to what is causing the error would be very helpful.
Your problem is almost certainly that you have "django.contrib.auth.context_processors.auth" in your TEMPLATE_LOADERS setting. As the name implies, that's a context processor, not a template loader - it should go in TEMPLATE_CONTEXT_PROCESSORS.