Django community: RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
Actually doing work - proof by blogging
I'm a programmer. Which means it is not always clear when I'm working. What I mean is that, if I'm sitting behind my screen, I might be reading an article about postgres performance improvements, but I might also be reading the news. And clicking on my ipad might mean I'm keeping my python knowledge up to date by reading weblogs, but I might also be reading some comics. Typing furiously on my keyboard might indicate great productivity, but it also might indicate a long personal email. The other way around, sitting nicely in the living room at home, clicking away on the laptop, might mean I'm relaxing by writing an update on my model railway work on a forum, but it might also mean I'm finishing off a work project in my own time. "Doing research" is a potential problem for me. I've spend a ton of time reading about Ansible and on how to use it. But it was only when I actually started doing something with it that I started to wrap my head around it. So... just reading and thinking is dangerous to my productivity. I want to be productive. One of the simplest tricks I can … -
VariableDoesNotExist: Failed lookup for key [text] in 'None'
VariableDoesNotExist: Failed lookup for key [text] in 'None', that was the error that I got out of an internal django app that I wrote. The app is the most error-free one that I ever build, so I was quite surprised. I was even more surprised when I started debugging it. The error was in the search functionality. Searching for "lizard" would give an "error 500" page. Searching for "lizar" gave 0 results (which is fine), for "lizardd", too. "lidard" also gave 0 results. Only a search for "lizard" crashed the site. Huh? Weird. I got the question whether I put a hidden message into this app (as "lizard" is one of our main products) :-) Locally it all worked fine, of course. Only on the server did I see the error. Here's the relevant part of the traceback: Stacktrace (most recent call last): File "django/core/handlers/base.py", line 112, in get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "haystack/views.py", line 50, in __call__ return self.create_response() File "haystack/views.py", line 144, in create_response return render_to_response(self.template, context, context_instance=self.context_class(self.request)) File "django/shortcuts/__init__.py", line 29, in render_to_response return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs) File "django/template/loader.py", line 169, in render_to_string return t.render(context_instance) File "django/template/base.py", line 140, in render return self._render(context) ... File … -
New emacs setup
I edit all my code with emacs. Since 1996 or so. And all blog entries. And all... So my emacs settings are "quite important" to me. When I installed the new laptop I'm typing this on, my settings stopped working: I just deleted my ~/.emacs.d/ directory because I wanted to upgrade my setup. Since a couple of years I used the emacs starter kit version 2. The link goes to my slightly modified copy of the original version 2 starter kit. The setup change I wanted to make was to update to the version 3 of the emacs starter kit. That link goes to the real starter kit, which in version 3 is... just a text document! Which is a good thing. You don't need to clone a git repo just to get a bunch of settings. You use emacs' package tool to download the better-defaults package, which contains most/all of the settings changes that made the original emacs starter git such a joy to use. And on top of that, it suggests a couple of handy packages to use also. I'm using all of the suggested ones (apart from 'scpaste', which I don't use). And it provides a template … -
Updated instructions to fix macbook pro with black screen
Last week I wrote about my macbook pro and its screen that suddenly stayed black after waking it from sleep. Including instructions on how to fix it. I now have improved instructions... The piece of crap did it again. (This time there was no external monitor in play, btw) Symptoms: you open up your late 2014 macbook pro 15" and expect it to wake from sleep in 0.5 seconds. Instead the screen stays black. Press shift-option-control-power and release them all at the same time. This resets the "system management controller", amongst others it makes sure the "power" button actually reacts. Now look at the back of your mac's screen. Make sure the light of the apple logo is off. If necessary, keep the power button pressed for 5 seconds to force a shutdown. (The power button should work again after step 1). Keep your fingers ready for pressing command-option-p-r at the same time. You need both hands for this weird combination. Press the power button and then hold these four keys. Your mac will now actually boot, give you the startup sound and lo and behold, you've got your screen back. I do hope apple will fix this Real Soon. … -
Proxying objects in Python
A lazy object proxy is an object that wraps a callable but defers the call until the object is actually required, and caches the result of said call. These kinds of objects are useful in resolving various dependency issues, few examples: Objects that need to held circular references at each other, but at different stages. To instantiate object Foo you need an instance of Bar. Instance of Bar needs an instance of Foo in some of it methods (but not at construction). Circular imports sound familiar? Performance sensitive code. You don't know ahead of time what you're going to use but you don't want to pay for allocating all the resources at the start as you usually need just few of them. There are other examples, I've just made up a couple for context. If you've used Django you may be familiar with SimpleLazyObject. For simple use-cases it's fine, and if you're already using Django the choice is obvious. Unfortunately it's missing many magic methods, most glaring omissions: __iter__, __getslice__, __call__ etc. It's not too bad, you can just subclass and add them yourself. But what if you need to have __getattr__? The horrors of the infinite recursive call beckon. … -
Proxying objects in Python
A lazy object proxy is an object that wraps a callable but defers the call until the object is actually required, and caches the result of said call. These kinds of objects are useful in resolving various dependency issues, few examples: Objects that need to held circular references at each other, but at different stages. To instantiate object Foo you need an instance of Bar. Instance of Bar needs an instance of Foo in some of it methods (but not at construction). Circular imports sound familiar? Performance sensitive code. You don't know ahead of time what you're going to use but you don't want to pay for allocating all the resources at the start as you usually need just few of them. There are other examples, I've just made up a couple for context. If you've used Django you may be familiar with SimpleLazyObject. For simple use-cases it's fine, and if you're already using Django the choice is obvious. Unfortunately it's missing many magic methods, most glaring omissions: __iter__, __getslice__, __call__ etc. It's not too bad, you can just subclass and add them yourself. But what if you need to have __getattr__? The horrors of the infinite recursive call beckon. … -
Proxying objects in Python
A lazy object proxy is an object that wraps a callable but defers the call until the object is actually required, and caches the result of said call. These kinds of objects are useful in resolving various dependency issues, few examples: Objects that need to held circular references at each other, but at different stages. To instantiate object Foo you need an instance of Bar. Instance of Bar needs an instance of Foo in some of it methods (but not at construction). Circular imports sound familiar? Performance sensitive code. You don't know ahead of time what you're going to use but you don't want to pay for allocating all the resources at the start as you usually need just few of them. There are other examples, I've just made up a couple for context. If you've used Django you may be familiar with SimpleLazyObject. For simple use-cases it's fine, and if you're already using Django the choice is obvious. Unfortunately it's missing many magic methods, most glaring omissions: __iter__, __getslice__, __call__ etc. It's not too bad, you can just subclass and add them yourself. But what if you need to have __getattr__? The horrors of the infinite recursive call beckon. … -
Django Forms
This is another example of Django's reusability. We saw how to create your own form in HTML and render it and get data from it using GET or POST methods. Again, Django does this for you. While it certainly is useful knowing how to do this on a basic level, using Django's Forms API can give you more powerful forms with richer features.Like models.py, create a file called forms.py in the app where the form is required. Let's create a simple contact form. In forms.py:forms.pyfrom django import formsclass SimpleUploadForm (forms.Form): name = forms.CharField (label = "Enter name") email = forms.EmailField (label = "Enter email ID", required = False) dob = forms.DateTimeField (label = "Enter date of birth")As you can see, just like a model, we created a simple form. Now let's render it from a view. views.pyfrom django.shortcuts import renderfrom forms import SimpleContactFormdef simple_contact_form (request): form = SimpleContactForm () return render (request, "simple_contact_form.html", {"form":form})Here, we import the SimpleContactForm class from forms.py and initialize its instance in the view. Then we pass that instance as a context to the webpage we're rendering. The view will become clearer when you see the template.simple_contact_form.html<html> <head><title>Simple Contact Form</title></head> <body> <form action = "" … -
Macbook pro black screen and restart problem
This morning I took my macbook pro (a very nice late 2014 retina 15" one) out of my backpack, opened it and plugged in the external monitor. But it stayed black. The macbook's own screen also stayed black. Ouch? It didn't seem to wake up... Some standard things I checked/tried: The battery couldn't be empty yet. The apple logo on the back of the screen was illuminated, right? Of course I had already attached it to the power cord. Which showed its LED, so power wasn't the problem. Screen brightness. Perhaps I dialed it down to zero by accident? No. Close/open again. Take out external display. Etc. Nope. Ok, time to press the power button. No reaction that I could see/hear. Press it 10 seconds. No. Problem with such a new macbook is that it doesn't have a harddisk. The noises a harddisk makes tend to provide pretty good feedback on what's happening normally :-) I just couldn't get the macbook to reset (or shut down)! If the power button doesn't work... Googling turned up two additional tricks: Resetting the "SMC", the system management controller. One of the things it does is monitor the power button... Press shift-option-control-power and release … -
Integrating front-end tools with your Django project
Front-end tools like Grunt and Gulp are becoming very widespread, and there’s good reason for it. The front-end code is no longer static, CSS pre-processors are the norm, and JavaScript modules such as Require, Browserify or even coffeescript are also becoming more common as well. On top of that, when you’re deploying to production, you want to minify everything but when you’re in development mode, you want sourcemaps for JS and CSS among other things. At Lincoln Loop, we use Gulp as our task runner to do the following things: Compile Sass into CSS, including autoprefixer support Compile JavaScript with Browserify Add sourcemaps for our compiled JavaScript and CSS Minify images with imagemin Reload the browser automatically on JavaScript (and/or template) changes Refresh the CSS without reloading the browser on CSS changes Trigger system notifications when Sass or JavaScript has errors Minify JavaScript and CSS for production deploy and finally: Generate a build folder where all the generated files are stored We won’t go into details about our Gulp setup on this post, there’s plenty of information out there (we wrote about Grunt before). If you’re curious about the code thou, here’s a gist of a gulpfile-in-one that includes most … -
Example Ember.js + Django + Django Rest Framework single-page application
Ember.js is one of JavaScript frameworks for creating interactive single page web applications. Pages may change but the browser doesn't reload the page. This framework has some similarities with Django and can be good pick to start with such frontend applications for Django developers with some JavaScript knowledge. In this article I'll show you an example Django + Django Rest Framework + Ember.js application. Classical posts and categories done in Ember and in Django for comparison. -
HTTP headers
Many times you many need the IP address of the computer that's accessing your webapp. You may want it for a survey, or cataloging that user to offer better services and what not. Today we'll see not only how to get the IP address, but a host of other client side information that can be used to fine tune your app.Let's take the IP address for example. I once needed it to see from where the app was being accessed from the most. I wrote a tracker that would piggyback on the client's signal and work backwards all the way to the source and get me the IP address. But little did I know that Django already does that for me. The IP address and host name (ISP) is encoded in the request parameter. Let's see how to view them.To do this we'll create a simple view. In HTML, we'll create a list that contains header name and value, and then populate it through Django.headers.html<html> <head><title>Django HTTP headers</title></head> <body> <ul> {% for header in headers %} … -
App specific URLs
As you may know, Django boasts reusability of apps. This means you can just copy one app from one project, plug it into another and it would still work the same way. Neat, huh? Well, admittedly, till now we've only focused on getting out projects to run. But now that we know the basics of creating a working Django project, it's time to delve deeper and take advantage of Django's rich features.Let's start with URLs. Till now, we just went on adding URLs to the main urls.py as we developed the project. But when the number of URLs starts going into the triple digits, the urls.py file can become massive and difficult to keep track of. So let's see how to create a urls.py file in each app and direct the main file to this new one.In the main urls.py file, add a pattern that will point to the app:url (r'^myapp/', include('myproject.myapp.urls')),This tells Django that whenever someone access the /myapp/ URL they should be directed to the urls.py file in that app. From here on in, all the views rendered in this app will have URLs like /myapp/foo/bar. This makes them easier to manage. There's also another advantage: you no longer need unique … -
Almost premature optimization
In airmozilla the tests almost all derive from one base class whose tearDown deletes the automatically generated settings.MEDIA_ROOT directory and everything in it. Then there's some code that makes sure a certain thing from the fixtures has a picture uploaded to it. That means it has do that shutil.rmtree(directory) and that shutil.copy(src, dst) on almost every single test. Some might also not need or depend on it but it's conveninent to put it here. Anyway, I thought this is all a bit excessive and I could probably optimize that by defining a custom test runner that is first responsible for creating a clean settings.MEDIA_ROOT with the necessary file in it and secondly, when the test suite ends, it deletes the directory. But before I write that, let's measure how many gazillion milliseconds this is chewing up. Basically, the tearDown was called 361 times and the _upload_media 281 times. In total, this adds to a whopping total of 0.21 seconds! (of the total of 69.133 seconds it takes to run the whole thing). I think I'll cancel that optimization idea. Doing some light shutil operations are dirt cheap. -
Almost premature optimization
In airmozilla the tests almost all derive from one base class whose tearDown deletes the automatically generated settings.MEDIA_ROOT directory and everything in it. Then there's some code that makes sure a certain thing from the fixtures has a picture uploaded to it. That means it has do that shutil.rmtree(directory) and that shutil.copy(src, dst) on almost every single test. Some might also not need or depend on it but it's conveninent to put it here. Anyway, I thought this is all a bit excessive and I could probably optimize that by defining a custom test runner that is first responsible for creating a clean settings.MEDIA_ROOT with the necessary file in it and secondly, when the test suite ends, it deletes the directory. But before I write that, let's measure how many gazillion milliseconds this is chewing up. Basically, the tearDown was called 361 times and the _upload_media 281 times. In total, this adds to a whopping total of 0.21 seconds! (of the total of 69.133 seconds it takes to run the whole thing). I think I'll cancel that optimization idea. Doing some light shutil operations are dirt cheap. -
Terrible choices: MySQL
I've used MySQL for a while now, and there were lots of surprising things I needed to cater for. This is from a Django and MySQL 5.5 perspective [*]. Later on you'll see the horrible things I did to work around. It was a terrible experience, as I've also used PostgreSQL ... Feel free to add your own experiences in the comments section. The defaults* MySQL supports a large part of the ANSI SQL 99 standard, however the default settings are nowhere close to that. If you used any other database then it's going to be a very perplexing experience. SQL mode* With the default settings MySQL truncates and does other unspeakable things to data for the sake of not giving errors. Where is this a correct choice, hard to say. What the defaults allow: Storing invalid dates like '0000-00-00' or '2010-01-00' [1]. Silently treating errors like: Specifying an unavailable storage engine. It will use the default engine, silently [2]. Inserting invalid data. Larger strings get truncated to the maximum length. Larger integers get truncated to the maximum. Other things get converted to NULL if the column allows that. All silently [3]. And no, ORMs won't save you from this … -
Terrible choices: MySQL
I've used MySQL for a while now, and there were lots of surprising things I needed to cater for. This is from a Django and MySQL 5.5 perspective [*]. Later on you'll see the horrible things I did to work around. It was a terrible experience, as I've also used PostgreSQL ... Feel free to add your own experiences in the comments section. The defaults * MySQL supports a large part of the ANSI SQL 99 standard, however the default settings are nowhere close to that. If you used any other database then it's going to be a very perplexing experience. SQL mode * With the default settings MySQL truncates and does other unspeakable things to data for the sake of not giving errors. Where is this a correct choice, hard to say. What the defaults allow: Storing invalid dates like '0000-00-00' or '2010-01-00' [1]. Silently treating errors like: Specifying an unavailable storage engine. It will use the default engine, silently [2]. Inserting invalid data. Larger strings get truncated to the maximum length. Larger integers get truncated to the maximum. Other things get converted to NULL if the column allows that. All silently [3]. And no, ORMs won't save you … -
Terrible choices: MySQL
I've used MySQL for a while now, and there were lots of surprising things I needed to cater for. This is from a Django and MySQL 5.5 perspective [*]. Later on you'll see the horrible things I did to work around. It was a terrible experience, as I've also used PostgreSQL ... Feel free to add your own experiences in the comments section. The defaults * MySQL supports a large part of the ANSI SQL 99 standard, however the default settings are nowhere close to that. If you used any other database then it's going to be a very perplexing experience. SQL mode * With the default settings MySQL truncates and does other unspeakable things to data for the sake of not giving errors. Where is this a correct choice, hard to say. What the defaults allow: Storing invalid dates like '0000-00-00' or '2010-01-00' [1]. Silently treating errors like: Specifying an unavailable storage engine. It will use the default engine, silently [2]. Inserting invalid data. Larger strings get truncated to the maximum length. Larger integers get truncated to the maximum. Other things get converted to NULL if the column allows that. All silently [3]. And no, ORMs won't save you … -
Custom Django Admin Actions
Doing bulk data actions in the admin is easy, if you know how to do it. In this video you will learn how to create your own custom admin actions so you can update a lot of data objects all at the same time.Watch Now... -
BangPypers Dev Sprint!
Here is the definition of sprint from Wikipedia:On 3rd Saturday of every month there will be a meetup for Python developers in Bangalore(BangPypers) to have talks and workshops. This month instead of talks/workshops, a dev sprint was held. The basic idea was to encourage Python developers to contribute to any open source projects or work on their open source projects.I was working on Nidaba, a project to extract information from StackOverflow dumps. I went to the meetup and gave a brief description about it. Several people were interested to work. I helped them a bit on getting started and the issues to solve. By the end of event, I got one pull request which fixed a typo in setup file. Few people helped in adding couple of utility functions for the project. A couple of them were interested to continue working on with it in the coming weekends. In the end PSSI sponsored T-shirt :) Here are a few snapshots of event. -
A quick introduction to Docker containers for Django web developers
Docker is a platform for running applications in isolation. Using Linux containers makes the software layer isolated from the base system. The overhead needed for hardware virtualization used by for example Virtualbox is removed. Docker can help in development and deployment of web based applications and services - let us see how and when. -
I did an Aú Batido in 2014, now what?
This is my summary of my resolutions for 2014 and an early pass at my resolutions for 2015. I'm doing this right now instead of at the end of the year because as of the afternoon of December 21, I'm going off the grid. Resolutions Accomplished in 2014 Release the second edition of Two Scoops of Django. Visited South America. Visited two new nations, Argentina and Brazil. Went back to the East Coast to visit Philadelphia and places in New Jersey. Went back to the Philippines. Took some awesome road trips around the USA. My favorite was driving up the Pacific Coast Highway. Took a fun class with Audrey. We did woodshop! Learned how to do Aú Batido! General Accomplishments Published Two Scoops of Django 1.6 with my awesome wife, Audrey Roy Greenfeld. Experienced my first major surgery. Started working at Eventbrite. Found a Inland Empire Capoeira group but still remained friends with Capoeira Batuque. Gave talks at: Wharton School of Business Web Conference PyDay Mendoza Python Brazil PyCon Argentina Experienced the unbelievably tasty asado (steak) of Argentina. Participated in organizing the first Django Bar Camp. Played in 2 Capoeira Rodas in Brazil. Participated in the successful effort to reboot … -
I did an Aú Batido in 2014, now what?
This is my summary of my resolutions for 2014 and an early pass at my resolutions for 2015. I'm doing this right now instead of at the end of the year because as of the afternoon of December 21, I'm going off the grid. Resolutions Accomplished in 2014 Release the second edition of Two Scoops of Django. Visited South America. Visited two new nations, Argentina and Brazil. Went back to the East Coast to visit Philadelphia and places in New Jersey. Went back to the Philippines. Took some awesome road trips around the USA. My favorite was driving up the Pacific Coast Highway. Took a fun class with Audrey. We did woodshop! Learned how to do Aú Batido! General Accomplishments Published Two Scoops of Django 1.6 with my awesome wife, Audrey Roy Greenfeld. Experienced my first major surgery. Started working at Eventbrite. Found a Inland Empire Capoeira group but still remained friends with Capoeira Batuque. Gave talks at: Wharton School of Business Web Conference PyDay Mendoza Python Brazil PyCon Argentina Experienced the unbelievably tasty asado (steak) of Argentina. Participated in organizing the first Django Bar Camp. Played in 2 Capoeira Rodas in Brazil. Participated in the effort to reboot LA … -
Server migration for BotBot.me
End of last week vitaly and I launched a redesign of BotBot.me user account. You can now suport us by subscribing to it for 3$/month and even log personal channel for 2$/month. If you are curious check it out here I would like to share with you the scenario & tactics we used to do this with least service interruption. BotBot.me receives less traffic than most our customers site but it collects hundreds of messages per minutes which lead to interesting challenges. As part of this deployment we were also changing cloud provider. So we needed to move all the underlying infrastructure. Here it is the main services constituing BotBot.me: redis postgresql go IRC client python plugins django web interface We use slatstack to configure our servers. I am not going to present the detailed configuration of each pieces but instead explain the strategy we followed. Reverse Proxy From now on legacy_server is the legacy server and new_server the new server. On this project we use nginx to do the TLS termination, serving static assets and finally reverse proxying the django app. We use autossh to create ssh tunnel from new_server to legacy_server. Here it is an example for redis: … -
Quick Introduction to Mock
Quick Introduction to Mock