Django community: RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
Дизайн API Я.ру
Вчера мы открыли в бету API для Я.ру. Это был первый пост в корпоративном блоге Яндекса с кодом на Питоне, что даже породило фан-арт :-). Для меня этот запуск имеет большое эмоциональное значение, потому что машиночитаемый веб — мой давний интерес, и этот проект — первый неигрушечный публичный API, где я занимаюсь дизайном, и могу смотреть, как выживают на практике теоретические соображения о том, как это должно делаться. Я говорю тут от своего лица, и чтобы не возникало ложных ощущений, должен сказать, что я это всё делаю, конечно, не один. Начинал писать собственно серверную часть Иван Челюбеев. Моя текущая роль — проектировщик и менеджер. Код пишет сейчас Костя Меренков, а со стороны Я.рушного бэкенда нам помогает Серёжа Чистович. Этот пост — несколько заметок о том, как всё устроено внутри. Пишите в комментариях, если что-то нужно раскрыть подробнее. Сервис над сервисом Сам Я.ру — многоуровневая кодобаза на нескольких языках и технологиях. Серверная её часть общается с внешним для себя миром по CORBA, и поэтому не подходит для публичного API. Кроме того, публичное API поддерживать тяжелее, чем внутреннее, из-за необходимости думать про обратную совместимость. Поэтому API сервиса Я.ру — это по сути ещё один отдельный сервис, который смотрит во внешний мир через … -
Going Mobile with Django and jQuery
Going Mobile with Django and jQuery -
An update to my Celery rant
An update to my rant on Celery’s frequently-changing API: I’ve decided to stay with Django-celery 2.5.5 and Celery 2.5.3. When I tried using Celery 3.0.4 with my existing code, Pylint threw about 60 warnings, many of which look real and all of which weren’t there when I used Celery 2.5.3. “Backwards-compatible” my ass! I shouldn’t have to chase my tail like this. Celery, you lost me. I’m now looking to replace you. Tagged: Django, Python -
Celery API changes drive me nuts
This is a rant. My company’s code base is over 65K lines of Python and JavaScript code. We use Celery, Django-Celery, and RabbitMQ for our background asynchronous tasks. Ten different tasks.py files contain 30 task classes, split roughly 50-50 between periodic and on-demand. We use subtasks. Today, I dug into updating from Celery 2.5.3 to 3.0.4, and I popped my cork. I am aggravated by the frequency and extent of Celery API changes. It’s easily changed more often than any other five technologies in our stack combined. I’ve been upgrading Celery and Django-celery every six months or so, which corresponds to upgrading every few minor versions. And the changes are similar in scope to what I see when upgrading any other technology across one or two major versions. Changes that may seem minor, like moving a class across modules (e.g., from celery.task.base to celery.app.task), are nontrivial when you have change lots of code. I might feel better if the edits gave my code some awesome new capabilities. But they don’t, so my internal reaction is, Why the <expletive> does this code have to change? Couldn’t the <unbelievably gross expletive involving a deformed farm animal> code have stayed where it <repeat … -
Django compressor and image preloading
Preface: Have you noticed how on some websites when you click on a link that opens a lightbox or any overlay for first time it takes some time to display the border/background/button images. Not quite fancy, right? This is because the load of this images starts at the moment the overlay is rendered on the screen. If this is your first load and these images are not in your browser cache it will take some time for the browser to retrieve them from the server. Solution: The solution for this is to preload the images i.e. to force the browser to request them from the server before they are actually used. With a simple javascript function and a list of the images URLs this is a piece of cake: $.preLoadImages = function() { var args_len = arguments.length; for (var i=0; i < args_len; i++) { var cacheImage = document.createElement('img'); cacheImage.src = arguments[i]; } } $.preLoadImages('/img/img1.png', '/img/img2.png') Please have in mind that the code above uses the jQuery library. Specialty: Pretty easy, but you have to hardcode the URLs of all images. Also if you are using Django compressor then probably you are aware that it adds extra hash to the … -
Do you check HTTPS certificates in your API clients?
Far from every API client checks HTTP Certificates by default. We need to educate each other to be safe! -
Django Update View without slug in the url
Today I wanted to use the Django Class Based View (CBV) UpdateView but without a slug identifier in the URL. For example, instead of /profiles/pydanny/ I would go to /my-crazy-profile/. Also, I needed to force authentication. I've done this with Django functional views a few times times, but today I did it in Django. This is what I did: 1. Added django-braces to my project Kenneth Love and Chris Jones' awesome django-braces package has some very handy mixins for working with Django CBVs. Kenneth and Chris really understand CBVs, specifically on how to extend them, and have provided a bunch of really useful utility methods in the django-braces library. Yeah, I could figure this stuff out on my own, but since those guys already did the hard work I might as well just lean on them. pip install django-braces==0.1.3 # settings.py INSTALLED_APPS = ( ... 'braces', ... ) 2. Wrote the view Assuming a very simple profile Model and Form (which they weren't - but that's not what this post is about), this is how I implemented the view: # profiles/views.py from django.views.generic import UpdateView from braces.views import LoginRequiredMixin # handles authentication from profiles.forms import ProfileForm from profiles.models import Profile … -
Django Update View without slug in the url
Today I wanted to use the Django Class Based View (CBV) UpdateView but without a slug identifier in the URL. For example, instead of /profiles/pydanny/ I would go to /my-crazy-profile/. Also, I needed to force authentication. I've done this with Django functional views a few times times, but today I did it in Django. This is what I did: 1. Added django-braces to my project Kenneth Love and Chris Jones' awesome django-braces package has some very handy mixins for working with Django CBVs. Kenneth and Chris really understand CBVs, specifically on how to extend them, and have provided a bunch of really useful utility methods in the django-braces library. Yeah, I could figure this stuff out on my own, but since those guys already did the hard work I might as well just lean on them. pip install django-braces==0.1.3 # settings.py INSTALLED_APPS = ( ... 'braces', ... ) 2. Wrote the view Assuming a very simple profile Model and Form (which they weren't - but that's not what this post is about), this is how I implemented the view: # profiles/views.py from django.views.generic import UpdateView from braces.views import LoginRequiredMixin # handles authentication from profiles.forms import ProfileForm from profiles.models import Profile … -
Orateurs, les rencontres django régionales ont besoin de vous !
L’un des résultats des discussions de la dernière DjangoCong fut de lancer l’idée de faire des DjangoCon régionales plus petites et qui permettraient de ne pas centraliser les attentes de toute la communauté sur un seul événement national. Et du coup d’éviter de générer de la frustration pour ceux qui n’ont pu acheter leurs billets et qui enragent, chez eux, parce qu’ils loupent la grand messe django FR. (pour avoir une idée de ce que cela peut donner, voir ce lien (oui je sais c’est un vieux lien, mais il est parfait pour la situation) ). Deux première initiatives ont été finalement été lancés : DjangoBreizh à Rennes (vive la Bretagne, le temps pourris, le cidre et les crêpes) le samedi 17 novembre. DjangoCon Toulouse (ils ont oublié un g à la fin du nom.. tss tss, mais bon vive le cassoulet) le 24 et 25 novembre. C’est avec une grande joie que j’ai vu naître ces deux initiatives locales (d’ailleurs je serais présent, d’une manière quasi certaine à Rennes et peut-être, mais vraiment peut-être à Toulouse). Parce que ce n’est pas forcément évident d’organiser des conférences, même ‘juste locale’. Et que même dans certains domaines, il me semble que … -
Django Update View without slug in the url
Today I wanted to use the Django Class Based View (CBV) UpdateView but without a slug identifier in the URL. For example, instead of /profiles/pydanny/ I would go to /my-crazy-profile/. Also, I needed to force authentication. I've done this with Django functional views a few times times, but today I did it in Django. This is what I did: 1. Added django-braces to my project Kenneth Love and Chris Jones' awesome django-braces package has some very handy mixins for working with Django CBVs. Kenneth and Chris really understand CBVs, specifically on how to extend them, and have provided a bunch of really useful utility methods in the django-braces library. Yeah, I could figure this stuff out on my own, but since those guys already did the hard work I might as well just lean on them. pip install django-braces==0.1.3 # settings.py INSTALLED_APPS = ( ... 'braces', ... ) 2. Wrote the view Assuming a very simple profile Model and Form (which they weren't - but that's not what this post is about), this is how I implemented the view: # profiles/views.py from django.views.generic import UpdateView from braces.views import LoginRequiredMixin # handles authentication from profiles.forms import ProfileForm from profiles.models import Profile … -
Redirect to Custom URL After Saving in Django Admin
Here's a quick and dirty admin hack that can save you *lots* of time if you're used to browsing between editing objects in admin and vieving the results on a live site. Put following `change_view`-method in your `admin.py` and now you can make links to admin change page that return you back to where you came from: ```python class BlogEntryAdmin(admin.ModelAdmin): ... def change_view(self, request, object_id, form_url='', extra_context=None): result = super(BlogEntryAdmin, self).change_view(request, object_id, form_url, extra_context) if request.GET.get('return_to', False): result['Location'] = request.GET['return_to'] return result ``` In your template you'd have something like: ```django {% if request.user.is_staff %} <a href="/admin/blog/blogentry/{{ entry.pk }}/?return_to={{ entry.get_absolute_url }}">Edit in admin</a> {% endif %} ``` Of course, if you're smart, you should convert any places that need this kind of functionality with frontend editing tools so you can forget going to the Admin in the first place. But meanwhile, those few lines of code may come in handy! -
Redirect to Custom URL After Saving in Django Admin
Here’s a quick and dirty admin hack that can save you lots of time if you’re used to browsing between editing objects in admin and vieving the results on a live site. Put following change_view-method in your admin.py and now you can make links to admin change page that return you back to where you came from: class BlogEntryAdmin(admin.ModelAdmin): ... def change_view(self, request, object_id, form_url='', extra_context=None): result = super(BlogEntryAdmin, self).change_view(request, object_id, form_url, extra_context) if request.GET.get('return_to', False): result['Location'] = request.GET['return_to'] return result In your template you’d have something like: {% if request.user.is_staff %} <a href="/admin/blog/blogentry/{{ entry.pk }}/?return_to={{ entry.get_absolute_url }}">Edit in admin</a> {% endif %} Of course, if you’re smart, you should convert any places that need this kind of functionality with frontend editing tools so you can forget going to the Admin in the first place. But meanwhile, those few lines of code may come in handy! -
Sunlight and Open Source
David Eaves has a thoughtful post over at TechPresident talking about open source and the transparency community's commitment to it -- a commitment that David sees as half-hearted. Sunlight's mentioned in the post, and the MySociety initiative that prompted the post is something that our team has been thinking about a lot. I think there's something to David's criticisms. But he's missing a few important things. But let's get the baseline stuff out of the way first. Sunlight loves open source. Our whole stack is built on it, from the Varnish cache your browser connects to, to the Django/Rails/Flask/Sinatra/whatever app behind it, to the Postgres/Mongo/Redis/Solr/elasticsearch datastores that powers it, to the OpenOffice suite that edits the grant application that paid for it all. All of our stuff is up on GitHub, and we welcome and celebrate contributions from the community. But, Kindle contest aside, the above examples are mostly about us benefiting from open source. What have we done for the movement lately? This is the crux of David's critique: So far, it appears that the spirit of re-use among the big players, like MySociety and the Sunlight Foundation, only goes so deep. Indeed often it seems they are limited … -
django-statsd 0.3.6.1
I just released django-statsd 0.3.6.1. I've been doing lots of incremental pushes of django-statsd over the last few months. In the last versions I added the following... A log client that will just log to a python logging handler. This is quicker and easier to set up than the toolbar which is still fraught with pitfalls. A quick example would be setting this: STATSD_CLIENT = 'django_statsd.clients.log' LOGGING = { 'handlers': { 'console': { 'class': 'logging.StreamHandler', }, }, 'loggers': { 'statsd': { 'handlers': ['console'], }, }, } ... and you'll get the statsd pings in your console. Useful for development. I've been collecting navigation timing on Mozilla sites for a while, but they haven't been supeer useful. In an attempt to copy what New Relic do, I started parsing the timing data in a similar manner. Giving some new statistics from navigation timing: network, app, dom and rendering. Although I think I still need to refine those. Finally, if you can get Django debug toolbar and django-statsd working together, you should now be rewarded with a nice waterfall view of the timings, for example: That's it for now. -
Two talks accepted by DjangoCon
I'm happy to report that the DjangoCon schedule has been announced and we have two presentations that made the cut. Joe Jasinski will be making the solo presentation, "So You Want to Make a Payment: Comparing and contrasting Django payment options" which will introduce and compare some common payment ... -
__name__ for Python class and Python function
This is my first post in the series of small random learnings. The attribute __name__ is by default present in every python function. But suprisingly an object of a class doesnt have this attribute unless specifically specified. class example(): def classFunction(): return "test" obj = example() print obj.__name__ #This is an error On the other hand , def functionA(): return "A" def functionB(params): return params.__name__ print functionB(functionA) # prints functionA -
Installation de virtualenvwrapper chez Alwaysdata
J’ai il y a quelques temps fait de multiples tests chez AD. Certains nécessitaient l’utilisation de paquet python non installé par défaut sur le serveur mutu AD. Du coup, j’ai voulu installer virtualenwrapper pour me simplifier la tache. Voici la méthode que j’ai utilisé. Je suis preneur de toute amélioration que vous pourriez me remonter sur ma manière de faire (pour le jour où j’aurais vraiment besoin d’installer des choses pour faire de la prod avec du virtualenv). D’ailleurs je la partage ici autant pour le plaisir de la partager, que pour ne pas l’oublier que pour vous permettre de l’améliorer. Le commencement, installer virtualenwrapper. En local sur son home. En faisant : pip install --install-option="--user" virtualenvwrapper du coup cela vous installe le tout dans $HOME/.local/…. Une fois installé, il faut faire (comme pour installation normale) la création du répertoire de vos environnements en faisant : export WORKON_HOME=$HOME/.virtualenvs mkdir -p $WORKON_HOME Ensuite histoire de ne plus avoir à le faire, dans votre .bash_profile : export PYTHONPATH=~/.local export WORKON_HOME=~/.virtualenvs export PATH=$PATH:$PYTHONPATH/bin source ~/.local/bin/virtualenvwrapper.sh un petit coup de source ~/.local/bin/virtualenvwrapper.sh et Voila ! (à dire comme avec l’accent d’un américain qui prend un accent français). Vous aller pouvoir faire des mkvirtualenv en … -
Django Workshop at Free Software Master 2012
Last weekend I gave my annual Django workshop for this year’s students of the Free Software Master that Igalia organizes. When I started with Django it was 2007 and I was happy with its version 0.96 Currently with its version 1.4, the path that it took and the improvements it has got are incredible and it is used by many interesting companies and organizations. My presentation for this year’s workshop covers more things and is based on Django 1.4. You can check it out below, the license is Creative Commons as usual: (direct link to presentation in SpeakerDeck) -
UnicodeEncodeError when uploading files in Django using Supervisor and Gunicorn
Issues related uploading files with unicode filenames have long history with Django and Linux. There are many stack overflow questions and blog posts on the subject already, but very few deal with solving the problem when using Supervisord and Gunicorn directly. If you are using apache2 and mod_python you can configure your as per the docs: https://code.djangoproject.com/wiki/django_apache_and_mod_wsgi#AdditionalTweaking Fundamentally all fixes have the same goal: configure the environment of the server running Django to use the correct locale, so sys.getfilesystemencoding() will return 'UTF-8' instead of 'ANSI_X3.4-1968'. What makes solving this problem especially hard is there are many possible solutions and picking the right one for you situation can be difficult. Ignore Nginx Related Advice Since you are using Gunicorn, I am going to guess you are using Nginx and you will come across posts saying you need to add charset utf-8 to the http section in the root Nginx config file. Though doing this is probably generally a good idea, it will not solve the problem. Nginx knows nothing about Python, Django or Gunicorn, except that when it makes requests down a unix or TCP socket it gets back bytes. No setting in Nginx will affect the environment Python is running … -
Spurious CORS Errors from Sentry
I realised the other day that [Sentry][sentry], the awesome system we have been using for a while to track our error logs from our [Django][django] project, can also be used to track exceptions from other systems. Like Javascript. In fact, there is a client available: [raven.js][ravenjs]. So, we have a server set up for work, but I have a side-project I have been working on, Workout Builder. So, I thought I'd set up a server in [Heroku][heroku] to act as my sentry server. And I found a nice simple way to get up and running: Daniel Watkins has a nice post over at [Odd_Blog][odd-blog], [Deploying Sentry on Heroku][odd-blog-sentry]. It's pretty straightforward, and extremely simple. I got it up and running in no time, and then attempted to set up an email service. Rather than use my actual account for sending, I thought I'd set up a sending-only account at my domain, hosted as a Gmail Apps domain. So, I set it up, and set about testing. All of a sudden, I'm getting errors, that didn't appear for 30 seconds, that my test domain is not permitted to send a request due to CORS. But, I had been sending them previously. … -
Django Training
Caktus is proud to announce that we are now offering Django training services. This new service includes both off and on-site training for you and your team to become a Djangonaut. We kicked off this new service by hosting a Django Bootcamp at the Caktus office. It was a wonderful experience and our trainees came ... -
apibaleares.com
El dimecres passat vàrem posar en producció la nova web de portal immobiliari del Col·legi Oficial d'Agents de la propietat immobiliària de les Illes Baleares, que es mostra baix el domini http://www.apibaleares.com. Com és habitual és una aplicació feta amb Django, i amb la idea de que sigui una aplicació escalable i sobretot mantenible. El sector immobiliari no passa pel seu millor moment, tot val a dir-ho, però és un projecte que m'ha fet especial il·lusió. Per una part significa tractar amb molta gent que fa servir el programa, cada un amb les seves pròpies sensibilitats, i en un sector on la tecnologia informàtica s'obre pas molt a poc a poc. És un dels primers projectes que fem amb Django 1.4, així que aprofitaré per fer cinc cèntims del que ha representat. Django 1.4 canvia un poc l'estructura dels projectes per a fer-los més modulars. Per la nostra banda hem aprofitat per també seguir amb el camí iniciat amb PropietariosOnline i SpokenPic i desenvolupar tota l'estructura de vistes mitjançant les class bassed views. Tot i que hi ha gent a favor i en contra, he de dir que ara per ara, i pels tipus de projectes que fem les CBV … -
PyFlakes-clean import of local_settings.py
Any project have own bunch of settings for different environments: for dev, production, staging. Everyone use it on daily basis. But annoying thing is that PyFlakes, great code analysis tool, warn about that. And it's reasonable.So, to have this functionality, but without warning I use this pattern:try: import local_settings for setting in dir(local_settings): if setting.startswith('_'): continue locals()[setting] = getattr(local_settings, setting)except ImportError: passSimple and useful!Update: I've added ignore of attributes which starting with underscore. Thanks to Igor Davydenko -
Running a Django (or any other) dev instance over HTTPS
Being able to run your dev instance over HTTPS is really useful: you might spot some weird bug that would have bitten you in production, and if you do find one, you can debug it much more easily. Googling for this subject resulted in several different tutorials using stunnel, but all of them broke in some way on my machine running Ubuntu Maverick. So here is how I got stunnel working – perhaps it will help someone else too: sudo aptitude install stunnel sudo su - cd /etc mkdir stunnel cd stunnel openssl req -new -x509 -days 365 -nodes -out stunnel.pem -keyout stunnel.pem openssl gendh 2048 >> stunnel.pem chmod 600 stunnel.pem logout cd Now create a file called dev_https with the following text: pid= foreground=yes debug = 7 [https] accept=8443 connect=8000 TIMEOUTclose=1 Note: this assumes your web server is running on port 8000. If it’s not, change the value of “connect” to the appropriate port. Finally, run: sudo stunnel4 dev_https Now if you go to https://localhost:8443/, you should see your HTTPS-enabled dev instance! Note: To properly simulate a HTTPS connection in Django, you should also set an environment variable HTTPS=on. Without this, request.is_secure() will return False. You could set it … -
July 15th, 2012 LA Open Source Recap
On July 15th, 2012, at the July LA Hackathons Open Source event, over 60 Python, Django, Ruby, Ruby on Rails, PHP, JavaScript, Node.js, Perl, and Clojure developers arrived to work on a variety of projects. We went from 10 AM to 10 PM, and there was much coding and learning done by all. There was a very powerful vibe shared by nearly everyone there, and it was great to see so many people from different personal and open source backgrounds working together and having a great time as a group. Filtering out distractions We made some changes to our proceedings for this event, entirely because last time with 'distractions' that caused some issues. In a nutshell, here is 50% of what we did: A clear statement in the event description and follow up emails that this event was for developers and designers only. Laptops were an absolute requirement for entry into the event. We also rolled in a couple other tricks I'm not going to write down. If you see me in person, ask away. The results were near perfect, and we won't hesitate to use these techniques again. Sponsors We let attendees know which sponsors were interested in potential …