Django community: RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
Advanced usage of Python requests - timeouts, retries, hooks
The Python HTTP library [requests](https://requests.readthedocs.io/en/master/) is probably my favourite HTTP utility in all the languages I program in. It's simple, intuitive and ubiquitous in the Python community. Most of the programs that interface with HTTP use either requests or urllib3 from the standard library. While it's easy to immediately be productive with requests because of the simple API, the library also offers extensibility for advanced use cases. If you're writing an API-heavy client or a web scraper you'll probably need tolerance for network failures, helpful debugging traces and syntactic sugar. Below is a summary of features I've found useful in requests when writing web scraping tools or programs that extensively use JSON API's. [TOC] ## Request hooks Often when using a third party API you want to verify that the returned response is indeed valid. Requests offers the shorthand helper `raise_for_status()` which asserts that the response HTTP status code is not a 4xx or a 5xx, i.e that the request didn't result in a client or a server error. For example ```python response = requests.get('https://api.github.com/user/repos?page=1') # Assert that there were no errors response.raise_for_status() ``` This can get repetitive if you need to `raise_for_status()` for each call. Luckily the requests library … -
Django & Healthcare - Jacinda Shelly
Weekly DjangoChat NewsletterDoctor on DemandApero HealthDjangoCon US 2014 - Connecting Patients to Doctors in Real-Time Using DjangoPyCon 2019 - But, Why is the (Django) Admin Slow?PyCon 2019 - Hands-On Web Application Security with DjangoDjango Forum - Top 5 3rd party packagesqrDjangoCon 2017 - Programming Post-Progeny -
A Week At A Time - Building SaaS #46
In this episode, we worked on a weekly view for the Django app. We made navigation that would let users click from one week to the next, then fixed up the view to pull time from that particular week. The first thing that I did was focus on the UI required to navigate to a new weekly view in the app. We mocked out the UI and talked briefly about the flexbox layout that is available to modern browsers. -
Dockerizing Flask with Postgres, Gunicorn, and Nginx
This tutorial details how to configure Flask to run on Docker along with Postgres, Nginx, and Gunicorn. -
How to Disallow Auto-named Django Migrations
When you run Django’s manage.py makemigrations, it will try to generate a name for the migration based upon its contents. For example, if you are adding a single field, it will call the migration 0002_mymodel_myfield.py. However when your migration contains more than one step, it instead uses a simple ‘auto’ name with the current date + time, e.g. 0002_auto_20200113_1837.py. Naming things is a known hard problem in programming. Having migrations with these automatic names makes managing them harder. In the worst case, their similarity could lead to data loss when trying to roll back in an emergency. It’s also all too easy to forget to fix the name and commit since Django doesn’t prompt you for a better name. We can guard against this with some automation! The below custom system check will ensure you don’t commit such migrations to your project. I’ve replicated in a couple projects so figure it’s worth sharing the code, but it’s a bit small to publish as a package. Let’s look at adding it, as per the guide on custom system checks. To add it your project, you’ll first want to add it to a module inside one of your apps. I normally write … -
Python in Production
I’m missing a key part from the public Python discourse and I would like to help to change that. -
Django News - Issue 11 - Feb 21st 2020
News Wagtail 2.8 is released! In case you missed it, Wagtail 2.8 was released and adds Django 3.0 support while dropping Django 2.0 support plus a bunch of new features and bugfixes. github.com 🚨 Docker/Postgres now requires a username & password In the latest version of Docker, Postgres now requires either a username & password to be set, or use another setting to allow non-pw access. This affects all Postgres versions for Docker. This is better for security, but frustrating that Docker made such a major change in this quiet way. It breaks many existing builds. github.com Articles How to cheat at unit tests with pytest and Black From Django co-creator Simon Willison, a clever way to combine pytest with the Black Python formatter. simonwillison.net A Tip About DRF Permissions Use permission classes and operators to simplify your Django REST Framework permissions. revsys.com Automating Performance Testing in Django A guide to testing--and avoiding--N+1 queries. testdriven.io How to add a robots.txt file to your Django site Add a testable robots.txt file to your Django site. adamj.eu Alpine makes Python Docker builds 50× slower, and images 2× larger Do you work with Docker? An interesting article on the alpine build many of … -
Templates and Logic - Building SaaS #45
In this episode, we added content to a template and talked about the N+1 query bug. I also worked tricky logic involving date handling. The first change was to update a course page to include a new icon for any course task that should be graded. After adding this, we hit an N+1 query bug, which is a performance bug that happens when code queries a database in a loop. We talked about why this happens and how to fix it. -
Getting data to home page with Django
In this post I show how to use get_context_data(). You might need it to display 'latest posts' on your 'home page'. -
Getting data to home page with Django
In this post I show how to use get_context_data(). You might need it to display 'latest posts' on your 'home page'. -
Thanks, Lukas! This is a really helpful article!
Thanks, Lukas!This is a really helpful article! -
Django Software Foundation
Weekly DjangoChat NewsletterDjango Software FoundationSponsorship & GitHub SponsorsHow Django Works Behind the ScenesDjango MerchandiseDSF Individual Membersdjangosnippets.orgjazzbandDjango PeopleDjango Forum -
The Innovation/Execution Spectrum
Lately I’ve been working with our startups to establish their engineering strategies. One model I’ve found useful is to place their technical challenge on an innovation/execution spectrum. -
Views On Views
In the previous Understand Django article, I covered URLs and the variety of tools that Django gives us to describe the outside interface to the internet for your project. In this article, we’ll examine the core building block that makes those URLs work: the Django view. What Is A View? A view is a chunk of code that receives an HTTP request and returns an HTTP response. Views describe Django’s entire purpose: to respond to requests made to an application on the internet. -
Safely Including Data for JavaScript in a Django Template
Django templates are often used to pass data to JavaScript code. Unfortunately, if implemented incorrectly, this opens up the possibility of HTML injection, and thus XSS (Cross-Site Scripting) attacks. This is one of the most common security problems I’ve encountered on Django projects. In fact I’ve probably seen it on every considerably-sized Django project, in some form or another. Also, not naming and shaming, but I’ve also seen it in lots of community resources. This includes conference talks, blog posts, and Stack Overflow answers. It’s hard to get right! It’s also been historically difficult, since it’s only Django 2.1 that added the json_script template tag to do this securely. (And the ticket was open six years!) Let’s look the problem and how we can fix it with json_script. The Vulnerable Way Let’s take this view: from django.shortcuts import render def index(request): mydata = get_mydata() return render(request, 'index.html', context={"mydata": mydata}) …and this template: <script> const mydata = "{{ mydata|safe }}"; </script> Unfortunately as written, the template is open to HTML injection. This is because if the data contains </script> anywhere, the rest of the result will be parsed as extra HTML. We call this HTML injection, and attackers can use it … -
How to search in a huge table on Django admin
Hello everyone! We all know that the Django admin is a super cool tool for Django. You can check your models, and add/edit/delete records from the tables. If you are familiar with Django, I’m sure you already know about it. I was given a task: Our client wanted to search in a table by one field. It seems easy enough, right? Well, the tricky part is that the table has 523.803.417 records. Wow. 523.803.417 records. At least the model was not that complex: On models.py: class HugeTable(models.Model): """Huge table information""" search_field = models.CharField(max_length=10, db_index=True, unique=True) is_valid = models.BooleanField(default=True) def __str__(self): return self.search_field So for Django admin, it should be a breeze, right? WRONG. The process First, I just added the search field on the admin.py: On admin.py: class HugeTableAdmin(admin.ModelAdmin): search_fields = ('search_field', ) admin.site.register(HugeTable, HugeTableAdmin) And it worked! I had a functioning search field on my admin. Only one problem: It took 3mins+ to load the page and 5mins+ to search. But at least it was working, right? WTF? First, let’s split the issues: Why was it taking +3mins just to load the page? Why was it taking +5mins to search if the search field was indexed? I started tackling … -
Django News - Issue 10 - Feb 14th 2020
News Sponsor Django on Github You can now sponsor Django directly via GitHub Sponsors. github.com DjangoCon Europe 2020 - Call for Proposals The deadline is February 29th so get yours in soon! djangocon.eu Articles Understand Group by in Django with SQL If you have ever struggled with the Django ORM's Aggregate features, then check out this side-by-side comparison/cheatsheet of SQL to Querysets. hakibenita.com Custom Exceptions in Django REST Framework Lacey Williams Henschel shows you how to handle cleaner DRF exceptions. revsys.com Moving to Django 3.0's Field.choices Enumeration Types Django 3.0 added a new powerful way of handle Enumerations for model field choices. Check out this article for a nicer way to use them. adamj.eu Sponsored Link Django Crash Course: Covers Python 3.8 and Django 3.x - Alpha Version The authors of Two Scops of Django have released their latest book, the Django Crash Course. Based on their corporate training, they are offering the book for under US$20, which is a steal considering they normally charge $3000/person for in-person training. Available in E-Book format, the paperback, coil-bound, and hardcover versions are available for pre-order. roygreenfeld.com Podcasts Django Chat #50 - Technical Writing with Mikey Ariel Mikey is a technical writer at … -
Python Packaging Metadata
Since this topic keeps coming up, I’d like to briefly share my thoughts on Python package metadata because it’s – as always – more complex than it seems. When I say metadata I mean mostly the version so I will talk about it interchangeably. But the description, the license, or the project URL are also part of the game. The overarching problem is that we have two places where we may need that metadata: -
Technical Writing - Mikey Ariel
Weekly DjangoChat NewsletterMikey Ariel personal siteWrite the DocsDjangoCon Europe 2019 - Docs or it didn’t happenLearnDjango.com -
Guide to Unit Testing Vue Components
This article serves as a guide for unit testing Vue components. -
Building an IVR System with Python, Django and Twilio
Last year my team and I worked on a very challenging IVR system. After almost a year in production and thousands of processed transactions, I teamed up with the great people over at the Twilio blog to write an introductory tutorial for developing IVR systems using Django and Twilio IVR. Aside from "making your server talk" and diving into the cool speech features, I found the most challenging part working on IVR is designing the views. Unlike APIs and Forms, IVR is very limited in the type of input it takes (DTMF tones, transcribed speech), and the amount of data it can communicate and process is limited. Read "Building an Interactive Voice Response (IVR) System with Python, Django and Twilio" on the Twilio blog ≫ Building an IVR System with Django and Twilio -
Storing Django Static and Media Files on Amazon S3
This tutorial shows how to configure Django to load and serve up static and media files, public and private, via an Amazon S3 bucket. -
Our New Django Book Has Launched!
Audrey and I wrote a new book titled Django Crash Course. You can get it right now on our website at roygreenfeld.com/products/django-crash-course. Right now it's in alpha, which means only the e-book is available. Later we'll produce it in print formats (perfect bound, spiral, and hardcover). As the book is in alpha, you're encouraged to submit bug reports to us for errors that you find. In turn we will give you credit for your contributions in not just the e-book, but also in the print paperback and online publicly on the web. This is your opportunity to have your name in one of our books as a contributor, which you are then welcome to add to your resume and LinkedIn profile. We followed the same pattern with our Two Scoops of Django books. Check it out! Django Crash Course is designed to build solid foundations for any developer looking to get quickly and solidly proficient with Django 3. Once you've finished the book, you'll be able to purchase Django Crash Course extensions on topics such as deployment on various platforms, Django REST Framework (DRF), Javascript frameworks like VueJS and/or React, third-party packages, and more. Some of My Favorite Features Friendly … -
Understand Group by in Django with SQL
Aggregation is a source of confusion in any type of ORM and Django is no different. The documentation provides a variety of examples and cheat-sheets that demonstrate how to group and aggregate data using the ORM, but I decided to approach this from a different angle. In this article I put QuerySets and SQL side by side. If SQL is where you are most comfortable, this is the Django GROUP BY cheat-sheet for you. Image by Jason Leung Table of Contents How to Group By in Django How to Count Rows How to Use Aggregate Functions How to Group By How to Filter a QuerySet With Group By How to Sort a QuerySet With Group By How to Combine Multiple Aggregations How to Group by Multiple Fields How to Group by an Expression How to Use Conditional Aggregation How to Use Having How to Group by Distinct How to Create Expressions Using Aggregate Fields How to Group By Across Relations How to Group By a Many to Many Relationship Going Further .side-by-side { display: flex; } .side-by-side .highlight { flex-grow: 1; flex-shrink: 0; width: 50%; } .side-by-side .highlight pre { height: 100%; } .side-by-side .highlight:first-child:after { content: " "; position: … -
How to add a robots.txt to your Django site
robots.txt is a standard file to communicate to “robot” crawlers, such as Google’s Googlebot, which pages they should not crawl. You serve it on your site at the root URL /robots.txt, for example https://example.com/robots.txt. To add such a file to a Django application, you have a few options. You could serve it from a web server outside your application, such as nginx. The downside of this approach is that if you move your application to a different web server, you’ll need to redo that configuration. Also you might be tracking your application code in Git, but not your web server configuration, and it’s best to track changes to your robots rules. The approach I favour is serving it as a normal URL from within Django. It becomes another view that you can test and update over time. Here are a couple of approaches to do that. With a Template This is the easiest approach. It keeps the robots.txt file in a template and simply renders it at the URL. First, add a new template called robots.txt in your root templates directory, or in your “core” app’s templates directory: User-Agent: * Disallow: /private/ Disallow: /junk/ Second, add a urlconf entry: from …