Django 1.5 released!
Feb. 26th, 2013 12:43 pm![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
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!
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!
Django 1.4 released!
Mar. 23rd, 2012 11:47 am![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
I am really excited about this release. Currently my two favorite features are *args and **kwargs support for template tag helper functions (and an "assignment tag") and list filters in admin interface
Tip of my day: db.reset_queries()
Feb. 28th, 2012 10:04 am![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
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.
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.
Scripts for Django and WSGI
Sep. 30th, 2011 10:40 am![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
An improved WSGI script for use with Django. Pretty long and interesting, might lead me to use some better practices.
A simple distinct inner join in Django
Oct. 12th, 2010 01:47 pm![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
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:
You don't even want to know how long it took me to figure this out, people.
ColorProperty.objects.filter(dwthemecolor__isnull = False).distinct()
You don't even want to know how long it took me to figure this out, people.
![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
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! )
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! )
Some useful vim syntax highlighting files
Aug. 16th, 2010 09:54 am![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
This one's for the Python recommended practices.
This one's purportedly for Django templates, though I haven't managed to get it working yet.
Beware CSRF when upgrading to 1.2!
Jun. 11th, 2010 04:15 pm![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
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.
Book recommendation: Pro Django
May. 26th, 2010 10:28 am![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
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.
![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
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! )Full page URL on page template
May. 19th, 2010 02:57 pm![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
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
Then you can use the
<a href="http://www.facebook.com/sharer.php?u={{ link|urlencode }}&t={{ title|urlencode }}">
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 }}">
![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
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.
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:
Tada! You now have a link in your admin's list display.
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.
Django just in case
Aug. 11th, 2009 02:41 pm![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
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!
So welcome!