// archives

Tips

This category contains 9 posts

WYSIWYG editor in Django Admin

As awesome as the standard Django admin might be, sometimes you want the textfields to have a little bit more functionality. This is quite simple to achieve. We’ll use WYMeditor in this case, but it should be equally easy with TinyMCE, FCKeditor or any other WYSIWYG editor out there.

First download jQuery and put it in [...]

Django foreign key default value example

Here’s an example of how to set a default value for a models.ForeignKey field:
class Foo(models.Model):
    a = models.CharField(max_length=10)
def get_foo():
    return Foo.objects.get(id=1)
class Bar(models.Model):
    b = models.CharField(max_length=10)
    a = models.ForeignKey(Foo, default=get_foo)
Hope this helps someone (I’ve been looking for this quite some time…). If anyone know a better/neater way, please let us know in the comments!!

GeoDjango: getting a random point within a multipolygon

While fooling around with the awesome GeoDjango framework, I was looking for a way to generate a random point within a multipolygon.
Justin Bronn (djangopeople.net/jbronn/), the lead developer of GeoDjango was kind enough to point me in the right direction. I thought I share the concept here as it might be helpful to someone:
Define the function:

def [...]

More on debugging Django

The other day, I read about another interesting way of debugging Django Apps:
If you use the following statement in you code:
import pdb; pdb.set_trace()
You can debug you app by running the server with the python command
manage.py runserver -p 8888
Then, after you visit the page where the set_trace() function is called, the server will break the code [...]

restrict access to Django site from all IP’s on Webfaction

For this one project I wanted to deny all access to the site except for a certain IP address. I knew it could be done by using the mod_authz_host Apache Module (more info here), but I couldn’t get it to work on a Webfaction shared hosting account.
After exchanging some mails with the Webfaction support team [...]

Django 1.0 mail notification of new comments

There are some old samples available online for doing this, but the 1.0 version of Django is not compatible with those. So I thought I’d share my solution for sending notification mails whenever there’s been posted a new comment on my Django site using Django signals.
The old way of connecting to a signal was:
dispatcher.connect(myFunction, signal=models.signals.post_save, [...]

Awesome Django Cheat sheet

The guys from Mercurytide released an updated version of their superhandy Django Cheat Sheet. Especially useful when you’re just starting with Django.
http://www.mercurytide.co.uk/whitepapers/django-cheat-sheet/

Error using New Comments in Django 1.0

After upgrading to Django 1.0 I got the following error when using the new comment framework in one of my templates:
Caught an exception while rendering: Reverse for ” not found.
After some searching I read somewhere I had to delete the old .pyc files (from before the Django upgrade). After doing that, everything worked as [...]

Tip: Create models.py from existing database

Ever wanted to use an old, existing, legacy database with a Django Project? That’s totally possible!
Django allows you to create a models.py file based on an existing database. To do this, you first have to make sure your database settings are properly specified in the settings.py file. Here’s an example of what the database settings [...]

Categories

  • No categories

Django Hosting:

DjangoDays recommends the following Django hosting provider: