Code


Version 9 (modified by hugo, 9 years ago) (diff)

fixed broken formatting

NewbieMistakes

Please feel free to share the things that tripped you up when you started with Django. We'll try to improve Django's error handling to catch such mistakes in the future.

URLconf include() misbehaving

Symptom

You're trying to get your URLconf files to work, but getting cryptic errors about how there is no module 'index' (where 'index' is some function you are trying to assign as a view), or module 'foo' has no attribute 'urlpatterns'.

Probable cause

You may have tried to load your view files using include() in the URLconf file (in tutorial 3, this is myproject/settings/urls/main.py). The include() call assumes that the file it's loading is also a URLconf file.

Solution

Remove the include(). Just give the module and function name (e.g., 'myproject.apps.polls.views.polls.index') as a string, with no include() around it.

Blank object names

Symptom

The automatic admin interface is showing nothing (or a single  ) in the "Select [object_type] to change" view.

Probable cause

You may have forgotten to create a __repr__() function for your model. Django calls __repr__() to find out how to display objects in the admin interface.

Solution

Add a __repr__() function to all your models. Make it a habit so it becomes automatic.

Integer & NULLS

Problem

When you have a Field: current_zip = meta.IntegerField(maxlength=5,blank=True)

django will create a not nullable field in the DB. However leaving the field blank (in admin/web) django will try and insert a NULL value in the DB.

Solution

Add null=True:

current_zip = meta.IntegerField(maxlength=5,null=True,blank=True)

Appending to a list in session doesn't work

Problem

If you have a list in your session, append operations don't get saved to the object.

Solution

Copy the list out of the session object, append to it, then copy it back in:

sessionlist = request.session['my_list']
sessionlist.append(new_object)
request.session['my_list'] = sessionlist

Errors about undefined attributes with one-char names

Problem

You get an AttributeError with some weird attribute name that's only one char long. You don't have that attribute name anywhere in your code.

Solution

Search your model and code for situations where you have to pass a tuple of values and want to pass a tuple with one element - and that element is a string like in this sample:

class META:     
    ...
    admin = meta.Admin(
        list_display = ('total_price'),
        ...
    )

It's a standard python error - you are just missing a comma in the list_display assignement like this:

class META:     
    ...
    admin = meta.Admin(
        list_display = ('total_price',),
        ...
    )

Since a tuple is expected but a string provided, the code will merrily iterate over the characters of the string instead of the tuple elements - and that's where the single-char attribute names come from.