Django community: Community blog posts RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
Building secure Django websites - Erik Romijn
-
Dutch Django meeting - lightning talks
-
Scalable Vector Graphics (SVG) - Jeroen Dijkmeijer
-
30 second apache/nginx/gunicorn timeout
-
Django permissions are untranslatable
-
Django unicodedecodeerror in a models' __unicode__
-
Why Read the Docs matters
Documenting projects is hard, hosting them shouldn't be. Read the Docs was created to make hosting documentation simple. I think that we have solved this problem well, but now we need to start thinking about the larger picture. Along with hosting, Read the Docs was created with 2 other main goals. One was to encourage people to write documentation, by removing the barrier of entry of hosting. The other was to create a central platform for people to find documentation. Having a shared platform for all documentation allows for innovation at the platform level, allowing work to be done once and benefit everyone. Having run the site for over a year now, I think there is a third thing that we should be striving for. That is to make the quality of documentation better. I think that we can help a documentation culture flourish within the open source world. Django is a shining example of what a project with great documentation can do, and it has a community that values docs more than the norm. I think we can help spread this culture throughout the Python world, and beyond. This has already started, and I want to think about how … -
Get decorators wrapping a function
I mentioned a week or so ago about my django templatetag that will only display a menuitem or link if the logged in user has access to the view that it points to. In passing, I stated how it was rather complicated to do this test. The complicated bit is finding out all of the decorators that are wrapping a given function. Fortunately, python is a dynamic language, and this type of introspection, whilst not completely simple, is possible. The key lies in the property that all function objects in python have: `func_closure`. According to the python docs: > `func_closure` is `None` or a tuple of cells that contain binding for the function's free variables Depending upon where you read this, it may or may not be writable. Luckily, we don't need to be able to write to this, only read from it. If `func_closure` is not `None`, then it will be a tuple of ```` objects. To do anything useful, we'll need to look at their `cell_contents` attribute. If that is callable, then it's a good candidate for a decorator. Because of the way decorators work, if you have multiple decorators on a function, each one wraps the next … -
Fakedata i Faketpv
Fa temps que estic fent feina a hores mortes amb un projecte que m'ajudi (que ens ajudi) en el nostre desenvolupament d'aplicacions, en una de les tasques més ingrates: tenir que testejar l'aplicació i afegir dades de prova. Un dels projectes amb que estic jugant és SST un envolcall per Selenium que fa molt senzill fer scripts per testejar webs des del navegador i de manera desatesa. He fet les proves inicials i la veritat és que promet molt el projecte. En aquests moments tenim moltes de les nostres aplicacions monitoritzades fent servir Twill i connectades a Nagios, de manera que podem saber quan l'aplicació cau o no dóna la resposta esperada i que ens avisi mitjançant una alarma de Nagios. SST no està cridat a substituir Twill de moment, però si que ajudarà molt a evitar repeticions de tasques ja que és força més àgil que Selenium, i al meu punt de vista més entenedor. L'altra línia de treball va dirigida a la tasca de generar dades de prova per les aplicacions. Supòs que també us heu vist amb la necessitat de testejar una aplicació en condicions semblants al que ens podem trobar a la realitat, i això vol … -
Different Django views for different HTTP methods
Django's generic views are cool, but the approach of dividing `EditView` from `DetailView` is unforgivable not REST-ful. -
Two-step authentication with Django
A quick post on django-twostepauth, a package I helped develop. This is a package that enables Django to use token devices for user authentication, like for example the free Google Authenticator available for many mobile phones. I've been using the two-step token authentication on my gmail account and that reduced my fear of having my email account cracked. I hope other websites, yes I'm thinking of you home banking, start to allow this kind of protection. If you do not know what two-step authentication is, the video below gives a quick tour of the authentication using the example application shipped with this django package. The package should be easy to integrate with any website. You can download it from the usual place, or clone it from the repository. As always, feedback is very welcome. -
Tweaks for making django admin faster
Here follow a number of tricks I’ve employed in the past to make django admin faster. Editable foreign keys in the changelist If you have foreign keys in list_editable django will make 1 database query for each item in the changelist. Quite a lot for a changelist of 100 items. The trick is to cache the choices for that formfield: class MyAdmin(admin.ModelAdmin): list_editable = 'myfield', def formfield_for_dbfield(self, db_field, **kwargs): request = kwargs['request'] formfield = super(MyAdmin, self).formfield_for_dbfield(db_field, **kwargs) if db_field.name == 'myfield': myfield_choices_cache = getattr(request, 'myfield_choices_cache', None) if myfield_choices_cache is not None: formfield.choices = myfield_choices_cache else: request.myfield_choices_cache = formfield.choices return formfield Foreign keys or many to many fields in admin inlines If you have fk of m2m fields on InlineModelAdmin for every object in the formset you’ll get a database hit. You can avoid this by having something like: class MyAdmin(admin.TabularInline): fields = 'myfield', def formfield_for_dbfield(self, db_field, **kwargs): formfield = super(MyAdmin, self).formfield_for_dbfield(db_field, **kwargs) if db_field.name == 'myfield': # dirty trick so queryset is evaluated and cached in .choices formfield.choices = formfield.choices return formfield Enable template caching It’s amazing how easy it is to forget to add this in your settings: TEMPLATE_LOADERS = ( ('django.template.loaders.cached.Loader', ( 'django.template.loaders.filesystem.Loader', 'django.template.loaders.app_directories.Loader', )), ) Use select_related … -
Tweaks for making django admin faster
Here follow a number of tricks I've employed in the past to make django admin faster. Editable foreign keys in the changelist If you have foreign keys in list_editable django will make 1 database query for each item in the changelist. Quite a lot for a changelist of 100 items. The trick is to cache the choices for that formfield: class MyAdmin(admin.ModelAdmin): list_editable = 'myfield', def formfield_for_dbfield(self, db_field, **kwargs): request = kwargs['request'] formfield = super(MyAdmin, self).formfield_for_dbfield(db_field, **kwargs) if db_field.name == 'myfield': myfield_choices_cache = getattr(request, 'myfield_choices_cache', None) if myfield_choices_cache is not None: formfield.choices = myfield_choices_cache else: request.myfield_choices_cache = formfield.choices return formfield Foreign keys or many to many fields in admin inlines If you have fk of m2m fields on InlineModelAdmin for every object in the formset you'll get a database hit. You can avoid this by having something like: class MyAdmin(admin.TabularInline): fields = 'myfield', def formfield_for_dbfield(self, db_field, **kwargs): formfield = super(MyAdmin, self).formfield_for_dbfield(db_field, **kwargs) if db_field.name == 'myfield': # dirty trick so queryset is evaluated and cached in .choices formfield.choices = formfield.choices return formfield Enable template caching It's amazing how easy it is to forget to add this in your settings: TEMPLATE_LOADERS = ( ('django.template.loaders.cached.Loader', ( 'django.template.loaders.filesystem.Loader', 'django.template.loaders.app_directories.Loader', )), ) Use select_related … -
Tweaks for making django admin faster
Here follow a number of tricks I've employed in the past to make django admin faster. Editable foreign keys in the changelist* If you have foreign keys in list_editable django will make 1 database query for each item in the changelist. Quite a lot for a changelist of 100 items. The trick is to cache the choices for that formfield: class MyAdmin(admin.ModelAdmin): list_editable = 'myfield', def formfield_for_dbfield(self, db_field, **kwargs): request = kwargs['request'] formfield = super(MyAdmin, self).formfield_for_dbfield(db_field, **kwargs) if db_field.name == 'myfield': myfield_choices_cache = getattr(request, 'myfield_choices_cache', None) if myfield_choices_cache is not None: formfield.choices = myfield_choices_cache else: request.myfield_choices_cache = formfield.choices return formfield Foreign keys or many to many fields in admin inlines* If you have fk of m2m fields on InlineModelAdmin for every object in the formset you'll get a database hit. You can avoid this by having something like: class MyAdmin(admin.TabularInline): fields = 'myfield', def formfield_for_dbfield(self, db_field, **kwargs): formfield = super(MyAdmin, self).formfield_for_dbfield(db_field, **kwargs) if db_field.name == 'myfield': # dirty trick so queryset is evaluated and cached in .choices formfield.choices = formfield.choices return formfield Enable template caching* It's amazing how easy it is to forget to add this in your settings: TEMPLATE_LOADERS = ( ('django.template.loaders.cached.Loader', ( 'django.template.loaders.filesystem.Loader', 'django.template.loaders.app_directories.Loader', )), ) Use select_related … -
Viewport: Making webpages look great on iOS devices
<meta name="viewport" content="width=device-width, initial-scale=1.0"> This is the default viewport meta tag found in the very useful html5 boilerplate. If you are like me, and don't initially understand exactly what it does, this default will get you into trouble. The main issue is your website will look great on your computer but when you look at it on an iPhone or iPad it will look small, squished even depending on your CSS. If you happen be making a website which is pixel perfect for the iPhone resolution, then everything will look good, but most websites I develop are meant to be viewed on computer browser and on mobile devices, so knowing what the viewport meta tag does helps you look great everywhere. What is the viewport The viewport is the space your webpage will rendered into. Think of it like the size of the browser window. When you zoom, you are zooming into the viewport, but the size of the viewport doesn't change. You can zoom out of the viewport either, though if the page is wider than the viewport you can scroll to see more. The above viewport meta tag says make the viewport the same size as the device's … -
Django: Taking note of ManyToManyField changes
I’ve gone through a bit of a mess lately, trying to do something I considered very possible, only to ultimately fail to find a working solution. This write-up will hopefully save someone else several hours of effort, asking for help, waiting, etc. Those reading the article, please be sure to also read any comments to the article, as others may have additional ideas, solutions, or helpful information. As you can see from the documentation links in this post, I am working with version 1.3. I am not aware of any differences in 1.3.1 or 1.4.0 regarding the topic covered in this post. Scenario You have a Django model. That model has a field which is a ManyToManyField. When an instance of your model is modified via any mechanism (custom web form, Django admin interface, code using the ORM, etc), you want to execute some custom code based on what happened to the model instance’s fields (including the ManyToManyField). Specifically, what if you need to keep a non-Django source in sync with a Django model’s data? In my case, I have a “simple” need to push changes to a Django model’s data to an LDAP server. class Interface(models.Model): fqdn = models.CharField(primary=True, … -
Dutch Django meeting - lightning talks
-
Scalable Vector Graphics (SVG) - Jeroen Dijkmeijer
-
My SOPA boycott
I'm against SOPA and PIPA.I believe that those bills will kill not just free speech, but also business within the USA. Innovation will wither. I'm also of the belief that those companies trying to get SOPA into place don't realize that no idea is new and if SOPA passes they'll be hammered with an increasing amount of takedowns and suits against them for anything they do. Litigation based on SOPA won't be as easily handled as the current status quo.I've signed the petitions, I've posted on Twitter, Facebook, and Google. That isn't enough. I have to be willing to make a sacrifice. And in this case I'm going to make the sacrifice my vote.My vote sacrifice is a boycott. It's directed at any politician, local or otherwise:My Boycott:If you as a politician vote for SOPA/PIPA then you lose my vote. Regardless of whatever other opinions you have or party you belong to, you've lost me as a supporter.If SOPA/PIPA passes you can get my vote back by voting for what bill that destroys SOPA/PIPA is nominated.If SOPA/PIPA fails you can get my vote back by voting against whatever bills are resurrected to replace SOPA/PIPA.I will ignore party boundaries. I will … -
Building secure Django websites - Erik Romijn
-
Back
-
Some quick Django optimisation lessons
Some tips for keeping your Django apps lean and mean from the beginning, using lessons from django-fiber, -
Three cheers for SourceTree
I'm not a big fan of GUIs for version control software. On Windows I've tried all the Tortoise incarnations, but they always just seem to slow me down. The built in Git GUI was decent but, at the end of the day, it was always easier to just use the command line. When I switched to using a Mac full time, I didn't spend much time looking. I installed MacHg and GitHub for Mac but never used them. I was, though, having trouble finding a good Mac file comparison tool. On Windows I'd always been relatively happy with Beyond Compare. It was fast and has a ton of features. So, I still haven't found a decent comparison tool for Mac, but I did stumble onto a great VCS GUI. SourceTree has been absolutely great to work with. I pretty much have it open full time as there's no need to switch between apps (it does Git, Mercurial, and SVN). It just happens to have built-in integration for GitHub, BitBucket, and Kiln (of which I use all three). And, as far as I can tell, it's free. If you're on a Mac, I highly recommend it. -
Three cheers for SourceTree
I’m not a big fan of GUIs for version control software. On Windows I’ve tried all the Tortoise incarnations, but they always just seem to slow me down. The built in Git GUI was decent but, at the end of the day, it was always easier to just use the command line. When I switched to using a Mac full time, I didn’t spend much time looking. I installed MacHg and GitHub for Mac but never used them. I was, though, having trouble finding a good Mac file comparison tool. On Windows I’d always been relatively happy with Beyond Compare. It was fast and has a ton of features. So, I still haven’t found a decent comparison tool for Mac, but I did stumble onto a great VCS GUI. SourceTree has been absolutely great to work with. I pretty much have it open full time as there’s no need to switch between apps (it does Git, Mercurial, and SVN). It just happens to have built-in integration for GitHub, BitBucket, and Kiln (of which I use all three). And, as far as I can tell, it’s free. If you’re on a Mac, I highly recommend it. -
Forms Part 2: Class Based Forms
Forms are generally annoying to deal with. Fortunately Django offers some great ways to work with forms to make your life easier. In this our second part on forms we walk you through Class Based Forms in Django on our way to understanding the breadth of what you can do with forms in Django.Watch Now...