Django community: RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
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 … -
Django 3 Tutorial & CRUD Example with MySQL and Bootstrap
Django 3 is released with full async support! In this tutorial, we'll see by example how to create a CRUD application from scratch and step by step. We'll see how to configure a MySQL database, enable the admin interface, and create the django views. We'll be using Bootstrap 4 for styling. You'll learn how to: Implement CRUD operations, Configure and access a MySQL database, Create django views, templates and urls, Style the UI with Bootstrap 4 Django 3 Features Django 3 comes with many new features such as: MariaDB support: Django now officially supports MariaDB 10.1+. You can use MariaDB via the MySQL backend, ASGI support for async programming, Django 3.0 provides support for running as an ASGI application, making Django fully async-capable Exclusion constraints on PostgreSQL: Django 3.0 adds a new ExclusionConstraint class which adds exclusion constraints on PostgreSQL, etc. Prerequisites Let's start with the prerequisites for this tutorial. In order to follow the tutorial step by step, you'll need a few requirements, such as: Basic knowledge of Python, Working knowledge of Django (django-admin.py and manage.py), A recent version of Python 3 installed on your system (the latest version is 3.7), MySQL database installed on your system. We will … -
Rasa looks pretty interesting and I definitely have some plans to use it in my projects and maybe…
Rasa looks pretty interesting and I definitely have some plans to use it in my projects and maybe write an article or two. -
Django News - Issue 9 - Feb 7th 2020
News Django security release: 3.0.3, 2.2.10, 1.11.28 This update fixes a potential SQL injection bug in Django. As ever, the safest strategy for security is to stay on the latest release! djangoproject.com Django Riffs, a podcast for learning Django Announcing Django Riffs, a new podcast aimed at helping beginners learn how to use Django by Matt Layman. mattlayman.com Articles How I'm Testing in 2020 From long-time Django core contributor James Bennett, a deep-dive on modern Python and Django testing techniques. b-list.org Common Celery Issues on Django Projects Celery is an extremely popular task manager. This article is a great overview of common issues you might encounter using it with Django. adamj.eu Asynchronous tasks in Django with Django Q A detailed tutorial on using django-q, a popular way to handle async tasks. valentinog.com Learn Django - The 10 Most-Used Django Packages A somewhat surprising look at the most popular Django-related packages based on PyPI downloads. learndjango.com Podcasts Django Chat Podcast - Growing Old Gracefully as a Programmer Deep thoughts on how to maintain a career as a programmer. djangochat.com Django Riffs - Get To Know Django A new podcast from popular Django teacher Matt Layman. djangoriffs.com Projects django-graphql-auth Django registration and … -
Growing Old Gracefully as a Programmer
Weekly Django Chat newsletterGrowing old gracefully: on being a career programmer @DjangoCon Europe 2018InstallPython3.comThe Mythical Man-Month: Essays on Software Engineering -
Django 3 Authentication with a MySQL Database— Login, Logout and Password Change/Reset
In this tutorial, you'll learn how to easily add a complete authentication system to your django 3 application with login, logout and password change and reset functionalities. We'll be using django 3 with a MySQL database. We'll also be using django-crispy-forms and Bootstrap 4 for styling the application UI. Prerequisites Let's start with the prerequisites for this tutorial. In order to follow the tutorial step by step, you'll need a few requirements, such as: Basic knowledge of Python, Working knowledge of Django (django-admin.py and manage.py), A recent version of Python 3 installed on your system (the latest version is 3.7), MySQL database installed on your system. We will be using pip and venv which are bundled as modules in recent versions of Python so you don't actually need to install them unless you are working with old versions. If you are ready, lets go started! Creating a Virtual Environment for Your Django 3 Project A virtual environment allows you to isolate your current project dependencies from the rest of packages installed globally on your system or in the other virtual environments. You can either use virtualenv which needs to be installed on your system or the venv module available as … -
Announcing Our New Django Book: Django Crash Course
Django Crash Course is a book consisting of a huge chunk of our corporate training materials. Right now we're offering the alpha version of the e-book at $9.99, which is a steal considering we used to teach corporate trainings for $3000 using the same curriculum materials. Even though there are other fantastic introductory Django books out there, this tutorial shows you how to do Django our way. Readers of this alpha version have the opportunity to help us really shape the direction of the book through their feedback, and to be credited as contributors or even technical reviewers, depending on experience and level of contribution. Django Crash Course: Covers Python 3.8 and Django 3.x is currently available as an e-book: The paperback, coil-bound, and hardcover versions are available for pre-order: Learn more and order the course-in-a-book here: https://www.roygreenfeld.com/collections/two-scoops-press/products/django-crash-course -
Announcing Our New Django Book: Django Crash Course
Django Crash Course is a book consisting of a huge chunk of our corporate training materials. Right now we're offering the alpha version of the e-book for under US$20, which is a steal considering we used to teach corporate trainings for $3000 using the same curriculum materials. Even though there are other fantastic introductory Django books out there, this tutorial shows you how to do Django our way. Readers of this alpha version have the opportunity to help us really shape the direction of the book through their feedback, and to be credited as contributors or even technical reviewers, depending on experience and level of contribution. Django Crash Course: Covers Python 3.8 and Django 3.x is currently available as an e-book: The paperback, coil-bound, and hardcover versions are available for pre-order: Learn more and order the course-in-a-book here: https://www.roygreenfeld.com/collections/two-scoops-press/products/django-crash-course -
"Where did we get lucky?"
Retrospectives are probably the most important software development practice. They build a culture of continuous improvement. We may fail, but we’ll learn and do better next time. (Or, at, least, fail differently.) The most common retrospective practice revolves around some variation of these three questions: “What went well?” “What could have gone better?” “What we should we differently next time?” I want to suggest adding a fourth question: -
How to use PyMySQL with Django
Django provides MySQL and MariaDB suport out of the box. It supports the mysqlclient library as its DB API driver to connect. mysqlclient is a Python 3 compatible fork of the original Python MySQL driver, MySQLdb. It still provides a Python module called MySQLdb. On install, it compiles against either the MariaDB client library or MySQL client library - whichever one you have installed. Compiling against the C extension can be frustrating: It requires not only the MariaDB/MySQL client library, but also OpenSSL. Obtaining the C extension libraries might not be trivial on some platforms, such as AWS Lambda. After install, Pip’s wheel cache will store the one compiled version, so if you swap to another C library (e.g. MySQL -> MariaDB), the wheel might not work any more. If these are blockers for you, an alternative is to use the PyMySQL library. It’s a pure Python DB API driver - no C compilation necessary! It’s also made by the same developers as mysqlclient and is better maintained at this point. Django doesn’t officially support it, but PyMySQL ships with an (undocumented) compatibility install that can help you use it. It’s an “at your own risk” feature. I’ve done this … -
How I'm testing in 2020
Once upon a time I wrote a bit about testing, specifically how I was organizing and testing my open-source Django apps. It’s been a while since that post, though, and the calendar has even flipped over to a new penultimate digit in the year number, so it’s worth revisiting to go over what’s changed in how I do things and what’s stayed the same. And since I do maintain a couple things that aren’t … Read full entry -
My reflections on programming languages I have worked with during my career.
I've played and worked with multiple programming languages over my 15-year long career in development (and a bit before, when I studied at school and university). Every language I touched left a piece of knowledge or a feeling in me. So I decided to remember them all and try to … Read now -
Common Celery Issues on Django Projects
Here are some issues I’ve seen crop up several times in Django projects using Celery. They probably apply to other web frameworks and other task queues, I simply haven’t used others so much. 1. Enqueueing Data Rather Than References If you duplicate data from your database in your task arguments, it can go stale in the queue before the task executes. I can’t describe this more completely than Celery’s documentation on task state. See the description and example there. This is not so easy to do accidentally on Celery since version 4, which changed the default serializer from Pickle to JSON. (If you’re not sure what serializer you’re using, check your settings.) But, it is still possible to enqueue data rather than references. For example, imagine you enqueue an email with a 1 minute delay, using the user’s email address in the task arguments, rather than ID. If the user changes their email address before the ask runs, the email gets sent to the wrong address. 2. Enqueueing Tasks Within Database Transactions Although you might think Celery is for executing tasks “later/eventually,” it can execute them quickly too! If you enqueue a task from within a database transaction, it might … -
Speeding up Django pagination
I assume you have already read Optimizing the Django Admin Paginator. If not, this is basically the take-away from that article: class InfinityPaginator(Paginator): @property def count(self): return 99999999999 class MyAdmin(admin.ModelAdmin): paginator = InfinityPaginator show_full_result_count = False Though the article has a trick with using a statement_timeout, I think it's pointless. In the real world you should expect to get that overt 99999999999 count all over the place. Unless you have some sort of toy project it's very likely your database will be under load. Add some user/group filtering and you'll be always hit the time limit. What if you could make the count more realistic, but still cheap? Using a random number would be too inconsistent. Strangely enough someone decided that it's a good idea to put a count estimate idea in the postgresql wiki and, for reasons I decided to see how hard is to implement it in django, in a somewhat generalized fashion From a series of "Just because you can, you have to try it!", behold [1]: class EstimatedQuerySet(models.QuerySet): estimate_bias = 1.2 estimate_threshold = 100 def estimated_count(self): if self._result_cache is not None: return self.count() try: qs = self.model._base_manager.all() compiler = self.query.get_compiler('default') where, params = compiler.compile(self.query.where) qs = … -
Speeding up Django pagination
I assume you have already read Optimizing the Django Admin Paginator. If not, this is basically the take-away from that article: class InfinityPaginator(Paginator): @property def count(self): return 99999999999 class MyAdmin(admin.ModelAdmin): paginator = InfinityPaginator show_full_result_count = False Though the article has a trick with using a statement_timeout, I think it's pointless. In the real world you should expect to get that overt 99999999999 count all over the place. Unless you have some sort of toy project it's very likely your database will be under load. Add some user/group filtering and you'll be always hit the time limit. What if you could make the count more realistic, but still cheap? Using a random number would be too inconsistent. Strangely enough someone decided that it's a good idea to put a count estimate idea in the postgresql wiki and, for reasons I decided to see how hard is to implement it in django, in a somewhat generalized fashion From a series of "Just because you can, you have to try it!", behold [1]: class EstimatedQuerySet(models.QuerySet): estimate_bias = 1.2 estimate_threshold = 100 def estimated_count(self): if self._result_cache is not None: return self.count() try: qs = self.model._base_manager.all() compiler = self.query.get_compiler('default') where, params = compiler.compile(self.query.where) qs = … -
Django News - Issue 8 - Jan 31st 2020
News DjangoCon US 2020 Announced DjangoCon US is returning to San Diego from October 11-17th. More details to come. djangocon.us Django Day Copenhagen 2020 - Call for Proposals A one-day Django conference on April 17th, 2020. The call for proposals is now live. Submit before February 2nd! djangoday.dk Announcing CockroachDB for Django ORM A Django package for using CockroachDB, an open-source distributed database. cockroachlabs.com Articles Demos, Prototypes, and MVPs Django co-creator Jacob Kaplan-Moss concisely covers the difference between three commonly misused terms: demos, prototypes, and MVPs. jacobian.org All You Need To Know About Prefetching in Django How to use Prefetch to speed up queries in Django hakibenita.com URLs Lead the Way A detailed look at how Django process URL requests internally. mattlayman.com Podcasts Django Chat Podcast - GeoDjango with Anna Kiefer GeoDjango is a powerful built-in contrib module that turns Django into a world-class geographic Web framework. djangochat.com Projects skorokithakis/django-tokenauth: Django authentication backend that uses tokens sent over email. Django-tokenauth is a simple, passwordless authentication method based on a one-time token sent over email which does not require user registration. github.com django-weasyprint: A Django class-based view generating PDF resposes using WeasyPrint WeasyPrint turns HTML into beautiful PDF pages. This project … -
Disabling Atomic Transactions In Django Test Cases
TestCase is the most used class for writing tests in Django. To make tests faster, it wraps all the tests in 2 nested atomic blocks. In this article, we will see where these atomic blocks create problems and find ways to disable it. Select for update Django provides select_for_update() method on model managers which returns a queryset that will lock rows till the end of transaction. def update_book(pk, name): with transaction.atomic(): book = Book.objects.select_for_update().get(pk=pk) book.name = name book.save() When writing test case for a piece of code that uses select_for_update, Django recomends not to use TestCase as it might not raise TransactionManagementError. Threads Let us take a view which uses threads to get data from database. def get_books(*args): queryset = Book.objects.all() serializer = BookSerializer(queryset, many=True) response = serializer.data return response class BookViewSet(ViewSet): def list(self, request): with ThreadPoolExecutor() as executor: future = executor.submit(get_books, ()) return_value = future.result() return Response(return_value) A test which writes some data to db and then calls this API will fail to fetch the data. class LibraryPaidUserTestCase(TestCase): def test_get_books(self): Book.objects.create(name='test book') self.client = APIClient() url = reverse('books-list') response = self.client.post(url, data=data) assert response.json() Threads in the view create a new connection to the database and they don't see … -
Disabling Atomic Transactions In Django Test Cases
TestCase is the most used class for writing tests in Django. To make tests faster, it wraps all the tests in 2 nested atomic blocks. In this article, we will see where these atomic blocks create problems and find ways to disable it. Select for update Django provides select_for_update() method on model managers which returns a queryset that will lock rows till the end of transaction. def update_book(pk, name): with transaction.atomic(): book = Book.objects.select_for_update().get(pk=pk) book.name = name book.save() When writing test case for a piece of code that uses select_for_update, Django recomends not to use TestCase as it might not raise TransactionManagementError. Threads Let us take a view which uses threads to get data from database. def get_books(*args): queryset = Book.objects.all() serializer = BookSerializer(queryset, many=True) response = serializer.data return response class BookViewSet(ViewSet): def list(self, request): with ThreadPoolExecutor() as executor: future = executor.submit(get_books, ()) return_value = future.result() return Response(return_value) A test which writes some data to db and then calls this API will fail to fetch the data. class LibraryPaidUserTestCase(TestCase): def test_get_books(self): Book.objects.create(name='test book') self.client = APIClient() url = reverse('books-list') response = self.client.post(url, data=data) assert response.json() Threads in the view create a new connection to the database and they don't see …