Django Static File Issue on Deployment - django

Hello i'm trying to deploy a django app which uses the {{ STATIC_URL }} tag.
When I put my code up on EC2 (in debug mode) I get the following error:
Module "django.core.context_processors" does not define a "static" callable
request processor
This doesn't happen on ./manage.py runserver
This also disapears when I remove the django.core.context_processors.static from my template context processors (but then I don't get static media)
Anyone know whats going on?

The static context_processor - and the staticfiles app - were added in version 1.3. It looks like you're running an older version in production.

See also: http://blog.madpython.com/2010/04/07/django-context-processors-best-practice/

I hope you found a solution. But since no one decided to put one here I will, In the event any one new to Django such as myself stumble upon this error, here is my solution. If you carefully read the error assuming you have DEBUG=True you will come to realise that the static definition(pythonic function) is missing from context_processors.py in the django.core module. Therefore head there in your directory. on my server it was /usr/lib/python2.6/dist-packages/django/core. Edit the context_processors.py and add the following function
def static(request):
# this func will static-related context variables to the context
return {'STATIC_URL': settings.STATIC_URL}

Related

How can I load a module in a Django template with a variable for the module name?

I want to build some very similar apps in Django, but I want them to load different tag/filter modules. For this I want the html templates to be generic. For example, I would like to have something like
{% load specific_tags %}
and then I would like to define the variable specific_tags in a context processor for each app. But Django thinks that it should look for "specific_tags" in the installed apps in settings.py. How can I tell Django/Python to first evaluate the content of the variable from the context processor?
Best regards
Why don't you simply put this values in setting files, this is the way how I am differentiating between dev, test, demo and production environments, and the use one of the solution found here to load that value sin your template:
Can I access constants in settings.py from templates in Django?

Django doesn't read from database – no error

I just set up the environment for an existing Django project, on a new Mac. I know for certain there is nothing wrong with the code itself (just cloned the repo), but for some reason, Django can't seem to retrieve data from the database.
I know the correct tables and data is in the db.
I know the codebase is as it should be.
I can make queries using the Django shell.
Django doesn't throw any errors despite the data missing on the web page.
I realize that it's hard to debug this without further information, but I would really appreciate a finger pointing me to the right direction. I can't seem to find any useful logs.
EDIT:
I just realized the problem lies elsewhere. Unfortunately I can't delete this post with the bounty still open.
Without seeing any code, I can only suggest some general advice that might help you debug your problem. Please add a link to your repository if you can or some snippets of your database settings, the view which includes the database queries etc...
Debugging the view
The first thing I would recommend is using the python debugger inside the view which queries the database. If you've not used pdb before, it's a life saver which allows you to set breakpoints in your Python script and then interactively execute code inside the interpreter
>>> import pdb
>>> pdb.set_trace()
>>> # look at the results of your queries
If you are using the Django ORM, the QuerySet returned from the query should have all the data you expect.
If it doesn't then you need to look into your database configuration in settings.py.
If it does, then you must might not be returning that object to the template? Unlikely as you said the code was the same, but double check the objects you pass with your HttpResponse object.
Debugging the database settings
If you can query the database using the project settings inside settings.py from the django shell it sounds unlikley that there is a problem with this - but like everything double check.
You said that you've set up a new project on a mac. What is on a different operating system before? Maybe there is a problem with the paths now - to make your project platform independent remember to use the os.path.join() method when working with file paths.
And what about the username and password details....
Debugging the template
Maybe your template is referencing the wrong object variable name or object attribute.You mentioned that
Django doesn't throw any errors despite the data missing on the web
page.
This doesn't really tell us much - to quote the Django docs -
If you use a variable that doesn’t exist, the template system will
insert the value of the TEMPLATE_STRING_IF_INVALID setting, which is
set to '' (the empty string) by default.
So to check all the variables available to your template, you could use the debug template tag
{{ debug }}
Probably even better though is to use the django-debugging-toolbar - this will also let you examine the SQL queries your view is making.
Missing Modules
I would expect this to raise an exception if this were the problem, but have you checked that you have the psycopg module on your new machine?

Migrating a django app from 1.3.1 to 1.5

We are considering migrating our dated django 1.3.1 app to the latest version (1.5.4).
I don't like migrations.
Are any specific problems to be thought of? Any show stoppers you may think of?
Of course I will study release notes and all this.
Will it be a real head ache or is it doable?
We have migrated our app from 1.4 to 1.5, we didn't fetch any issue which is blocker. We had needed to done couple of changes related to adminmedia, json library uses and url syntaxing in our html templates. They were some normal issues only.
But yes as you said its always better to check release notes before upgrading to latest version.
Hope this will help you.
Here are the few things you need to take care of:
Project directory structure has been changed.
Include ALLOWED_HOSTS in settings.py.
django.utils.simplejson is deprecated. import simplejson can be used instead.
Session data will not be saved, when the response code is 500.
django.forms.ModelMultipleChoiceField now returns an empty QuerySet as the empty value instead of an empty list.
Uploaded files are no longer created as executable by default. If you need them to be executable change FILE_UPLOAD_PERMISSIONS to your needs.
{% load adminmedia %} template tag can no longer be used.
If you’re using django.contrib.redirects, make sure INSTALLED_APPS contains django.contrib.sites.

How to add app to all pages in django?

I have an app called lastapp that I would like it to be viewable on all pages. It renders base.html so it's available on all apps. So far I've tried adding it to the urls.py like this:
urlpatterns = patterns('',
(r'^first/$', firstapp, lastapp),
(r'^last/$', lastapp),
)
But when I go to /first/ it gives me the error: Error, 'function' not iterable. It shows fine when going to /last/. If I remove lastapp from the /first/ site in urls.py, then firstapp shows fine also. Is there a special way to do this? Thanks in advance.
What exactly are you trying to do?
All installed applications are available in your project, but the application by it self does not return any data to the template, that's the job of the views which are part of that application.
My guess is that firstapp and lastapp are rather views then apps.
If that is a case, context processors make it possible to always pass certain data to a template.
So, create a context processor out of lastapp and set it in TEMPLATE_CONTEXT_PROCESSORS in settings.py.
I think what you need is probably Middleware.

Django url tag gives wrong values

My URLconf has:
url(r'^view-item$', 'appname.views.view_item', name='view-item'),
Now, if I go to http://myhost/path_to_django_app/view-item/, it works. However, {% url view-item %} returns '/view-item/'. Why is it doing this?
This problem occurred when I moved the application to a new server, so I'm guessing something must be configured wrong, but I don't even know where to look.
This may be due to the SCRIPT_NAME variable not being set correctly. The url tag will use this variable to compose the final absolute path to return.
You should check what request.META['SCRIPT_NAME'] is set to in one of your views. If it is not set correctly, then you may need to look into your backend configuration. If you are using mod_python, this usually involves making sure that django.root is set in the apache config. Check the installation docs for more info.
If you still can't get it to work, you can try adding this to settings.py:
FORCE_SCRIPT_NAME = '/path_to_django_app/'
The usual way to write your URl in django is with a trailing '/':
url(r'^view-item/$', 'appname.views.view_item', name='view-item')
or:
url(r'^view-item/', include('view.urls')),
Life will be much easier if you follow this convention.