Interpreting Django Traceback - django

I'm getting the following message. I somewhat understand that my project server cannot find the templates. However, I have no idea exactly what it's complaining about the templates. Would be great to hear alternate explanations of what's happening.
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/polls/1/
Django Version: 1.4
Python Version: 2.7.3
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'polls',
'django.contrib.admin')
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')
Template Loader Error:
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
/home/stanley/mytemplates/polls/detail.html, polls/poll_detail.html (File does not exist)
Using loader django.template.loaders.app_directories.Loader:
/usr/local/lib/python2.7/dist-packages/django/contrib/auth/templates/polls/detail.html, polls/poll_detail.html (File does not exist)
/usr/local/lib/python2.7/dist-packages/django/contrib/admin/templates/polls/detail.html, polls/poll_detail.html (File does not exist)
Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
136. response = response.render()
File "/usr/local/lib/python2.7/dist-packages/django/template/response.py" in render
104. self._set_content(self.rendered_content)
File "/usr/local/lib/python2.7/dist-packages/django/template/response.py" in rendered_content
79. template = self.resolve_template(self.template_name)
File "/usr/local/lib/python2.7/dist-packages/django/template/response.py" in resolve_template
55. return loader.select_template(template)
File "/usr/local/lib/python2.7/dist-packages/django/template/loader.py" in select_template
193. raise TemplateDoesNotExist(', '.join(not_found))
Exception Type: TemplateDoesNotExist at /polls/1/
Exception Value: polls/detail.html, polls/poll_detail.html

I'll try to answer this in a bit more depth, especially to react to your statement "I think i got the templates in the wrong place." This can be easily solved using the traceback you posted.
First thing it tells you is some stuff about your environment - you can simply ignore this. This is not usefull for a TemplateLoaderError. It can be important though if any of your apps requires a special version of Python or something the like.
Next thing to spot is what kind of error has ocurred, and this one is extremely helpfull for your special case:
Template Loader Error:
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
/home/stanley/mytemplates/polls/detail.html, polls/poll_detail.html (File does not exist)
If you take a closer look at this one, it tells you exactly where Django tried to find the specified templates, and what happened while it was looking for them (File does not exist). This will help you to find a solution to check if you really got your templates in the wrong place (which will happen quite often, even to a bit more experienced Django developers).
The next part is the traceback. You can savely ignore it in your case, because there is no file involved that is located in your project folder. If it was, that would mean you possibly screwed up something in the named file, and Django even tells you in which line your code did (possibly) provoke the exception.
Last part is information about the exception that has been raised, in your case TemplateDoesNotExist. Django has some special exceptions that you can find here . Other than these Django raises the standard Python exceptions.
Hope i didn't tell you too much you allready knew.

Do you have something like this in your settings?
PROJECT_ROOT = path.dirname(path.abspath(__file__)) #gets directory settings is in
TEMPLATE_DIRS = (
PROJECT_ROOT+'/templates',
)

You might have specified some path in your TEMPLATE_DIRS tuple in settings.py file.Django searches the html templates only in those directories.So add the following path to your TEMPLATE_DIRS.
TEMPLATE_DIRS = ('/polls',)

Related

Image don't shown in Django LFS

I've added image to product, but it not shown in product preview. Error message appears: "The request content cannot be loaded. Please try again later".
Web-page is located in localhost, DB is in UTF8_general_ci (MySQL), Django 1.8, Python 2.7.
Also when I try to open an attachement (i've put image there), I recieve an error, traceback il below that post:
Environment:
Request Method: GET
Request URL: http://localhost:8000/media/files/book1.png
Django Version: 1.8
Python Version: 2.7.6
Installed Applications:
('lfs_theme',
'compressor',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.staticfiles',
'django.contrib.sites',
'django.contrib.flatpages',
'django.contrib.redirects',
'django.contrib.sitemaps',
'django_countries',
'pagination',
'reviews',
'portlets',
'lfs.addresses',
'lfs.caching',
'lfs.cart',
'lfs.catalog',
'lfs.checkout',
'lfs.core',
'lfs.criteria',
'lfs.customer',
'lfs.customer_tax',
'lfs.discounts',
'lfs.export',
'lfs.gross_price',
'lfs.mail',
'lfs.manage',
'lfs.marketing',
'lfs.manufacturer',
'lfs.net_price',
'lfs.order',
'lfs.page',
'lfs.payment',
'lfs.portlet',
'lfs.search',
'lfs.shipping',
'lfs.supplier',
'lfs.tax',
'lfs.tests',
'lfs.utils',
'lfs.voucher',
'lfs_contact',
'lfs_order_numbers',
'localflavor',
'postal',
'paypal.standard.ipn',
'paypal.standard.pdt')
Installed Middleware:
('django.middleware.csrf.CsrfViewMiddleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.redirects.middleware.RedirectFallbackMiddleware',
'pagination.middleware.PaginationMiddleware')
Traceback:
File "/home/stp/Рабочий стол/lfs-installer/eggs/Django-1.8-py2.7.egg/django/core/handlers/base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/stp/Рабочий стол/lfs-installer/eggs/Django-1.8-py2.7.egg/django/views/static.py" in serve
54. fullpath = os.path.join(document_root, newpath)
File "/usr/lib/python2.7/posixpath.py" in join
80. path += '/' + b
Exception Type: UnicodeDecodeError at /media/files/book1.png
Exception Value: 'ascii' codec can't decode byte 0xd0 in position 10: ordinal not in range(128)
Your problem is that media root path contains non-ASCII characters, such as "Рабочий стол".
Posible solutions:
Move your project to ASCII-only (non-Cyrillic in your case) path
Use Python3 instead 2, which not have this Unicode problems
Change MEDIA_ROOT setting to Unicode string, e.g. u'/home/stp/Рабочий стол/myproject/media/'
To get image and override ANSII encoding I've done following:
Added string # -*- coding: utf-8 -*- at the beginning of the
settings.py in my LFS;
As Mr. Tikhonov said, changed MEDIA_ROOT string
After that images started to load normally.

Django middleware: Isn't a middleware module error

I am using the middleware provided in https://gist.github.com/426829 to do cross site scripting.
However, when I add the middleware to MIDDLEWARE_CLASSES, I get the error:
ImproperlyConfigured: isn't a middleware module.
My MIDDLEWARE_CLASSES looks like this:
MIDDLEWARE_CLASSES = ('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'TempMiddleware',)
I have not changed any code in the gist. process_request and process_response methods are there. I am on Ubuntu running the latest versions of Python and Django.
What's TempMiddleware? The name of the module, or the name of the class? As you can see with the other entries, you need the fully-qualified Python path of the actual class. If TempMiddleware is the name of the module, you need TempMiddleware.MyMiddlewareClass (and you should really follow PEP8 naming conventions) - and if it's the name of the class, you need my_module.TempMiddleware.
Edit:
TempMiddleware is not importable. It's the name of the class, you must put the entire import path.
E.g:
'django.contrib.auth.middleware.AuthenticationMiddleware'
and not
'AuthenticationMiddleware'
So if your class is in app_name/middleware.py, it should be
app_name.middlaware.TempMiddleware
It just mean that in your settings file, the variable MIDDLEWARE_CLASSES contains a list of modules in which one of the listed module is not a middleware.
Possible causes:
you added a middleware that doesn't declare middleware methods: fix that by removing the last middleware you added
you added a correct middleware but forget to put a coma at the end of the name, so strings are concatenated and it make django thinks 2 middlewares are in fact one: fix that by adding the missing coma

Django - urlpatterns cannot find a module

I'm new to Django and from ASP.NET MVC. I just want to make a folder structure something below, though, if I navigate to http://localhost:8000/community_web/, that gives me an error saying "Could not import community_web.controllers.home. Error was: No module named controllers.home".
Folder structure what I want.
<project>
urls.py
<community_web>
urls.py
<controllers>
home.py
I've added the following codes.
To project.urls.py
urlpatterns = patterns('',
(r'^community_web/', include('community_web.urls')),
)
To project.community_web.urls.py
urlpatterns = patterns('',
(r'^$', 'community_web.controllers.home.index'),
)
I thought a views.py would correspond to a controller in ASP.NET MVC term and so I don't want to put all request handlers into one file. If I moved the home.py to its parent folder, it works fine, but without a hierarchical folder structure, creating lots of files in one folder is not my preference. How can I achieve this? or maybe not a good practice in Django?
Thanks in advance,
Yoo
Try popping a __init__.py into the directory controllers. This will make the directory a module python can see.
It is typically not good practice to do it that way. But do what you feel comfortable with. Under controllers do you have a init py file? If not it might not be able to find the home.py file.
Can you post the contents of home.py? There might be something in there that is causing it not load correctly.
Have you put 'community_web' into your installed apps section of your settings.py?
http://docs.djangoproject.com/en/dev/ref/settings/#installed-apps
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.admin',
'community_web',
)

django_cms ImproperlyConfigured: Error importing middleware cms.middleware.media

I'm moving an application that uses django_cms from one server, where everything worked, to another and have spent the last 4 hours trying to find the cause of this error. A suggestions very welcome!
mod_wsgi (pid=21972): Exception occurred within WSGI script '/var/www/vhosts/compdoctest.com/django/compdoc/django.wsgi'.
Traceback (most recent call last):
File "/usr/lib/python2.5/site-packages/django/core/handlers/wsgi.py", line 230, in __call__
self.load_middleware()
File "/usr/lib/python2.5/site-packages/django/core/handlers/base.py", line 42, in load_middleware
raise exceptions.ImproperlyConfigured('Error importing middleware %s: "%s"' % (mw_module, e))
ImproperlyConfigured: Error importing middleware cms.middleware.media: "No module named media"
The offending line is the last one in the middleware list in settings.py
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'cms.middleware.page.CurrentPageMiddleware',
'cms.middleware.user.CurrentUserMiddleware',
'cms.middleware.multilingual.MultilingualURLMiddleware',
'cms.middleware.media.PlaceholderMediaMiddleware',
)
If I remove the final line then the code continues and falls over later saying that item is required in middleware.
I'm using slightly different version of django, 1.2.1 final on the old working server and 1.2.3 final on the new server.
All the things I've tried:
The same version of django_cms - 2.1.0 beta 3 - that was used on the old server
The latest version on github - 2.1.0.beta2-550 Putting the cms, mptt, menus, publisher folders in the
app From python importing the
googled (nobody having the same problem that I can find)
middleware file directly (no problem)
result of opening in python:
python manage.py shell
Python 2.5.2 (r252:60911, Jan 20 2010, 23:14:04)
[GCC 4.2.4 (Ubuntu 4.2.4-1ubuntu3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import cms.middleware.media
>>> cms.middleware.media.__file__
'/var/www/vhosts/compdoctest.com/django/compdoc/cms/middleware/media.pyc'
>>> from django.forms.widgets import Media
>>> import cms.middleware.media
>>>
Thanks to my friend Bartosz Ptaszynski for pointing me in the right direction on this one. It was a path problem. Added this to the top of the settings.py file and it all magically started working.
sys.path.insert(0, '/path_to_app/app/')
And as he pointed out:
Exception occurred within WSGI script means that the path while running under the web server was wrong it's a completely different environment than the manage.py shell
I had the same problem. But since it only occurs when using mod_wsgi, another solution is adding the path in the apache config (rather than editing the syspath inside setting.py):
# mod_wsgi settings
WSGIDaemonProcess name user=user group=group python-path=/app_path/app/:/app_path/lib/python2.6/site-packages/:/app_path/
WSGIProcessGroup polykum
Including the site packages in the path is also given in the example of Jon Black.

non-consistent django error

I'm running django on Dreamhost right now with fastcgi, and I'm getting really weird behavior. First, the server runs Python 2.3. On my computer, I'm running 2.6 and all my source code works. When I put it on my host though, nothing works. Right now, when I pkill python and then load a page, the first error complains about a syntax error at 'class Item_list()' line:
from dtms.models import *
class Item_list():
def __init__(self, list = None, house_id = None):
self.list = list
self.house_id = house_id
def ret_list(self):
return self.list
Then, if I reload it again without changing anything, I get this error:
Django Version: 1.1 alpha 1 SVN-10114
Python Version: 2.3.5
Installed Applications:
['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.admin',
'mysite.dtms']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware')
Traceback:
File "/home/victor/django/django_src/django/core/handlers/base.py" in get_response
82. callback, callback_args, callback_kwargs = resolver.resolve(
File "/home/victor/django/django_src/django/core/urlresolvers.py" in resolve
184. for pattern in self.url_patterns:
File "/home/victor/django/django_src/django/core/urlresolvers.py" in _get_url_patterns
212. raise ImproperlyConfigured("The included urlconf %s doesn't have any"
Exception Type: ImproperlyConfigured at /dtms/login/
Exception Value: The included urlconf mysite.urls doesn't have anypatterns in it
Any ideas?
class Item_list():
You can't include an empty inheritance list in Python 2.3. There seems to have been a change in the grammar that allows it now but not then.
It would normally be written:
class Item_list:
if you don't want any base classes. But generally these days you'd want to derive from object to get new-style classes.
I don't know much about your deployment environment, but in general when you've tried to import something and got an exception, it can leave behind partially-initialised modules in sys.modules that will frustrate future attempts to import them, resulting in otherwise inexplicable errors where properties and actions of the module are not where they were expected.
In general once an import has failed you should consider the environment lost and start again, but I don't know how your Django deployment copes with errors like that and process-restarting issues. Maybe the original error has left an interpreter running without having written the expected stuff to url_patterns, or something.