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

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.

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

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

Django/Python ImportError: No module named admin

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.

Django import error in views - no module named 'x'

I keep getting an error that there's no such module.
The project name is gmblnew, and I have two subfolders- core and gmblnew - the app I'm working on is core.
My urls.py file is
from django.conf.urls import *
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'gmblnew.views.home', name='home'),
# url(r'^gmblnew/', include('gmblnew.foo.urls')),
url(r'^league/', include('core.views.league')),
# 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)),
)
This seems to be fine. The views.py file is:
from django.http import HttpResponse
def league(request):
from core.models import Division
response = HttpResponse()
response['mimetype'] = 'text/plain'
response.write("<HTML><>BODY>\n")
response.write("< TABLE BORDER=1><CAPTION>League List</CAPTION><TR>\n")
all_leagues = Division.objects.all()
for league in all_leagues:
response.write("<TR>\n")
response.write("<TD> %s" % league)
response.write("</TD>\n")
response.write("</BODY></HTML>")
return response
Traceback:
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
103. resolver_match = resolver.resolve(request.path_info)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/urlresolvers.py" in resolve
319. for pattern in self.url_patterns:
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/urlresolvers.py" in url_patterns
347. patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/urlresolvers.py" in urlconf_module
342. self._urlconf_module = import_module(self.urlconf_name)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/importlib.py" in import_module
35. __import__(name)
File "/Users/chris/Dropbox/Django/gmblnew/gmblnew/urls.py" in <module>
12. url(r'^league/', include('core.views.league')),
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/conf/urls/__init__.py" in include
25. urlconf_module = import_module(urlconf_module)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/importlib.py" in import_module
35. __import__(name)
Exception Type: ImportError at /admin/
Exception Value: No module named league
I've tried a number of variants on the url(r'^league/', include('core.views.league')), line, including gmblnew.core.views.league, views.league, views.view_league, etc. I'm obviously missing something super simple on the structure of that line.
Your problem is here:
url(r'^league/', include('core.views.league')),
By using include you are specifying a module, which does not exist.
include is used to include other url confs, and not to target view methods
What you want is refer to the view method league
url(r'^league/$', 'core.views.league'),
should work.
Also, note the $ after ^league/ , which represents the end of the URL pattern.
include takes a path to a url file, not a view. Just write this instead:
url(r'^league/', 'core.views.league'),

Categories

Resources