Tag Archives: Django

ProTip for Setting Up Django on OSX

Just got around to provisioning Django and Python on my (relatively) new MacBook Air for a project I’m working on in my spare time.  The fact that it’s months since I got this machine shows you just how much spare time I have.  That said, I have a confession: I completely suck at deploying things on OS X.  Particularly development things.  I can do this well on Linux environments, but for some reason I just have a really hard time getting Django running on a clean OS X environment.Generally speaking, here’s how it goes:

  1. Install the latest Django.
  2. Clone my project from Github.  Follow their excellent documentation to get my keys setup, etc.
  3. Run “python manage.py runserver”.
  4. Remember my project requires the Python Imaging Library (PIL).
  5. Download and start the install of the PIL.  Am told it requires Python 2.5 or greater by the OS X installer.
  6. Download Python 3.2, this doesn’t work.
  7. Download Python 2.7, this doesn’t work.
  8. Download Python 2.6, this doesn’t work.
  9. Download Python 2.5, this works.
  10. Now try to run Django and spend 45 minutes in PYTHONPATH hell.
  11. Give up and just re-run the Django installer, this fixes all my environment and pathing problems, and now it works.
  12. Want to kill everyone because this should be easier.

PROTIP for myself: install Django last.If anyone out there has any other tips for how to make this process easier, I’d love to hear them.

Nasty Config Issue for Django and SQLite 3

Was getting the following errors (alternately, depending on permissions settings):

  • “Unable to Open Database File”
  • “Unable to Write to Database File”

Found a lot of SQLite users complaining about this, then finally got the tip from this page that I needed to set my permissions on the parent director to writeable by the Apache user too.  What a pain.Another note: One nice thing that alleviates some of the normal irritation when moving between dev environments and production is the following code I added into the settings.py file:

import os.pathPROJECT_DIR = os.path.dirname(__file__)DATABASE_NAME = os.path.join(PROJECT_DIR, "database.db")

Enjoy!

How to Access Your Django Models from External Python Scripts

Spent a good portion of time on this over the weekend, and it turned out to be frustrating enough and the answers available incomplete enough for me to want to document this here briefly.I wanted to be able to populate my Django models from an external script by simply calling MyObject.save() .  My particular case was scraping some information off of a website using BeautifulSoup then inserting it into my Django application, where it would show up and be editable from the admin section.  It turned out that I had a major problem with environment variables, so after a lot of reading and some trial and error, the following should work for you.

import osimport syssys.path.append('/path/to/the/directory/above/your/project')os.environ['DJANGO_SETTINGS_MODULE'] = 'yourproject.settings'fromĀ  yourproject.yourapp.models import YourModel

To be a little more clear: f your project is in “/Users/username/myproject”, append “/Users/username” to your system path.  This clears up your environment so that when you set the location of your django settings file using the dot notation, it knows where to look.As always, if you have any questions or comments or a better way to do this, let me know.  I’m using this on Snow Leopard with a trunk SVN checkout of Django.

A Bit of Django Admin Trickery

OK, so I’ve been playing with the latest release of Django (1.1, well technically I’m using the latest SVN release) and am really enjoying the things they’ve added since 1.0.One major irritant I spent most of the morning working through, however, was something I think needs to be attended to, or at least clarified in the documentation.  I’ve got a model “Client” that can have more than one address “Address”, defined as a ForeignKey relationship.  I want to allow editing of the addresses inline for convenience, but kept getting pushback from the GUI when I’d try to just add in a client without an address.Turns out, the Django automatic admin interface detects the presence/absence of fields in the inline widget to decide whether or not to validate the input.  In other words, if you’re putting any value into any of your Address’ fields, it will validate them.  Because I was using a “USStateField()” type on my model, it was always putting in a value for the state, hence it was always failing the validation b/c I wasn’t putting in other fields.Man, very frustrating.  Replacing “USStateField()” with a “models.CharField(max_length=2)” did the trick but I don’t think it was clear at all what was happening.Anyone who has solved this more elegantly, please get in touch.