Django community: RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
Getting Started with Django Admin
The admin is a very useful tool for developers to use when working with django. It is super easy to setup, and get your models registered with it so you can start CRUDing data.Watch Now... -
Making Clean Code a Part of Your Build Process (And More!)
At Caktus, “clean” (in addition to “working”!) code is an important part of our delivery. For all new projects, we achieve that by using flake8. flake8 is a wrapper around several tools: pep8, pyflakes, and McCabe. pep8 checks to make sure your code matches the PEP 0008 style guidelines, pyflakes looks for a few additional things like unused imports or variables, and McCabe raises warnings about overly complex sections of code. -
AWS load balancers with Django
We recently had occasion to reconfigure some of our existing servers to use Amazon Web Services Elastic Load Balancers in front of them. Setting this up isn’t hard, exactly, but there are a lot of moving parts that have to mesh correctly before things start to work, so I thought I’d write down what we did. -
Check out PyCon PL 2015 agenda
The PyCon PL 2015 will be held in Hotel Ossa Congress & Spa located in Ossa on October 15th through October 18th. The conference is held in Poland but consists of two blocks of talks - English and Polish, so non-Polish speaking attendees can benefit from it too. The agenda and other details can be found on the conference website. -
Working with Environment Variables in Python
If you look at modern code deployment practices, like in the 12 factor app, environment variables are very important for keeping secret information secret, or server specific information set for that server. All while not having long crazy settings in a settings file for every single server and computer. It helps keep things slimmer in your code along with the following quoted from the 12factorapp site: * Resource handles to the database, Memcached, and other backing services * Credentials to external services such as Amazon S3 or Twitter * Per-deploy values such as the canonical hostname for the deploy What Are Environment Variables? Environment variables are a key value pairs that can affect how a program runs. They need to be set at some point before a process is run so the process can read them in and act accordingly. A lot of times in production environments your database name and password are set as environment variables so that information does not end up in a code repository somewhere. By relying on an environment variable your code doesn't care what a setting is because you can set that at some other point. Actually working with environment variables is really simple. … -
Announcing the Caktus Open Source Fellowship
We are excited to announce the creation and funding of a pilot program for open source contributions here at Caktus Group. This program is inspired by the Django Software Foundation’s fellowship as well as the Two Day Manifesto. For this program, Caktus seeks to hire a part-time developer for twelve weeks this fall for the sole purpose of contributing back to open source projects. Caktus builds web applications based on open source tools and the continued growth of these projects is important to us. Open source projects such as Python and Django have given so much to this company and this is one of many ways we are trying to give back. -
Template fragment caching gotchas
Variables in cached template fragment Assuming this is in template. {% cache 300 nums %} {% for i in nums %} <p>i</p> {% endfor %} {% endcache %} And assuming we send {'nums': range(100)} from context, then 0 to 99 will be sent in the response. Now suppose we change context to {'nums': range(1000)}, still for next 5 minutes i.e until the cache expires, 0 to 99 will be sent in the response. 0 to 999 will not be sent in the response. To fix this, we should use the variable too with the {% cache %} tag. So correct code would be {% cache 300 nums_cache nums %} {% for i in nums %} <p>i</p> {% endfor %} {% endcache %} After this whenever context nums changes, cache would be reevaluated. Boolean variable in cached template fragment Assuming template contains {% cache 300 hello %} {% if hello %} <p>Hello</p> {% endif %} {% endcache %} and assuming {'hello': True} is sent in context. Then <p>Hello</p> will be sent in response. Now even when we send {'hello': False} in context, "<p>Hello</p>" will still be sent in response because it's already cached. To fix this. {% cache 300 hello_cache hello … -
Announcing Django Girls RDU: Free Coding Workshop for Women
We’re incredibly excited to announce the launch of Django Girls RDU, a group in NC’s Triangle region that hosts free one-day Django coding workshops for women. Django Girls is part of an international movement that’s helped 1,600 (and counting!) women learn how to code. -
[Django Tips] Use for ... empty in Django templates.
It is quite common to send a query set from a view to template, loop over it and display items in it. If no items are there, it is good if we display some message. For that we can do something like this.{% if books %} {% for book in books %} <li>{{ book }}</li> {% endfor %}{% else %} <li>Sorry, there are no books.</li>{% endif %}Django {% for %} tag takes an optional {% empty %} clause which will be rendered when queryset is empty. So we can rewrite the above code as{% for book in books %} <li>{{ book }}</li>{% empty %} <li>Sorry, there are no books.</li>{% endfor %}This code is much cleaner than previous one. I tried to profile both code blocks. The first block took 0.9ms and second block took 0.8ms which means it is 11% faster than previous code :)Reference: Django docsRead more articles about Python!Read more articles about Django! -
Django Tips & Tricks #5 - Use for...empty in Django templates
It is quite common to send a query set from a view to template, loop over it and display items in it. If no items are there, it is good if we display some message. For that we can do something like this.{% if books %} {% for book in books %} <li>{{ book }}</li> {% endfor %}{% else %} <li>Sorry, there are no books.</li>{% endif %}Django {% for %} tag takes an optional {% empty %} clause which will be rendered when queryset is empty. So we can rewrite the above code as{% for book in books %} <li>{{ book }}</li>{% empty %} <li>Sorry, there are no books.</li>{% endfor %}This code is much cleaner than previous one. I tried to profile both code blocks. The first block took 0.9ms and second block took 0.8ms which means it is 11% faster than previous code :)Reference: Django docs -
Django Tips & Tricks #5 - Use for...empty in Django templates
It is quite common to send a query set from a view to template, loop over it and display items in it. If no items are there, it is good if we display some message. For that we can do something like this.{% if books %} {% for book in books %} <li>{{ book }}</li> {% endfor %}{% else %} <li>Sorry, there are no books.</li>{% endif %}Django {% for %} tag takes an optional {% empty %} clause which will be rendered when queryset is empty. So we can rewrite the above code as{% for book in books %} <li>{{ book }}</li>{% empty %} <li>Sorry, there are no books.</li>{% endfor %}This code is much cleaner than previous one. I tried to profile both code blocks. The first block took 0.9ms and second block took 0.8ms which means it is 11% faster than previous code :)Reference: Django docs -
Using Unsaved Related Models for Sample Data in Django 1.8
Note: In between the time I originally wrote this post and it getting published, a ticket and pull request were opened in Django to remove allow_unsaved_instance_assignment and move validation to the model save() method, which makes much more sense anyways. It's likely this will even be backported to Django 1.8.4. So, if you're using a version of Django that doesn't require this, hopefully you'll never stumble across this post in the first place! If this is still an issue for you, here's the original post: -
PyCon 2015 Workshop Video: Building SMS Applications with Django
As proud sponsors of PyCon, we hosted a one and a half hour free workshop. We see the workshops as a wonderful opportunity to share some practical, hands-on experience in our area of expertise: building applications in Django. In addition, it’s a way to give back to the open source community. -
Reviews of two recent Django Books
Introduction When I started building sites in Django, I learned the basics from the excellent Django tutorial. But I had to learn by trial and error which approaches to using Django’s building blocks worked well and which approaches tended to cause problems later on. I looked for more intermediate-level documentation, but beyond James Bennett’s Practical Django Projects and our Karen Tracey’s Django 1.1 Testing and Debugging, there wasn’t much to be found. -
Django and Python 3 How to Setup pyenv for Multiple Pythons
We need to be doing Django development in Python 3. Unfortunately, we have a lot of projects still in Python 2.7 so switching between the 2 versions can be frustrating. Fortunately pyenv takes the guess work out of switching, and makes it super simple.Watch Now... -
Testing Django Views Without Using the Test Client
Testing Django Views Without Using the Test Client -
[Django Tips] Make Django Development Server Persistent.
I use Emacs for writing Python code. I also use real-auto-save to save my files after 10 seconds of inactivity.While coding, when I stop writing in the middle, after 10 seconds Emacs auto saves the file. Django recognizes this & reloads the development server.Once I complete writing code, I go to browser & refresh the page. Since the code I was writing was incomplete somewhere in the middle and had some errors, Django development server was stopped and page won't reload. Now I have to go back to terminal and restart the server and again go back to browser to refresh page.To overcome this, Django server must be made persistent. The easiest way to accomplish this is to use a simple bash script as described here.$ while true; do python manage.py runserver; sleep 4; doneWhen Django development server stops, after 4 seconds it tries to start automatically and goes on forever.Instead of typing that everytime, it better to write this as a shell script and put it in system path, so that it can be used in any project.while true; do echo "Re-starting Django runserver" python manage.py runserver sleep 4doneSave this & make it executable(chmod +x), use it is ./filename.sh and to stop … -
The uWSGI Swiss Army Knife
uWSGI is one of those interesting projects that keeps adding features with every new release without becoming totally bloated, slow, and/or unstable. In this post, we'll look at some of its lesser used features and how you might use them to simplify your Python web service. Let's start by looking at a common Python web project's deployment stack. Nginx: Static file serving, SSL termination, reverse proxy Memcached: Caching Celery: Background task runner Redis or RabbitMQ: Queue for Celery uWSGI: Python WSGI server Five services. That's a lot of machinery to run for a basic site. Let's see how uWSGI can help you simplify things: Static File Serving uWSGI can serve static files quite efficiently. It can even do so without tying up the same worker/thread pool your application uses thanks to it's offloading subsystem. There are a bunch of configuration options around static files, but the common ones we use are: offload-threads the number of threads to dedicate to serving static files check-static this works like Nginx's @tryfiles directive, checking for the existence of a static file before hitting the Python application static-map does the same, but only when a URL pattern is matched Other options exist to allow you … -
Django Tips & Tricks #4 - Make Django Development Server Persistent
I use Emacs for writing Python code. I also use real-auto-save to save my files after 10 seconds of inactivity.While coding, when I stop writing in the middle, after 10 seconds Emacs auto saves the file. Django recognizes this & reloads the development server.Once I complete writing code, I go to browser & refresh the page. Since the code I was writing was incomplete somewhere in the middle and had some errors, Django development server was stopped and page won't reload. Now I have to go back to terminal and restart the server and again go back to browser to refresh page.To overcome this, Django server must be made persistent. The easiest way to accomplish this is to use a simple bash script as described here.$ while true; do python manage.py runserver; sleep 4; doneWhen Django development server stops, after 4 seconds it tries to start automatically and goes on forever.Instead of typing that everytime, it better to write this as a shell script and put it in system path, so that it can be used in any project.while true; do echo "Re-starting Django runserver" python manage.py runserver sleep 4doneSave this & make it executable(chmod +x), use it is ./filename.sh … -
Django Tips & Tricks #4 - Make Django Development Server Persistent
I use Emacs for writing Python code. I also use real-auto-save to save my files after 10 seconds of inactivity.While coding, when I stop writing in the middle, after 10 seconds Emacs auto saves the file. Django recognizes this & reloads the development server.Once I complete writing code, I go to browser & refresh the page. Since the code I was writing was incomplete somewhere in the middle and had some errors, Django development server was stopped and page won't reload. Now I have to go back to terminal and restart the server and again go back to browser to refresh page.To overcome this, Django server must be made persistent. The easiest way to accomplish this is to use a simple bash script as described here.$ while true; do python manage.py runserver; sleep 4; doneWhen Django development server stops, after 4 seconds it tries to start automatically and goes on forever.Instead of typing that everytime, it better to write this as a shell script and put it in system path, so that it can be used in any project.while true; do echo "Re-starting Django runserver" python manage.py runserver sleep 4doneSave this & make it executable(chmod +x), use it is ./filename.sh … -
Understanding Django Middlewares
I assume you have read official Django docs on middleware. I will elaborate on things mentioned in the documentation but I assume you are familiar with basics of middleware. In this post we will discuss the following. What is a middleware When to use middleware Things to remember when writing middleware Writing some middlewares to understand how order of middleware matters What is a middleware Middlewares are hooks to modify Django request or response object. Putting the definition of middleware from Django docs. Middleware is a framework of hooks into Django’s request/response processing. It’s a light, low-level “plugin” system for globally altering Django’s input or output. When to use middleware You can use middleware if you want to modify the request i.e HttpRequest object which is sent to the view. Or you might want to modify the HttpResponse object returned from the view. Both these can be achieved by using a middleware. You might want to perform an operation before the view executes. In such case you would use a middleware. Django provides some default middleware. eg: AuthenticationMiddleware Very often you would have used request.user inside the view. Django wants user attribute to be set on request before any view … -
Profiling Django Middlewares
I assume you have a basic understanding of Profiling, what it means and why we use it. Why this post Recently I had to profile a Django application which wasn't performing as fast as it should. This application had several custom middlewares too. So it was possible that custom middlewares were the cause of slow performance. There are some existing Django libraries to profile Django code. eg: Django Debug Toolbar, django-silk , django_cprofile etc. Most of them can profile view code well but they can't profile other middlewares. I wanted a way to profile middlewares too. Problem with Django Debug Toolbar I assume you understand middlewares and how the order in which middlewares are defined matter. If you want to get more idea about middlewares, this post might help. Django debug toolbar is probably designed for profiling the views. It uses process_view() and returns an HttpResponse instace from process_view(). process_request() of all middlewares run before any middleware's process_view(). So using Django debug toolbar, it's not possible to profile what's going on inside process_request() of different middlewares. And since process_view() of debug toolbar returns HttpResponse, process_view() of other middlewares is bypassed and so we can't profile process_view() of other middlewares. So … -
How to access your Django site from a different computer
Hey guys! Today we'll see how to access your Django site from another computer. Now this won't be a problem if you've hosted on a remote server like PythonAnywhere or Heroku. But what if you have hosted it on your local machine and want to provide people with access to it?I'm developing an online mentorship platform for my college that is hosted on the college server. So for that I had to use this concept. Before you do this, however, here's a disclaimer: only computers connected in the same network as your local machine can access your Django site. You can, of course, provide global access, but there's a lot of complicated networking stuff involved in that. Let's not go into that right now.So let's get on with it. You know the command for hosting and accessing the site on your local machine is python manage.py runserver. This essentially gives your local machine server capabilities and lets you access it from the same machine. However, to let other machines access it, here's the command:python manage.py runserver <ip_address>:<port_number>Replace <ip_address> with your machine's IP address. If you don't know the IP address, do one of the following:Windows: Open command prompt and type ipconfig. … -
Django Birthday: Recap
Django Birthday: Recap -
Q3 2015 ShipIt Day ReCap
Last Friday marked another ShipIt Day at Caktus, a chance for our employees to set aside client work for experimentation and personal development. It’s always a wonderful chance for our developers to test new boundaries, learn new skills and sometimes even build something entirely new in a single day.