Entries tagged “django”

Plus Addressing

One of my biggest pet peeves is registration forms that don’t accept “plus” addresses, e.g. myname+whatever@mysite.com.

Read full post
Naming things and a recursion

Most Mozilla webdev projects have an awful project structure, and it’s partially my fault. I’m attemtping to fix that, but I cringe every time someone creates a new playdoh (Mozilla’s Django template) based project.

Read full post
Choosing a New Web Stack

I like building web sites, a lot. Usually every few years I need to re- evaluate the stack I use for a side-project. Joshua’s Stack Parts site is handy for this.

Read full post
DjangoCon Testing Tutorial

DjangoCon 2011

Read full post
How we slug at Mozilla

One problem we find with slug generators, is they do an awful job with unicode. For a string like this: Bän...g (bang) you get something like bng---g--bang- or at best bang-bang. But it’s 2011, urls can have unicode… here’s what we really want: bäng-bang.

Read full post
Testing Redis in Django

Read full post
Retrieving elements in a specific order in Django and mySQL

If you have a list of ordered ids and you want to turn them into an ordered result set you can use FIELD() in mysql:

SELECT * FROM addons
ORDER BY FIELD(id, 3, 5, 9, 1);

This is a handy trick if you use an external search engine which gives you an ordered list of ids and you want to pull out entire row sets.

We do this in addons.mozilla.org using the Django ORM like so:

The code in action.

Read full post
DjangoCon wrapup

I went to DjangoCon this past week for work. Django is one of my favorite frameworks. I dropped PHP and the symfony framework to learn python and Django and I haven’t looked back. I think for Mozilla’s webdev team it would be the framework of choice. We have 100s of sites in many frameworks, but not a lot of resuability. Django apps are built to built to be reusable. If you build correctly you don’t have to refactor, it’s already done.

Read full post
Resolving Django dumpdata errors

Recently I recieved this wonderful piece of news when I ran ./manage.py dumpdata for the first time:

Error: Unable to serialize database: User matching query does not exist.

I knew this might not work out since I was dealing with a legacy database, but the resolution is quite simple. First I had to narrow it down to which app was causing this. Naturally I assumed it was one of the two apps I had, either common or restaurant. So I ran: ./manage.py dumpdata common and ./manage.py dumpdata restaurant. The latter had no problem whatsoever.

This made sense, since my common application was the only one that made any reference to a User. By looking in my models.py for that application, I narrowed it down to my Profile object. Sure enough, commenting it out meant I could get my data.

It ended up being a foreign key mismatch between the profile and user tables. Since this is legacy data, this mismatch made sense. A simple SELECT id,userid FROM profile WHERE userid NOT IN (SELECT id FROM auth_user) gave me a list of bad profiles. Removing them allowed me to create my Django fixtures.

Read full post
Resizing Image on upload in Django

I had trouble wrapping my head around Django ORM’s handling of Images.

Read full post
Database versus files for Images

This is a dead topic for sure, but one of the bad web development habits I had picked up was that storing everything in a database made things easier.

Read full post
Versioning Django Models

In symfony, versioning a model was not terribly difficult. I had my own specialized brute-force way of doing this.

Read full post
Mimic propel's update_at and created_at in Django models

One “trick” that propel offers you is tables with fields created_at and updated_at.

Read full post
Django models: saving markdown

I love markdown. I write my blogs in markdown. For most user text areas in my web apps I support markdown and save both the markdown and the formatted text into my data store.

Read full post
Custom error messages for Django forms

For some reason, it was difficult for me to find the documentation for this. If your Django form field is required you’ll by default get an error stating that ‘This field is required.’ You can easily replace that when defining your form like so:

Read full post
sfGuardUser -> django.contrib.auth

If you find yourself moving from symfony to Django, here’s how you sf_guard_user’s user table to django.contrib.auth user table:

Read full post
Django Admin and Cookies

I was dusting off an old Django project and everything was working except the admin site:

Read full post
Why code rewrites are often coupled with redesigns

I’ve done some rewrites of code, and they usually are coupled with redesigns.

Read full post
nginx proxying to apache

I gave up on fastcgi with NginX and django. Too many things just didn’t work, so I decided to keep Apache, but lock it down and thrown NginX in the front to serve static content and to prevent max client issues.

Read full post
Unicode or How to deal with f'd up text in Django

So I’ve been going out of my mind trying to figure out why something like:

Read full post
Enabling a debug.css in django

I like for extra magical stuff to occur when I am in development/debug mode. One of those magical things is a a magic debug.css style sheet.

Read full post
the magic of django: get_callable

Read full post
sfGuardUser to django.contrib.auth.models.User

Let’s pretend that your assignment was to convert a symfony app that used sfGuardAuth to a django-based system. Wouldn’t it be great if someone just gave you a bunch of SQL that you could use to convert the symfony sf_guard_user table to a django auth_user table?

Read full post
Django circular model references

I’m used to circular references in my model. Often I do a versioning of an Item with an ItemVersion. Item will link to the latest ItemVersion and ItemVersion will link to the relevant Item.

Here’s how you can define your appropriate django models:

Note in the first model, Item, we reference ItemVersion in quotes because ItemVersion is not yet defined.

Read full post
Why Django Templating is awesome and why I get smarty again

I get Smarty thanks to django… yeah, it’s weird.

Back to my original comment about templating, smarty really is trying to limit the scope of PHP in a good way. Too often I see a lot of heavy-lifting in the templates. It’s so bad it makes my MVC’d brain explode.

Django templates are very limited, based on a philosophy of keeping view logic and only view logic in the templates.

This is what smart tries to do and it’s a reasonable solution to the fact that PHP is a templating language with a kitchen sink. It’s saying, okay… well let’s treat PHP as a programming language, and keep Smarty as the template.

symfony of course says (well not really, or in any official capacity, but would you believe… frameworks talk to me), some PHP is view and some is model/controller. We’ll suggest which ones are which, but really we’re not getting in the way of making your life a living hell by sticking complicated business logic inline.

Read full post
Girl Geek Dinner

Katie and I went to Girl Geek Dinner. The best thing about being a Geek Husband is having a Geek Wife to bring you as a guest.

Read full post
reusability

[tags]django, plugins, apps, projects, symfony[/tags]

Read full post
Templating

[tags]smarty, symfony, php, python, django[/tags]

Read full post
django experiment: day 2 templates, and that's it?

[tags]django, symfony[/tags]

Read full post
python: relative paths

So I started yesterday with Django, and I decided I didn’t want to futz with creating another mysql database that I’d need to manage, etc. Instead I’ll just use sqlite.

I wanted to keep my sqlite database within my project regardless of where I might move my project later. So I did this:

I confused a lot of people on IRC, but it’s really quite easy:

  • __file__ is the filename of the current script, very similar to PHP’s __FILE__
  • os.path.abspath calculates the absolute path, hence the absolute path of the current file
  • os.path.join does all the nasty business of joining paths together and figuring out what type of slashes are needed, etc.
  • ‘data/db.sqlite’ is a string

So really all we were doing is creating a relative path, but setting it absolutely.

Read full post
GeoDjango

[tags]geodjango, django, gis[/tags]

Read full post
django experiment: Day 1 and Model Inheritence

[tags]django, inheritance[/tags]

Read full post