foxfirefey: A guy looking ridiculous by doing a fashionable posing with a mouse, slinging the cord over his shoulders. (geek)
[personal profile] foxfirefey2013-02-26 12:43 pm
Entry tags:

Django 1.5 released!

Woo!

https://www.djangoproject.com/weblog/2013/feb/26/15/

This release includes experimental Python 3 support, configurable user models. Note: the new URL template tag is in full swing now. Any stragglers in your templates that aren't using it will need to convert before upgrading!
foxfirefey: A guy looking ridiculous by doing a fashionable posing with a mouse, slinging the cord over his shoulders. (geek)
[personal profile] foxfirefey2012-02-28 10:04 am
Entry tags:

Tip of my day: db.reset_queries()

Are you writing a script to update tons of stuff in your Django database?

Then you are going to need to from django import db and use db.reset_queries() on appropriate occasions to prevent your script from taking up ALL the memories.
foxfirefey: A guy looking ridiculous by doing a fashionable posing with a mouse, slinging the cord over his shoulders. (geek)
[personal profile] foxfirefey2011-09-30 10:40 am
Entry tags:

Scripts for Django and WSGI

An improved WSGI script for use with Django. Pretty long and interesting, might lead me to use some better practices.
aithine: (SQL users)
[personal profile] aithine2011-06-11 04:54 pm

Question about search plugins

Has anyone here used Haystack? What did you like or dislike about it? What search engine backend did you choose? Why?

(Why yes, I do have a potential major RL project that needs to have a serious search function, why do you ask? *vbg*)
foxfirefey: A guy looking ridiculous by doing a fashionable posing with a mouse, slinging the cord over his shoulders. (geek)
[personal profile] foxfirefey2010-10-12 01:47 pm

A simple distinct inner join in Django

Let's say I have ColorProperty and DWThemeColor (which is the through of a ManyToMany between DWTheme and ColorProperty). And I want to get all distinct ColorProperties that belong to DWThemes. This is how:

ColorProperty.objects.filter(dwthemecolor__isnull = False).distinct()

You don't even want to know how long it took me to figure this out, people.
foxfirefey: A guy looking ridiculous by doing a fashionable posing with a mouse, slinging the cord over his shoulders. (geek)
[personal profile] foxfirefey2010-10-04 11:53 am

Make a sortable ordering item list with jQuery and jQuery UI

Here's an example of making an admin page that lets people reorder the items through drag and drop. It's *fairly* genericized already--you can use the same template for multiple data types--and has the potential to be MORE generic by creating a generic view sort of thing, but I haven't done that yet.

All you need is a model that has a (Integer or PositiveInteger) field named 'order'.

The form will let you reorder the items by drag and drop and save them, and also lets you link to the usual form to add an item.

This way to the code! )
foxfirefey: A guy looking ridiculous by doing a fashionable posing with a mouse, slinging the cord over his shoulders. (geek)
[personal profile] foxfirefey2010-07-06 04:06 pm
Entry tags:

Treemenus

Treemenus is "a simple and generic tree-structured menuing system for Django with an easy-to-use admin interface."
foxfirefey: Smiley faces are born through factorized mechanical torture. (grimace)
[personal profile] foxfirefey2010-06-11 04:15 pm

Beware CSRF when upgrading to 1.2!

This one caught me due to inattentiveness--if you're upgrading to 1.2, be sure to read the notes on CSRF. There's some changes that are really important if you're using forms. I think it's great that they're making it easier to keep your site secure, but beware, you might end up with 403 errors if you're not careful.
foxfirefey: A guy looking ridiculous by doing a fashionable posing with a mouse, slinging the cord over his shoulders. (geek)
[personal profile] foxfirefey2010-05-26 10:28 am
Entry tags:

Book recommendation: Pro Django

I can't say I was actually the target audience for Pro Django: I didn't have enough of a basis in either Django or Python. However, I love this book. It taught me more about Python metaprogramming than anything else, due to its use it Django internals, and has given me a bunch of tools to use in Django development. I haven't used many of the Django tips yet, I admit, but knowing them gave me a better understanding of the system as a whole, and I've definitely used Python techniques I've learned here in other projects.
foxfirefey: Fox stealing an egg. (Default)
[personal profile] foxfirefey2010-05-19 04:39 pm
Entry tags:

Example of a filter that adds HTML to a string

This is a filter that turned out to be not quite as useful as I thought because some of the phrases our graphic designer wanted more than one word bolded, but I could totally change around the argument of this filter to take in a number of words to enclose, instead of making the class the optional argument. But, I thought it made a good basic example, so I figured I'd post it. It's useful if you want to visually emphasize the first word of a string, since there's no CSS pseudoselector for that.

On to the example code! )
aithine: (Default)
[personal profile] aithine2010-05-19 02:57 pm

Full page URL on page template

Say you need the full URL of the current page you're displaying that you need to use on the template of that page. (For example, to add links to Facebook, Delicious, etc., and you don't want to have additional JavaScript that you didn't write on your pages. Shush, yes, I'm a control freak. :P) Also say that you don't want to use the sites framework to hack it together. The permalink decorator won't work, either, because that gives you the relative link.

Thankfully, there's a new request object that's been added to 1.0 that will do it.

To use it, include the bolded bit in your views.py file, as part of each view where you want to have the variable available:

return render_to_response('dir/template.html', {'object': obj, 'link': request.build_absolute_uri()}, RequestContext(request))

Then you can use the link variable on the template where you need the URL:

<a href="http://www.facebook.com/sharer.php?u={{ link|urlencode }}&t={{ title|urlencode }}">
foxfirefey: Fox stealing an egg. (Default)
[personal profile] foxfirefey2010-04-28 02:38 pm
Entry tags:

Making a "non-built-in" link in the admin list view

This was something I found out how to do recently and got inspired to share! It's about how to add a link that's not a built in one (like the link to edit the model) to the list view in the admin. This can be useful in some cases.

First, in your model definition, add a function that returns the link that you want.

def external_link(self):
        """Returns a link for display in the admin table."""
        if self.article_link:
            return u'Link' % self.article_link
        else:
            return None
    external_link.allow_tags = True 
    external_link.short_description = "Link"


Adding the allow_tags attribute to this function lets the admin know it can display the raw HTML. The short_description attribute is optional and lets you specify what you want the column's title to be; if you don't have it, it'll be the function's name with underscores replaced by spaces.

Then, you can add it to the list display:

class NewsItemAdmin(admin.ModelAdmin):
    list_display =  ('title', 'date_published', 'external_link')


Tada! You now have a link in your admin's list display.
foxfirefey: Fox stealing an egg. (Default)
[personal profile] foxfirefey2009-08-11 02:41 pm

Django just in case

I don't know how many other people on Dreamwidth do development with Django, or want to, but I made this community for that just in case, because I do.

So welcome!