Django community: RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
How to Automatically Retrying Failed Celery Tasks
In this tutorial, we'll look at how to automatically retry failed Celery tasks. -
Automatically Retrying Failed Celery Tasks
This post looks at how to automatically retry failed Celery tasks. -
Richer Django logging
I recently tested Rich logging with a Django project and the results were good. Here's what it looks like: © 2020 Will McGugan A Django app with Rich logging Note the file and line at the right hand of the terminal which shows were the log function was called. If your terminal supports hyperlinks, you can click that link to open the file! For reference, here's what it looks like without Rich logging. © 2020 Will McGugan A Django app without Rich logging This is the default RichHandler class in Rich. It would be possible to do a little more customization for Django logs. Maybe highlight 5xx in red, for example. To add this to your Django project, pip install rich then configure the console to use "rich.logging.RichHandler" rather than StreamHandler. LOGGING = { "version": 1, "disable_existing_loggers": False, "formatters": {"rich": {"datefmt": "[%X]"}}, "handlers": { "console": { "class": "rich.logging.RichHandler", "formatter": "rich", "level": "DEBUG", } }, "loggers": {"django": {"handlers": ["console"]}}, } The process is much the same for any project. See the documentation for the Rich logging handler for details. -
Django A/B testing with Google Optimize
A/B testing is a great way to decide what path your product should take. Create two variations, collect data points and see which variation is better. It will help you in understanding your users and their needs. Google Optimize is a tool that simplifies A/B testing by managing variant weights, targeting rules, provides analytics and integrates with other Google product as Google Adwords and Google Analytics. There are two ways to run A/B tests: client-side and server-side. Client-side A/B tests with Google Optimize executes javascript in browser based on the changes you would like (style changes, positioning of elements etc.), it is fairly limited. In server-side A/B tests there are no limitations, you can write any code you'd like which will display different application versions based on the session's active experiment variant. ## The Google Optimize basics When a user visits your site and an experiment is active he will be given an experiment variant which is stored in a cookie called `_gaexp.` The cookie can have the following content `GAX1.2.MD68BlrXQfKh-W8zrkEVPA.18361.1` and it contains multiple values where each value is divided by dots. There are two key values for server-side A/B testing. Firstly, the experiment ID which in the example … -
How to Unit Test a Django Form
This post is an adapted extract from my book Speed Up Your Django Tests, available now. Django’s test client is really useful for writing integration tests for your project. It’s great because it has a simple API for testing your application similarly to how a web browser would interact with it. Unfortunately it can be slow, because each call creates a request, passes it through all your middleware, view, maybe a template, then the response comes back through the same layers. The Test Structure chapter of Speed Up Your Django Tests includes a section on rewriting integration tests that use the test client as unit tests. This rewriting makes them faster and more accurate. Here’s one example rewriting some integration tests for a form as unit tests. Forms are a great example of a component that can be easily unit tested. They accept a dictionary of values, validate it, and return either errors or cleaned data. For an example, take this form: from django import forms from example.core.models import Book class AddBookForm(forms.ModelForm): class Meta: model = Book fields = ["title"] def clean_title(self): title = self.cleaned_data["title"] if not title: return title if not title[0].isupper(): self.add_error("title", "Should start with an uppercase letter") … -
Write JSON APIs In Pure Django For Beginners – Part 2
This is the 2nd part of the previous post. To continue with the tutorial, we need to install an API client such as Postman. This makes our life easier while developing APIs. After installation, try to execute the get all books API we created in the previous part. You can see the response we observed previously. For GET APIs it doesn’t make much difference but for POST, PUT, DELETE APIs it’s quite convenient to use Postman. We are going to build these APIs and will be using Postman for debugging and testing purpose. If you are not familiar with these HTTP verbs, I strongly advise you to read about these here or here Alright, let’s begin with POST API. But before that, let’s recap how does BookView class look like from the previous part. class BookView(View): def get(self, request): books_count = Book.objects.count() # TOTAL books in the database books = Book.objects.all() # Get all book objects from the database books_serialized_data = serialize('python', books) data = { 'books': books_serialized_data, 'count': books_count, } return JsonResponse(data) This view just supports the GET method. To support the POST method, we have to add another method called post in this View. We also have to … -
Write JSON APIs In Pure Django For Beginners
In this tutorial, we are going to build a few JSON based CRUD(Create, Read, Update, Delete) APIs in Django without using any additional library such as DjangoRestFramework or Tastypie. This tutorial will be posted in two parts-– Writing JSON APIs In Pure Django For Beginners – Part 1– Writing JSON APIs In Pure Django For Beginners – Part 2 Install & Setup 1. Install Django This tutorial is based on Python3.7 and Django 3.0. So please make sure you have Python3.7 or later and Django 3.0 or later installed in your system. Install pip from here if you haven’t already, skip otherwise. Once pip installed, you can install Django using pip. (you might need to run the command with sudo if you are not using virtualenv) gaurav@gaurav$ pip install django==3.0 You can also Follow the detailed installation instructions here if you face any issue. 2. Create and setup the project First, we need to set up the project. For that, you can simply follow the below instructions to get everything ready. gaurav@gaurav$ django-admin startproject pyscoop gaurav@gaurav$ cd pyscoop gaurav@gaurav$ python manage.py startapp myapp gaurav@gaurav$ python manage.py migrate Apply all migrations: admin, auth, contenttypes, sessions Running migrations: Applying contenttypes.0001_initial... OK Applying … -
Django News - Virtual DjangoCon Europe - Jun 12th 2020
News DjangoCon Europe 2020 goes virtual The virtual conference will take place on September 18-19 (Friday and Saturday). djangocon.eu Django Forum - Tailwind CSS Advice The Django Forum is a great place to ask Django-specific questions and hear from the experts. This thread was on Tailwind and Jeff responded. We encourage everyone to give the Forum a shot! djangoproject.com Articles Stop Using datetime.now! A look at time and practical dependency injection in Python & Django. hakibenita.com Python Crash Course - Recommended Django Resources A curated list of Django resources from the author of Python Crash Course. github.io Django's Git flow The author, Sage Abdullah, was a 2019 Google Summer of Code student and here writes on how Django uses git with the official codebase. laymonage.com Avoid Hardcoding ID's in Your Tests An elegant way to prevent a common Django testing issue. adamj.eu Sponsored Link Two Scoops of Django 3.x: Best Practices for the Django Web Framework The long-awaited update covers various tips, tricks, patterns, code snippets, and techniques of Django best practices. feldroy.com Podcasts Django Chat #67 - Python Crash Course with Eric Matthes Eric is the author of Python Crash Course, the bestselling Python book in the world. We … -
A View From Start To Finish - Building SaaS #60
In this episode, I created a view to add students from beginning to the end. I used Error Driven Development to guide what I needed to do next to make the view, then wrote tests, and finished it all off by writing the template code. At the start of the episode, I gave a quick overview of the models in my application and which models I planned to focus on for the stream. -
Avoid Hardcoding ID's in Your Tests
This is a test anti-pattern I’ve seen creep in on many Django test suites. I know of several large projects where it became a major undertaking to undo it. The good news is it’s easy to avoid adding it when you first write your tests. 🙂 Take this test: from http import HTTPStatus from django.test import TestCase from example.core.models import Book class BookViewTests(TestCase): def test_view(self): book = Book.objects.create(title="Meditations") response = self.client.get("/books/1/") self.assertEqual(response.status_code, HTTPStatus.OK) self.assertContains(response.content, book.title) It creates a book object in the database, retrieves the associated page, and checks the response looks correct. In all likelihood this test will pass, at least initially. The test database should be empty at the start, the database will assign the new book ID 1, and when the test fetches the URL “/books/1/” the view finds the correct book. Unfortunately, many potential changes to the project will break the ID = 1 assumption. For example, if you create some “default books” in a post_migrate signal handler (as I covered previously), the book in the test will be given a different ID. Also, whilst databases tend to be predictable, they often don’t guarantee that future versions or configuration changes won’t ever affect ID generation. The … -
Python Crash Course - Eric Matthes
Python Crash Course, Second Edition Book https://nostarch.com/pythoncrashcourse2ePython Crash Course website https://ehmatthes.github.io/pcc_2e/Python Crash Course Cheat Sheets https://ehmatthes.github.io/pcc_2e/cheat_sheets/cheat_sheets/Python Flash Cards https://nostarch.com/pythonflashcardsDjango Homepage 5 Different WaysDjangoCon 2019 - Using Django as a Micro-Framework by Carlton Gibson https://youtu.be/w9cYEovduWIDjango Microframework Github repo https://github.com/wsvincent/django-microframework -
Django Hello, World 5 Different Ways
Django is a "batteries-included" framework that comes with built-in scaffolding provided by the `startproject` command and the general concepts of apps. But, actually, Django provides incredible flexibility if desired around … -
How to Check Multiple Tables Are Empty in SQL
I encountered this problem recently for a Django ticket for speeding up part of Django’s test runner. We have a list of tables to truncate. How can we figure out which are already empty, so we can skip issuing a somewhat costly TRUNCATE TABLE on them? You might think the database could skip TRUNCATE TABLE on empty tables, but it normally does work beyond removing the rows such as resetting auto-increment counters. In MySQL, TRUNCATE TABLE is even equivalent to DROP TABLE and CREATE TABLE, which requires removing and creating the table file on disk. You might think the easiest way to find out which tables are empty would be to count the rows in each table: mariadb [1]> SELECT COUNT(*) FROM table1; +----------+ | COUNT(*) | +----------+ | 123 | +----------+ 1 row in set (0.002 sec) mariadb [2]> SELECT COUNT(*) FROM table2; +----------+ | COUNT(*) | +----------+ | 0 | +----------+ 1 row in set (0.001 sec) This works, but it’s slow. Database systems don’t keep a counter of the number of rows in each table, because the overhead to keep it consistent with transactions would be too high. Therefore a COUNT(*) means touching every individual row, obtaining … -
Lack of Volume Persistence in kartoza/postgis Docker Container
I have been working on a Django project that needs maps integration. I looked into using GeoDjango, but needed to upgrade my current Postgresql database to add PostGIS.… -
Lack of Volume Persistence in kartoza/postgis Docker Container
I have been working on a Django project that needs maps integration. I looked into using GeoDjango, but needed to upgrade my current Postgresql database to add PostGIS. This was totally outside what I’ve done before. Here’s how I got it to work. Starting Point My knowledge of Docker comes entirely from Will Vincent’s Django for Professionals book. In there, he builds from the official postgres Docker image within a docker-compose.yml file. version: "3.7" services: web: build: . command: python /code/manage.py runserver 0.0.0.0:8000 volumes: - .:/code ports: - 8000:8000 depends_on: - db db: image: postgres:11 volumes: - postgres_data:/var/lib/postgresql/data/ environment: - "POSTGRES_HOST_AUTH_METHOD=trust" volumes: postgres_data: Learning to Use GeoDjango Luckily, I found a great tutorial over at Real Python to introduce myself to GeoDjango: “Make a Location-Based Web App With Django and GeoDjango” by Ahmed Bouchefra. If you’re totally new to GeoDjango, I highly recommend this article. Ahmed even uses Docker in the article, so super win if you’re coming from Vincent’s book like me. PostGIS is an extension of PostgreSQL, so it’s kind of like a standard postgres database you might use for your Django project, but adds support for geographic objects. In the article, we use a new PostgreSQL image … -
Docker & Django Local Development
At the end of this post you will have: Set up Docker locally on your dev box. Run Django in a Docker container on the same dev box. Put a breakpoint and debugged the code! Pre-reqs Docker installed locally. For the purposes of this proof-of-concept, I used Docker Desktop. Minimal Docker Setup Our minimal docker setup will: run a relational database: Postgres directly run runserver command, which is what should be done for debugging purposes Our minimal docker setup will not: run a webserver, such as Nginx run gunicorn or uwsgi as a glue between the framework (Django code) and the webserver Since the objective is local development with Docker neither are needed. Minimal Docker Understanding If some Docker concepts are still not clear to you, do not worry. I have to search for new stuff myself all the time. When I started, I found this article really helpful: 6 Docker Basics You Should Completely Grasp When Getting Started. This article explains the differences and the relationship between: Containers Images Dockerfiles Volumes Port Forwarding Docker compose A lifesaver if you’re confused with the barrage of new Docker jargon. In this post we’ll be setting up: one Dockerfile, one Docker compose … -
[Django Rest Framework] Build Your First REST API with Python Django Rest Framework. (CRUD Operation)
Django REST framework is a powerful and flexible toolkit for building Web APIs. Some reasons you might want to use REST framework: The Web browsable API is a huge usability win for your developers. Authentication policies including packages for OAuth1a and OAuth2. Serialization that supports both ORM and non-ORM data sources. Customizable all the way down – just use regular function-based views if you don’t need the more powerful features. Extensive -
Django News - New Django 3.0.7 security release - Jun 5th 2020
News Django security releases issued: 3.0.7 and 2.2.13 Two new security patches. As ever, updating to the latest version of Django is highly recommended. Here is how to upgrade per the official docs. djangoproject.com 🖤 Thank you DRF, DSF, PSF, DEFNA, Wagtail, and everyone who showed their solidarity this week. #BlackLivesMatter Django Newsletter Articles Minimal Environment Variables Setup for Django A guide to configuring environment variables in Django projects. untangled.dev Django Class-Based Views in Diagrams Starting with each class-based views (CBVs) dispatch method, these diagrams break down each view until it reaches a response or redirects. brennantymrak.com Spell Check Django Templates, HTML, Markdown, and RST Files in VS Code How to add spellcheck support for HTML, Django/Jinja templates, reStructuredText (Sphinx), and markdown in VS Code. coderedcorp.com So you work in tech and want to support Black Lives Matter I've written this guide for my fellow non-Black folks in tech to help you figure out for yourself what specific actions you can take to help, now and into the future. dev.to Python Community Interview With Russell Keith-Magee Russell is a longtime member of the Django community. realpython.com Sponsored Link Two Scoops of Django 3.x: Best Practices for the Django Web Framework … -
[Django Rest Framework] OperationalError at /api/ no such table: django_session
If you are creating a API using Django Framework than you generally encounter this issue regarding django_session. Error: OperationalError at /api/ no such table: django_session If you want to follow along this blog and want to know how this error has occurred than visit the link below to setup your first Django Rest Framework: Building -
Designing A View - Building SaaS #59
In this episode, I focused on a single view for adding a course to a school year. This view is reusing a form class from a different part of the app and sharing a template. We worked through the details of making a clear CreateView. The stream started with me answering a question about how I design a new feature. I outlined all the things that I think through for the different kinds of features that I need to build. -
Two Scoops of Django - Daniel Feldroy
Two Scoops of Django 3.xDjango Crash CourseDaniel personal sitecookiecutter-djangodjango-crispy-formscookiecutter -
Introducing time-machine, a new Python library for mocking the current time
Whilst writing Speed Up Your Django Tests, I wanted to add a section about mocking the current time. I knew of two libraries such mocking, but I found it hard to pick one to recommend due to the trade-offs in each. So I delayed adding that section and shaved a rather large yak by writing a third library. This post is my introduction to the problem, the trade-offs with the other libraries, and how my new library, time-machine, tries to solve them. The next version of Speed Up Your Django Tests can now include a section on mocking time. The Problem It’s especially common in web projects to have features that rely on changes to the current date and time. For example, you might have a feature where each customer may only make an order once per day. For testing such features, it’s often necessary to mock the functions that return the current date and time. In Python’s standard library, these live in the datetime and time modules. There are various ways of doing this mocking: it can be done generically with unittest.mock, or in a more targeted fashion with a time-mocking library. Using unittest.mock works, but it’s often inaccurate … -
Extracting values from environment variables in tox
[Tox](https://tox.readthedocs.io) is a great tool for automated testing. We use it, not only to run matrix testing, but to run different types of tests in different environments, enabling us to parallelise our test runs, and get better reporting about what types of tests failed. Recently, we started using [Robot Framework](https://robotframework.org) for some automated UI testing. This needs to run a django server, and almost certainly wants to run against a different database. This will require our `tox -e robot` to drop the database if it exists, and then create it. Because we use [dj-database-url](https://pypi.org/project/dj-database-url/) to provide our database settings, our [Codeship](https://codeship.com/) configuration contains an environment variable set to `DATABASE_URL`. This contains the host, port and database name, as well as the username/password if applicable. However, we don't have the database name (or port) directly available in their own environment variables. Instead, I wanted to extract these out of the `postgres://user:password@host:port/dbname` string. My tox environment also needed to ensure that a distinct database was used for robot: {% highlight text %} [testenv:robot] setenv= CELERY_ALWAYS_EAGER=True DATABASE_URL={env:DATABASE_URL}_robot PORT=55002 BROWSER=headlesschrome whitelist_externals= /bin/sh commands= sh -c 'dropdb --if-exists $(echo {env:DATABASE_URL} | cut -d "/" -f 4)' sh -c 'createdb $(echo {env:DATABASE_URL} | cut -d "/" … -
Improving Code Confidently with Test-Driven Development
Looks at an example of how Test-Driven Development improves code quality. -
Stop Using datetime.now!
One of my favorite job interview questions is this: Write a function that returns tomorrow's date This looks innocent enough for someone to suggest this as a solution: import datetime def tomorrow() -> datetime.date: return datetime.date.today() + datetime.timedelta(days=1) This will work, but there is a followup question: How would you test this function? Before you move on.... take a second to think about your answer. One of these pigeons is a mockPhoto by Pedro Figueras Table of Contents Naive Approach Dependency Injection Dependency Injection in The Wild Injecting Functions Injecting Values When to Instantiate Injected Values Dependency Injection in Practice IP Lookup Assigning Responsibility Using a Service Changing Implementations Typing Services Using a Protocol Nondeterminism and Side-Effects Conclusion Naive Approach The most naive approach to test a function that returns tomorrow's date is this: # Bad assert tomorrow() == datetime.date(2020, 4, 16) This test will pass today, but it will fail on any other day. Another way to test the function is this: # Bad assert tomorrow() == datetime.date.today() + datetime.timedelta(days=1) This will also work, but there is an inherent problem with this approach. The same way you can't define a word in the dictionary using itself, you should not …