Django: When a foreign key links to a huge list of data

Imagine the following model for your Django application:

class Address(models.Model):
    ...

class Person(models.Model):
    address = models.ForeignKey(Address)
    ...

The Django admin inteface for Person will show a DropDownList with many many items. For the user this is hardly usable since it will be quite cumbersome to pick the correct entry from that list when you want to change a person’s address. If you even want to edit the person’s address you have to change to the Address model admin view, search the item and edit it there. Not nice.

The solution is hidden very nicely in the Django documentation about raw_id_fields. Just add the following code to your model admin:

class ContactPersonAdmin(model.ModelAdmin):
    model = ContactPerson
    raw_id_fields = ("address",)

This will result in the following:

Now you can click the icon and search for the address you need.

  • Share/Bookmark

Tags:

django-cms imports module that is never referenced

This one just drove me nuts so I will post it here as personal note.

If you are running a django-cms powered site where some pages have application_urls hooked into the page you have to be careful when removing the hooked up application. I for instance was using the wonderful django-contact_form application for quite a while but recently I created my own app so that contact_form was no longer needed. After removing the app from my INSTALLED_APPS and from the project folder I got the following error when sending any request:

TemplateSyntaxError at /
Caught an exception while rendering: No module named urls_apply

Thankfully I used a specially named urls.py – if not the error message would have been totally confusing since the stack trace doesn’t give any hints on where the module wants to be imported.

To solve this issue you have to delete all references to that urls.py from the table cms_title (column application_urls).

  • Share/Bookmark

Tags: , , ,

Invalid template name in ‘extends’ tag: ”. Got this from the ‘template’ variable.

When writing a django-application that should be hooked into a django-cms page you might stumble upon this error message:

TemplateSyntaxError at /bewerben/
Invalid template name in 'extends' tag: ''. Got this from the 'template' variable.

In order to fix this you need to pass your context to the template in your view-method:

return render_to_response(
        'yourtemplate.html',
        {'form': form,},
        context_instance=RequestContext(request),
    )
  • Share/Bookmark

Tags: , ,

Speak after me: pubsubhubbub

I read about this tongue twister a lot already and of course it sounds like a super cool new development on the internet. So: Time to activate PubSubHubbub support at my feedburner account for this blog.

For more information see:
http://code.google.com/p/pubsubhubbub/
http://adsenseforfeeds.blogspot.com/2009/07/whats-all-hubbub-about-pubsubhubbub.html

  • Share/Bookmark

Tags: ,

Extending the for loop in Django templates

Django comes with an excellent templating language which has many built-in template tags and filters. Although it is very powerful already there are a few scenarios which cannot be solved, yet. As far as I know the for-loop can only iterate over a list of objects. You are not able to use a C-like for-loop like so:

for(int i=0; i<20; i++)

Luckily Django allows us to extend the templating language with custom filters. I will show you how you can create a classic for-loop for Django templates by implementing three simple steps…
Read the rest of this entry…

  • Share/Bookmark

Tags: , , , ,

Where to host websites powered by Python and Django

As this blog is dedicated to web development powered by Python and Django my first article consequently is going to try to answer the first question you probably ask yourself when planning to create a Django powered Website: “Where can I host my stuff?” Read the rest of this entry…

  • Share/Bookmark

Tags: , ,

We are Twitter, Resistance is Futile!

On last Wednesday evening I was looking forward to attend the PyCologne Python user group meeting for the first time together with a colleague. “Twitter with Python” was announced as one of the topics which sounded pretty interesting to me. Unfortunately the meeting was canceled due to not enough attendees. Guess how we came to know the bad news? Right! The information hit us through the big hive mind called Twitter.

Read the rest of this entry…

  • Share/Bookmark

Tags: , ,

Hello world!

Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!

Read the rest of this entry…

  • Share/Bookmark

Tags: , , , ,