Fix Python – Running a specific test case in Django when your app has a tests directory

The Django documentation (http://docs.djangoproject.com/en/1.3/topics/testing/#running-tests) says that you can run individual test cases by specifying them:
$ ./manage.py test animals.AnimalTestCase

This assumes that you have your tests in a tests.py file in your Django application. If this is true, then this command works like expected.
I have ….

Fix Python – Django TemplateDoesNotExist?

My local machine is running Python 2.5 and Nginx on Ubuntu 8.10, with Django builded from latest development trunk.
For every URL I request, it throws:

TemplateDoesNotExist at /appname/path appname/template_name.html
Django tried loading these templates, in this order:
* Using loader django.template.loaders.filesystem.function:
* Using loader….

Fix Python – Automatic creation date for Django model form objects

What’s the best way to set a creation date for an object automatically, and also a field that will record when the object was last updated?
models.py:
created_at = models.DateTimeField(False, True, editable=False)
updated_at = models.DateTimeField(True, True, editable=False)

views.py:
if request.method == ‘POST’:
form = MyForm(request.POST)
….

Fix Python – Execute code when Django starts ONCE only?

I’m writing a Django Middleware class that I want to execute only once at startup, to initialise some other arbritary code. I’ve followed the very nice solution posted by sdolan here, but the “Hello” message is output to the terminal twice. E.g.
from django.core.exceptions import MiddlewareNotUsed
from django.conf import settings

class StartupMid….

Fix Python – django test app error – Got an error creating the test database: permission denied to create database

When I try to test any app with command (I noticed it when I tried to deploy myproject using fabric, which uses this command):
python manage.py test appname

I get this error:
Creating test database for alias ‘default’…
Got an error creating the test database: permission denied to create database

Type ‘yes’ if you would like to try deleting the….

Fix Python – django MultiValueDictKeyError error, how do I deal with it

I’m trying to save a object to my database, but it’s throwing a MultiValueDictKeyError error.
The problems lies within the form, the is_private is represented by a checkbox. If the check box is NOT selected, obviously nothing is passed. This is where the error gets chucked.
How do I properly deal with this exception, and catch it?
The line is
is_….

Fix Python – How to do a less than or equal to filter in Django queryset?

I am attempting to filter users by a custom field in each users profile called profile. This field is called level and is an integer between 0-3.
If I filter using equals, I get a list of users with the chosen level as expected:
user_list = User.objects.filter(userprofile__level = 0)

When I try to filter using less than:
user_list = User.objects…..

Fix Python – Django – makemigrations – No changes detected

I was trying to create migrations within an existing app using the makemigrations command but it outputs “No changes detected”.
Usually I create new apps using the startapp command but did not use it for this app when I created it.
After debugging, I found that it is not creating migration because the migrations package/folder is missing from an a….