Django community: Community blog posts RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
Djangocon: building real-time apps with django - Iacopo Spalletti
(One of my summaries of a talk at the 2018 european djangocon.) The web isn't a pure request/response web anymore. There's real-time web now. There are lots of tools (yeah!), but you also get lots of complexity (booo!). Django channels is a framework to use django in the non-http world. Websockets, but not only those. Channels (version 1) is out now for three years, version 2 came out a few months ago. The talk assumes version 2. Some concepts: Asynchronous. For us, this means event-driven. A problem is that asynchronous code is normally harder to understand because of its non-linear nature. But... channels hides most of the complexity for you. ASGI. A protocol spec (like WSGI), independent of channels. It is ASGI all the way down. You can nest them. Protocol server. This implements the ASGI spec for a specific protocol. It interacts with the connection. It bridges the network and the application. For channels, this often is 'daphne'. Routing. Maps incoming messages to consumers. It is nestable and composable. Scope. A "scope" is created for every connection. It holds the data of the connection. Channel. IPC mechanism ("Inter Process Communication"). FIFO "at-most-once" queue. Consumers. The core abstraction to build … -
Djangocon: can packaging improve django deployments? - Markus Zapke-Gründemann
(One of my summaries of a talk at the 2018 european djangocon.) Markus started created websites at the end of the 1990s. He calls himself an open source software developers. Open source is how he learned programming, so he likes giving back. How can packaging make deployments easier, faster and more reliable? He showed a django project structure, modified for packaging it with python. Extra files are MANIFEST.in, setup.cfg, setup.py. And the actual project code is in a subdirectory, as that's what setup.py likes. If you have apps, he suggests to put them in an yourproject/apps/ subdirectory. Many people use requirements.txt files to list their dependencies. They only use setup.py for libraries or apps that they publish. But why not use the setup.py also for projects? There's an install_requires key you can use for mentioning your dependencies. Note that if your setuptools is new enough, you can put everything into setup.cfg instead of having it all in a weird function in setup.py. This includes the dependencies. Your setup.py will now look like this: from setuptools import setup setup() As we're deploying a project (instead of a library), we can even pin the requirements to specific versions. He mentioned bumpversion to … -
Djangocon: representing hierarchies in relational databases - Jacob Rief
(One of my summaries of a talk at the 2018 european djangocon.) Jacob works with django since 2011 and he created and maintains a couple of django apps. One of the projects he took over maintenance of is django-treebeard. django-treebeard can be used to create hierarchical structures in your relational database. A tree structure. Like the structure of a company. Or nested categories in a shopping website. There are four common solutions to get a hierarchical relation in your database: Adjecent list. Basically you add a parent foreign key to your model. Traversing the tree has bad performance, as you have to recurse through every "level" to find all parents or children of a node. It gets worse if you also want to filter. You can Materialized path tree. You give every node a special "path" field. The first element is A, its children are AA, AB, AC. One level down AAA, AAB, ABA, ABB, ABC, etc. Now if you want to find the children of 'AAB', you search for all items with a path that starts with 'AAB'. (You have to exclude the current node, though). As long as you have a good index, the performance is good. If … -
Djangocon: creating solid APIs - Rivo Laks
(One of my summaries of a talk at the 2018 european djangocon.) Our applications are increasingly also used by other computers. But the APIs they use are ironically aimed at humans: at the programmers that have to program on your API. "API" means "application programming interface", but it might be better to say "application programmer interface" as you really have to focus on the programmer that has to do the programming! What makes an API good? Documentation. Familiarity. Lack of friction. Documentation This is often overlooked. But it is the first point of contact of your API, so it gives the first impression. An important deciding factor. It takes lots of work to write good documentation, but the effort is worth it. Look at your documentation as a "sales page" for your API. What do you want on your sales page? Here are some suggestions: How do I access it? Do I need authentication? How? What is the root URL? General info on encodings and formats. Make it explicit. Pagination. Document the common errors (error codes) you can get. Code for getting started. It gives your users a warm feeling if their first try works right away. When you document … -
Djangocon: making smarter queries with advanced ORM resources - Mariana Bedran Lesche
(One of my summaries of a talk at the 2018 european djangocon.) There are details in django's ORM/model/query documentation that you often skip over. But there are very useful things in there. Recently she had to work on an old PHP project where she had to write SQL by hand. And some of those raw SQL statements, she couldn't get working right away with django. So she went back to the harder sections of the documentation. When should you use the advanced methods? For instance when the regular optimizations don't give enough results: Good indexes on your tables. Paginated requests. select_related, prefetch_related are there. You limit the output with values, values_list, only and defer. You use a database cache. You use the assertNumQueries on tests to figure out if too many queries are made. Mariana comes from Brazil, where they have a law that says that all non-secret goverment information should be made available. There's the https://brasil.io project that tries to make the data available in useful datasets (as the source data isn't always in a useful format). That's where she got her sample data from: info on companies and their owners. Owners can be regular persons, other companies or … -
Djangocon: accessibility matters: building a better web - Lindsey Dragun
(One of my summaries of a talk at the 2018 european djangocon.) Lindsey runs the DisabledInTech slack channel. For Web accessibility, see https://www.w3.org/WAI/fundamentals/accessibility-intro/ as a good intro. There are standards to help with accessibility and for many government instances, they're mandatory. Disabilities are often grouped in: visual, auditory, motor/physical, cognitive/neurological, language/speech. There are other (more inclusive!) ways to group: permanent (born blind), temporary (like just being sick!), acquired (like ageing), societal (like lefthandedness). Notice how this comes much closer? There are alternative input/output tools. Braille displays, eye trackers, foot pedals instead of mouse buttons, etc. Important: think deeper. Categories can be desceptively simple. Users don't always fall into just one category. Examples. The web is sometimes like the letters at the eye doctor: "read the line with the smallest letters". Color contrast. il1: what is the I, what the L, what the number? Dislexia? Bad font? Make changes noticable. Feedback in forms, for instance. Which field contained the error? The above list might make it look like accessibility is easy. It often isn't. How do I implement accessibility? Avoid gimmicks. What's popular on the web isn't necessarily handy. Infinite scrolling in combination with a sidebar. When you scroll, you'll never … -
Djangocon: lightning talks wednesday
(One of my summaries of a talk at the 2018 european djangocon.) Lightning talks are quick 5 minute talks, so I won't have every detail and speaker name correctly :-) Adding multi-factor authentication to your django project - Justin Mayer Passwords are ubiquitous. Passwords are terrible. Solutions: sms (bad), TOTP, one-time passwords. U2F key. That one is great. It will also be a standard in browsers. There's django-u2f to help you with it. He managed to get a demo working within 5 minutes! Instant feedback - Johannes Hopper Making reviews (on pull requests) great again with the new "github checks". What is tedious as a maintainer are the small tasks like "could you remove the trailing spaces". With "github checks", you can let a linter do that job and give the feedback to the user. Such feedback of a program is much less irritating to the person that created the pull requests than when you do it yourself. "Github checks" is a new (beta) feature. It is made for computer feedback. With a colleague, he set up a collection of linters, ready for integration with github. Hosted on AWS. If you're interested, contact him. (It is all open source, of … -
Official Django REST Framework Tutorial - A Beginners Guide
If you have struggled to complete the official tutorial on your own, consider this guide a good place to start instead. -
I'm in Heidelberg for the djangocon 2018 (plus other info)
Yes, I'm in Heidelberg (Germany) for the annual European djangocon. That's not a particularly shocking discovery, as I've been to quite a number :-) There are however some reasons for me to write this entry. The most important reason is that I've set up my old blog software on a brand new laptop. "Old software" means "python 2" and it is "python 3" now. And I've modernized the setup on my server a bit. So: this entry is my first test if my setup also works when writing a blog entry. That's not something I want to test when I'm making summaries tomorrow. New laptop? Two months ago I asked advice: mac/linux. Both had advantages. What I type this on is a linux laptop. A powerful Dell precision 5520! I'll probably write about my new setup later :-) New laptop? Yes, because of a new job. The company is also quite new (from February this year), it doesn't have a website yet: "Triple S transformations". I'd describe my job as senior developer with a big focus on python and open source. Triple S is part of VolkerWessels, the second biggest Dutch construction firm. Our aim is to replace expensive and … -
Using Braze so that marketers are not the developers’ nightmare
Braze is an external platform to send messages like emails, push notifications, in-app, sms, etc. The platform has two ways to send them: through an API or offers an SDK to integrate.You can store in Braze the user and products data that you need and use it in the messages to be sent. The platform offers the possibility to send messages at scale.Marketers can manage messages and their scheduling without relying on programmers.CampaignsMessages are sent through campaigns. Braze provides multiple scheduling options for campaigns. These alternatives allow you to send messages to users at the right time according to the needs.The first option is the Scheduled Delivery. Messages will be sent as soon as the campaign is launched, sending at a designated time (it allows to specify the days and hours the campaign will send the messages) or using the “Intelligent Delivery” (once, daily, weekly, monthly).The second option is Action-Based Delivery. Messages are sent when users perform actions. The trigger events can be starting a session, performing a custom event, performing a campaign’s conversion event, making a purchase, interacting with other campaigns, among others. For this option you can select how long to wait before sending the campaign. The campaign’s … -
Taming Irreversibility with Feature Flags (in python)
Feature Flags are a very simple technique to make features of your application quickly toggleable. The way it works is, everytime we change some behavior in our software, a logical branch is created and this new behavior is only accessible if some specific configuration variable is set or, in certain cases, if the application context respects some -
Best Django Books 2018
List of current Django 2.0 books. -
How To Deploy Django Channels To Production
In this article, we will see how to deploy django channels to production and how we can scale it to handle more load. We will be using nginx as proxy server, daphne as ASGI server, gunicorn as WSGI server and redis for channel back-end. Daphne can serve HTTP requests as well as WebSocket requests. For stability and performance, we will use uwsgi/gunicorn to serve HTTP requests and daphne to serve websocket requests. We will be using systemd to create and manage processes instead of depending on third party process managers like supervisor or circus. We will be using ansible for managing deployments. If you don't want to use ansible, you can just replace template variables in the following files with actual values. Nginx Setup Nginx will be routing requests to WSGI server and ASGI server based on URL. Here is nginx configuration for server. server { listen {{ server_name }}:80; server_name {{ server_name }} www.{{ server_name }}; return 301 https://avilpage.com$request_uri; } server { listen {{ server_name }}:443 ssl; server_name {{ server_name }} www.{{ server_name }}; ssl_certificate /root/certs/avilpage.com.chain.crt; ssl_certificate_key /root/certs/avilpage.com.key; access_log /var/log/nginx/avilpage.com.access.log; error_log /var/log/nginx/avilpage.com.error.log; location / { proxy_pass http://0.0.0.0:8000; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_redirect off; } … -
How To Deploy Django Channels To Production
In this article, we will see how to deploy django channels to production and how we can scale it to handle more load. We will be using nginx as proxy server, daphne as ASGI server, gunicorn as WSGI server and redis for channel back-end. Daphne can serve HTTP requests as well as WebSocket requests. For stability and performance, we will use uwsgi/gunicorn to serve HTTP requests and daphne to serve websocket requests. We will be using systemd to create and manage processes instead of depending on third party process managers like supervisor or circus. We will be using ansible for managing deployments. If you don't want to use ansible, you can just replace template variables in the following files with actual values. Nginx Setup Nginx will be routing requests to WSGI server and ASGI server based on URL. Here is nginx configuration for server. server { listen {{ server_name }}:80; server_name {{ server_name }} www.{{ server_name }}; return 301 https://avilpage.com$request_uri; } server { listen {{ server_name }}:443 ssl; server_name {{ server_name }} www.{{ server_name }}; ssl_certificate /root/certs/avilpage.com.chain.crt; ssl_certificate_key /root/certs/avilpage.com.key; access_log /var/log/nginx/avilpage.com.access.log; error_log /var/log/nginx/avilpage.com.error.log; location / { proxy_pass http://0.0.0.0:8000; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_redirect off; } … -
Debugging Python Applications with pdb
Debugging isn’t a new trick – most developers actively use it in their work. Of course, everyone has their own approach to debugging, but I’ve seen too many specialists try to spot bugs using basic things like print instead of actual debugging tools. Or even if they did use a debugging tool, they only used a small set of features and didn’t dig deeper into the wide range of opportunities good debuggers offer. And which could have saved those specialists a lot of time. Debugging Python with the Print Command As mentioned above, some of us use ‘print’ to display information that reveals what’s going on inside the code. Some of us use a logger for the same purpose – but please don’t confuse this with the logger for the production code, as I’m referring to developers that only add the logger during the problem searching period, i.e. just until the developing process ends. But the truth is, print has a lot of loopholes. Probably, its biggest drawback is that you have to add changes to your code, then re-run the application to see the different variable you’re writing, or the current variable type, for example. So how is a … -
Django Channels 2.0 to Production Environment
Learn how to deploy a Django C... -
New Course: Creating and Distributing Python Packages
Myself and Audrey Roy Greenfeld have released a course, Creating and Distributing Python Packages. Features it includes: Covers Python packaging details including setup.py, not to mention automating things like testing, versioning, documentation, dependency management, and checking for security issues. Available in English or Spanish. Demonstrates Cookiecutter and Cookiecutter PyPackage. Will help fund our open source work in a way that doesn't involve us directly asking people for money (because we suck at that). Who is this course good for? Do you have a script, function, class, or snippet of code you copy/paste from project to project? If that's the case, this course is for you. We'll teach you how to package that up and add automated tests, documentation, and so much more. This especially applies to organizations that share code between projects. If you are copy/pasting common code that means updating it across projects is challenging and error prone, introducing the risk of bugs and security concerns. Plus, odds are it's not tested reliably. By packaging your copy/pasting code and adding automation, it becomes just another dependency, easily updated across multiple projects. Or what if you have an API that needs a Python client SDK? If that's the case, then … -
Always return namespaces in Django REST Framework
By default, when you hook up a model to Django REST Framework and run a query in JSON format, what you get is a list. E.g. For GET localhost:8000/api/mymodel/ [ {"id": 1, "name": "Foo"}, {"id": 2, "name": "Bar"}, {"id": 3, "name": "Baz"} ] This isn't great because there's no good way to include other auxiliary data points that are relevant to this query. In Elasticsearch you get something like this: { "took": 106, "timed_out": false, "_shards": {}, "hits": { "total": 0, "hits": [], "max_score": 1 } } Another key is that perhaps today you can't think of any immediate reason why you want to include some additonal meta data about the query, but perhaps some day you will. The way to solve this in Django REST Framework is to override the list function in your Viewset classes. Before # views.py # views.py from rest_framework import viewsets class BlogpostViewSet(viewsets.ModelViewSet): queryset = Blogpost.objects.all().order_by('date') serializer_class = serializers.BlogpostSerializer After # views.py from rest_framework import viewsets class BlogpostViewSet(viewsets.ModelViewSet): queryset = Blogpost.objects.all().order_by('date') serializer_class = serializers.BlogpostSerializer def list(self, request, *args, **kwargs): response = super().list(request, *args, **kwargs) # Where the magic happens! return response Now, to re-wrap that, the response.data is a OrderedDict which you can change. Here's … -
Creating Dynamic Forms with Django
What is a dynamic form and why would you want one? Usually, you know what a form is going to look like when you build it. You know how many fields it has, what types they are, and how they’re going to be laid out on the page. Most forms you create in a web app are fixed and static, except for the data within the fields. A dynamic form doesn’t always have a fixed number of fields and you don’t know them when you build the form. The user might be adding multiple lines to a form, or even multiple complex parts like a series of dates for an event. These are forms that need to change the number of fields they have at runtime, and they’re harder to build. But the process of making them can be pretty straightforward if you use Django’s form system properly. Django does have a formsets feature to handle multiple forms combined on one page, but that isn’t always a great match and they can be difficult to use at times. We’re going to look at a more straightforward approach here. Creating a dynamic form For our examples, we’re going to let the … -
Setup React
Below is a reference we made t... -
Staging Django for Production & Local Development
## How do you have different s... -
Angular 6|5 Tutorial (with RESTful Django Back-End)
Throughout this Angular 6 tutorial, we'll learn to build a web application with Angular 6, the latest version of Angular--the most popular framework/platform for building mobile and desktop client side applications created and used internally by Google. By the end of this Angular 6 tutorial, you'll learn: how to install the latest version of Angular CLI, how to use the Angular 6 CLI to generate a new Angular 6 project, how to use Angular 6 to build a simple CRM application, what's a component and component-based architecture how to create Angular 6 components, how to add component routing and navigation, how to use HttpClient to consume a REST API etc. The Django Back-End We'll make use of a simple CRM API built with Django and Django REST framework. Since this is an Angular tutorial we'll not focus on building the API as this will be the subject of a separate tutorial but you can grab the source code of the back-end API from this repository You can use the following commands to start the development server: # Clone the project and navigate into it $ git clone https://github.com/techiediaries/django-crm $ cd django-crm # Create a virtual environment and install packages $ … -
Overriding Field Widgets In Django Doesn't Work. Template Not Found. The Solution
One day you may want to override a default Django form input widget. Let's say; you'll want to tweak a Date widget, so it has type="date" on it. So you'd go out and do the regular drill: 1) Find the source file inside the Django dir ... Read now -
2018 life update
“So, what’s new with you?” Um, a lot: Today’s my last day at 18F. It’s been an honor and a privilege to get to work at such an amazing place with such fantastic colleagues. I’ve learned so much. If you have the oportunity to join 18F – take it. It’s truly a unique oportunity. If you’re thinking about joining and want to chat about my experience there, I’d be happy to talk. -
A reading list for new engineering managers
Like many engineers, I got thrown into management without any real guidance. I thought management was just telling people what to do. I thought there wasn’t any real science to it; you just needed to feel your way through it. I was wrong: there’s a whole field of study here, and you can learn a lot by, you know, studying! This is the reading list I wish I’d been given as a new engineering manager.