Django community: Community blog posts RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
Django Quiz 4
On Wednesday evening last week I held a quiz at the January London Django Meetup Group. This was the fourth quiz, which has become an annual Christmas tradition at the meetup. Unfortunately it was a month late this year due to venue changes, so I’ve titled this post “Django Quiz 4”. Here it is so you can try it at home - answers are at the bottom. Enjoy! Part 1: Trivia 1. What creature is the Django pony? Unicorn Pegasus Horse Centaur 2. In which city will DjangoCon Europe 2020 be held? Prague, Czechia Porto, Portugal London, UK Vatican City 3. What is the “code word” for the latest major Django release? “Salmagundi” “Smorgasbord” “Raft” “Range” 4. What is the Django Software Foundation funding goal for 2020 in US Dollars? $30,000 $200,000 $600,000 $1,000,000 5. What was the name of one of Django Reinhardt’s cousins? Ruby Python ECMAScript Pascal Part 2: Coding in Django 6. The Django tutorial has you build a Django app called what? quizzes surveys polls testimonies 7. What is the date for the final Python 2.7 minor version release? April 2019 October 2019 January 2020 April 2020 8. According to crawler.ninja on 2020-01-14, what percentage of … -
How to Use Chart.js with Django
Chart.js is a cool open source JavaScript library that helps you render HTML5 charts. It is responsive and counts with 8 different chart types. In this tutorial we are going to explore a little bit of how to make Django talk with Chart.js and render some simple charts based on data extracted from our models. Installation For this tutorial all you are going to do is add the Chart.js lib to your HTML page: <script src="https://cdn.jsdelivr.net/npm/chart.js@2.9.3/dist/Chart.min.js"></script> You can download it from Chart.js official website and use it locally, or you can use it from a CDN using the URL above. Example Scenario I’m going to use the same example I used for the tutorial How to Create Group By Queries With Django ORM which is a good complement to this tutorial because actually the tricky part of working with charts is to transform the data so it can fit in a bar chart / line chart / etc. We are going to use the two models below, Country and City: class Country(models.Model): name = models.CharField(max_length=30) class City(models.Model): name = models.CharField(max_length=30) country = models.ForeignKey(Country, on_delete=models.CASCADE) population = models.PositiveIntegerField() And the raw data stored in the database: cities id name country_id population … -
Django News - Issue 6 - Jan 17th 2020
News HTTPX Release HTTPX is a next-generation HTTP client, likely the successor to the popular requests package. This latest release of HTTPX supports both sync and async and is one step away from a forthcoming 1.0 release. Django Newsletter Django Day Copenhagen 2020 - April 17, 2020 The call for participation is currently out for this one day Django conference. djangoday.dk DjangoCon Europe Early Bird Tickets Now Available The conference is in Porto, Portugal from May 27-31, featuring 3 days of talks and 2 days of springs. djangocon.eu Articles Advice for new contributors from the Django documentation There are many ways to contribute to Django itself, especially the documentation. Here is a getting-started guide. If you have any issues, put a question on the Django Forum and you'll get help! djangoproject.com Make Django Tests Always Rebuild the Database if It Exists - Adam Johnson An elegant quick fix for a common Django issue. adamj.eu Counting Queries: Basic Performance Testing in Django - Vinta Building Beyond Blog A nice overview of first-pass performance testing in a Django app. vinta.com.br Videos Django REST Framework Full Course for Beginners A 2+ hour course on building APIs with Django 3.0. youtu.be DjangoCon 2019 - … -
Demos, Prototypes, and MVPs
Much of my work at Hangar involves early product development, helping our startups “break ground” on their products. We’re starting with little more than an idea, and maybe some theories from the research, and taking the first steps towards a marketable product. I’m usually building one of three things: a demo, a prototype, or a minimum viable product (MVP). I’ve seen some confusion over these terms — some people seem to use them somewhat interchangeable. -
Django's Async Future - Tom Christie
Weekly DjangoChat NewsletterTom Christie personal siteEncodeDjangoCon Europe 2019 - Sketching out a Django redesignDjangoCon US 2019 - Just Add Await: Retrofitting Async into DjangoDjango REST FrameworkMkDocsmkautodocHTTPXurllib3Starlettehostedapi - demo app built with Starlette -
Asynchronous tasks in Django with Django Q
Requirements To follow along you'll need: an Heroku account if you want to use their Redis add-on the Heroku CLI installed on your system a newer version of Python, ideally 3.6 or 3.7 Git Deployment on Heroku is optional, and you can use your own Redis instance if you've already got one locally. Setting up the project And now let's get to work! To start off we're going to create a new Python virtual environment alongside with a Django installation: mkdir django-q-django && cd $_ python3 -m venv venv source venv/bin/activate pip install django Next up we're going to create a new Django project from a template: django-admin startproject \ --template https://github.com/valentinogagliardi/ponee/archive/master.zip \ --name=Procfile \ --extension=py,example django_q_django . If you're wondering what I'm doing here, this is a template of mine. I've got a link in the resources with a tutorial for creating your own Django project template. Now let's install the dependencies with pip: pip install -r ./requirements/dev.txt We need also to provide some environment variables for our project: mv .env.example .env and finally we're going to run Django migrations: python manage.py makemigrations python manage.py migrate at this point you should be able to run the development server: python … -
Asynchronous tasks in Django with Django Q
Learn how to use Django Q, the task queue, with the Redis broker for offloading long running tasks in your Django applications. Requirements To follow along you’ll need: an Heroku account if you want to use their Redis add-onthe Heroku CLI installed on your systema newer version of Python, ideally 3.6 or 3.7Git Deployment on Heroku is optional, and you can use your own Redis instance if you’ve already got one locally. Setting up the project And now let’s get to work! To start off we’re going to create a new Python virtual environment alongside with a Django installation: mkdir django-q-django && cd $_ python3 -m venv venv source venv/bin/activate pip install django Next up we’re going to create a new Django project from a template: django-admin startproject \ --template https://github.com/valentinogagliardi/ponee/archive/master.zip \ --name=Procfile \ --extension=py,example django_q_django . If you’re wondering what I’m doing here, this is a template of mine. I’ve got a link in the resources with a tutorial for creating your own Django project template. Now let’s install the dependencies with pip: pip install -r ./requirements/dev.txt We need also to provide some environment variables for our project: mv .env.example .env and finally we’re going to run Django migrations: … -
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 … -
Make Django Tests Always Rebuild the Database if It Exists
If you use Django’s test runner, you’ll probably have encountered this message: $ python manage.py test Creating test database for alias 'default'... Got an error creating the test database: database "myproject_test" already exists Type 'yes' if you would like to try deleting the test database 'myproject_test', or 'no' to cancel: The main reason this happens is that the last test run crashed and it left the database around. It can be annoying when you start the test run, go for a cup of tea, and come back to this prompt still waiting, rather than a complete test run. Here’s how to make Django always rebuild the database in this case. We’ll do it by extending test with our own custom managemment command. Create a new test management command inside one of your Django apps, for example testapp/management/commands/test.py. Then add this content: from django.core.management.commands.test import Command as BaseCommand class Command(BaseCommand): def handle(self, *test_labels, **options): # Wrap Django's built-in test command to always delete the database if # it exists options["interactive"] = False return super().handle(*test_labels, **options) This works by intercepting the normal handle() method to first always set the interactive option to False. This is the same as always passing the --noinput … -
How to have default/initial values in a Django form that is bound and rendered
Django's Form framework is excellent. It's intuitive and versatile and, best of all, easy to use. However, one little thing that is not so intuitive is how do you render a bound form with default/initial values when the form is never rendered unbound. If you do this in Django: class MyForm(forms.Form): name = forms.CharField(required=False) def view(request): form = MyForm(initial={'name': 'Peter'}) return render(request, 'page.html', form=form) # Imagine, in 'page.html' that it does this: # <label>Name:</label> # {{ form.name }} ...it will render out this: <label>Name:</label> <input type="text" name="name" value="Peter"> The whole initial trick is something you can set on the whole form or individual fields. But it's only used in UN-bound forms when rendered. If you change your view function to this: def view(request): form = MyForm(request.GET, initial={'name': 'Peter'}) # data passed! if form.is_valid(): # makes it bound! print(form.cleaned_data['name']) return render(request, 'page.html', form=form) Now, the form is bound and the initial stuff is essentially ignored. Because name is not present in request.GET. And if it was present, but an empty string, it wouldn't be able to benefit for the default value. My solution I tried many suggestions and tricks (based on rapid Stackoverflow searching) and nothing worked. I knew one thing: … -
How to have default/initial values in a Django form that is bound and rendered
Django's Form framework is excellent. It's intuitive and versatile and, best of all, easy to use. However, one little thing that is not so intuitive is how do you render a bound form with default/initial values when the form is never rendered unbound. If you do this in Django: class MyForm(forms.Form): name = forms.CharField(required=False) def view(request): form = MyForm(initial={'name': 'Peter'}) return render(request, 'page.html', form=form) # Imagine, in 'page.html' that it does this: # <label>Name:</label> # {{ form.name }} ...it will render out this: <label>Name:</label> <input type="text" name="name" value="Peter"> The whole initial trick is something you can set on the whole form or individual fields. But it's only used in UN-bound forms when rendered. If you change your view function to this: def view(request): form = MyForm(request.GET, initial={'name': 'Peter'}) # data passed! if form.is_valid(): # makes it bound! print(form.cleaned_data['name']) return render(request, 'page.html', form=form) Now, the form is bound and the initial stuff is essentially ignored. Because name is not present in request.GET. And if it was present, but an empty string, it wouldn't be able to benefit for the default value. My solution I tried many suggestions and tricks (based on rapid Stackoverflow searching) and nothing worked. I knew one thing: … -
Github basic auth deprecation and jenkins
I have been getting periodic deprecation notice emails from github for the last few months: Hi @nenskins, You recently used a password to access an endpoint through the GitHub API using okhttp/2.7.5. We will deprecate basic authentication using password to this endpoint soon: https://api.github.com/ We recommend using a personal access token (PAT) with the appropriate scope to access this endpoint instead. Visit https://github.com/settings/tokens for more information. Thanks, The GitHub Team Hm, that @nenskins user, that is our old jenkins instance talking to github somehow. Apparently through basic auth. Only... where? Most of the github traffic seemed to use just an access token. Jenkins calls that the secret text type. Basic auth is type username with password in jenkins. What it turned out to be was the github branch source plugin. This periodically looks at our github organisation to see if there are new projects or new branches that it missed. Normally github tells our jenkins when there's a new project or pull request or so. Ok, on to the jenkins settings for my organisation. The confusing thing here is that the "credentials" setting says this: Note that only "username with password" credentials are supported. Existing credentials of other kinds will … -
Django News - Issue 5 - Jan 9th 2020
News Django 3.0.2 bugfix release It's always a good idea to be on the latest version of Django. The 3.0.2 bug fix was just released. Make sure to upgrade with the help of this guide: https://docs.djangoproject.com/en/3.0/howto/upgrade-version/ djangoproject.com 7.0.0 — Pillow (PIL Fork) 7.0.0 documentation If you manage images then you probably use Pillow. Pillow 7 is out and drops support for Python 2.7. readthedocs.io Articles Typechecking Django and DRF Nikita Sobolev guides us through the process of adding typechecking to our Django and DRF applications using a few third-party libraries. sobolevn.me Getting a Django Application to 100% Coverage Adam Johnson shows us how to install and use coverage to achieve 100% test coverage in our Django Application. A few plugins were new to us, including template coverage. adamj.eu From Browser to Django A solid overview of how browsers and Django work together. mattlayman.com I'm not feeling the async pressure by Armin Ronacher From the creator of Flask, some deep thoughts on async. pocoo.org Podcasts Django Chat - Authentication with José Padilla José has made major contributions to the Django ecosystem, especially around Django REST Framework and authentication. He is now an engineer at Auth0. djangochat.com Projects djangorestframework-stubs Adds static type … -
Authentication - José Padilla
Weekly Django Chat NewsletterJosé Padilla personal siteDjangoCon 2014 - JSON Web Tokens by José PadillaFilePreviewsAuth0Auth0 Django Quickstart TutorialPyCon2019 - Python, Governments, and Contractsdjango-project-templatepyjwtSponsor José on Github -
Feature Checking versus Version Checking
Bruno Oliveira, known for his work on the Pytest project, tweeted this thread last July: I always prefer explicitly checking versions instead of checking for presence of features: # feature checking try: from importlib import metadata except ImportError: import importlib_metadata as metadata # explicit checking if sys.version_info >= (3, 8): from importlib import metadata except ImportError: import importlib_metadata as metadata Reasons: 1) This makes it explicit which versions supports the desired functionality, which serves as documentation and makes it easy to drop the legacy code later when that version is no longer supported: search for sys.version_info in the codebase and remove the old code. 2) In the specific case of ImportErrors, broken environments might cause an otherwise valid import to raise an error, which would then hide the real reason why the import failed. Had seen this myself a few times in "frozen" applications (cx_freeze, pyinstaller) where build problems caused ImportErrors which should not happen on that version. I have also moved towards doing this myself, for the same reasons. 1. It’s More Explicit Being more explicit, is the primary appeal to me. Feature-checking is normally accompanied with an explanatory comment about version numbers anyway. For example here’s some code … -
How to create a Django project from a template
What is a Django project template? A rather obscure feature of Django is the ability to install a Django project starting from a template, that is, from a custom directory structure. This is convenient when the default project format is not enough anymore (it isn't as soon as you want to deploy in production, trust me), or when you reached the point where you repeat the same configurations over and over for a lot of Django projects. A Django project template is nothing more than a pre-defined project with a custom directory structure, and you can create one for yourself in minutes. An example of a popular Django template is django-cookiecutter. With a wide range of features and configurations ready for rock-solid production deployments, django-cookiecutter is the way to go for almost any new Django project. However, django-cookiecutter sometimes is simply too much, especially for beginners, and a simpler Django template can help you to grasp the basics before going bigger with django-cookiecutter. In the next sections you'll learn how to build your own template. How to create your own Django project template To start off create a Python virtual environment in a folder of choice and install Django (note … -
Counting Queries: Basic Performance Testing in Django
It's very common to read about testing techniques such as TDD and how to test application business logic. But testing the performance of an application is whole different issue. There are many ways you can do it, but a common approach is to setup an environment where you can DDoS your application and watch how it behaves. This is a very interesting -
A Python Packaging Carol
I have endeavoured in this Ghostly little book, to raise the Ghost of an Idea, which shall not put my readers out of humour with themselves, with each other, with the season, or with me. May it haunt their houses pleasantly, and no one wish to lay it. Every year around Christmas, I make a point of re-reading Charles Dickens’ A Christmas Carol. If you’ve only ever been exposed to the story through adaptations into … Read full entry -
📣 Now on Google Cloud Run
A quick announcement: as a bit of an experiment, I’ve moved this site to Google Cloud Run. Please let me know if anything seems broken. I may in the future write a bit about Cloud Run: there’s a lot to like, and also some definite here-be-dragons things. In the meantime, though, my capsule review: Here's my capsule review of Cloud Run / Cloud SQL / Cloud Build, from a Heroku stan: -
MyCLI: A MySQL CLI Based on Python with Auto-completion and Syntax Highlighting
If you prefer to work with MySQL via its command-line interface, you'll like mycli which is a CLI tool with auto-completion and syntax highlighting built on top of Python and prompt_toolkit for building interactive command line applications with Python. It is cross-platform and it is tested on Linux, MacOS and Windows. According to the official website: mycli is a command line interface for MySQL, MariaDB, and Percona with auto-completion and syntax highlighting. Prerequisites Python 2.7 or Python 3.4+. How to Install MyCLI? Supposed you have Python and pip installed, open a new terminal and run the following command: $ pip install mycli Check out the official website for instructions on how to install MyCLI on the other platforms. You can check the source code of this tool on GitHub. -
Our Transition to Multiple Publishing Imprints Under the Roy Greenfeld Umbrella
Starting today, we're going to be using different imprints for different types of books. We'll continue to publish our Two Scoops of Django books under the Two Scoops Press imprint. We may publish other technical books under this imprint in the future as well. We're moving our fantasy books to a new imprint. We just came up with it a few hours ago. Impossible Hero Books will be the imprint we use to publish all fantasy books from now on. We'll be republishing our past fantasy books under this imprint. That effort will include revising those books and producing new covers. We're excited about this new brand. We're also going to be publishing children's books, apps, and physical products under the Fuzzy Rainbow brand. Years ago, Audrey created this imprint and published a children's book with it. We're going to be republishing that book and its corresponding mobile app. We have other children's books and products in the works too. We've decided to call our main umbrella brand Roy Greenfeld. (Honestly, we didn't know what else to call it. :) Our online store is moving from twoscoopspress.com to roygreenfeld.com. This will make it possible for us to offer books from … -
Our Transition to Multiple Publishing Imprints Under the Roy Greenfeld Umbrella
Starting today, we're going to be using different imprints for different types of books. We'll continue to publish our Two Scoops of Django books under the Two Scoops Press imprint. We may publish other technical books under this imprint in the future as well. We're moving our fantasy books to a new imprint. We just came up with it a few hours ago. Impossible Hero Books will be the imprint we use to publish all fantasy books from now on. We'll be republishing our past fantasy books under this imprint. That effort will include revising those books and producing new covers. We're excited about this new brand. We're also going to be publishing children's books, apps, and physical products under the Fuzzy Rainbow brand. Years ago, Audrey created this imprint and published a children's book with it. We're going to be republishing that book and its corresponding mobile app. We have other children's books and products in the works too. We've decided to call our main umbrella brand Roy Greenfeld. (Honestly, we didn't know what else to call it. :) Our online store is moving from twoscoopspress.com to roygreenfeld.com. This will make it possible for us to offer books from … -
Our Transition to Multiple Publishing Imprints Under the Roy Greenfeld Umbrella
Starting today, we're going to be using different imprints for different types of books. We'll continue to publish our Two Scoops of Django books under the Two Scoops Press imprint. We may publish other technical books under this imprint in the future as well. We're moving our fantasy books to a new imprint. We just came up with it a few hours ago. Impossible Hero Books will be the imprint we use to publish all fantasy books from now on. We'll be republishing our past fantasy books under this imprint. That effort will include revising those books and producing new covers. We're excited about this new brand. We're also going to be publishing children's books, apps, and physical products under the Fuzzy Rainbow brand. Years ago, Audrey created this imprint and published a children's book with it. We're going to be republishing that book and its corresponding mobile app. We have other children's books and products in the works too. We've decided to call our main umbrella brand Roy Greenfeld. (Honestly, we didn't know what else to call it. :) Our online store is moving from twoscoopspress.com to roygreenfeld.com. This will make it possible for us to offer books from … -
Mental Models for Class Based Views
Django’s class-based views are powerful and customizable, but unless you have a solid mental model of how they work, they’re also confusing and tricky to master. It’s easy to use class-based views without a deep understanding of their inner workings, which leads to Googling for which method or attribute to modify rather than confident usage of your tools. In this post, we’ll walk through the base classes Django uses its class-based views. You’ll gain a useful picture of how class-based views work under the hood and how they differ from function-based views. Hopefully, this will make you more comfortable working with class-based views and less dependent on StackOverflow in the future. The entirety of this post focuses on a single Django file, /django/django/views/generic/base.py, which contains the classes that live at the top of the view classes’ inheritance trees. If you’re looking for something specific, you can jump to that section below. ViewClosuresFunction-based vs. class-based viewsView.setup()View.dispatch()RedirectViewTemplateViewContextMixinTemplateResponseMixinParting Thoughts View Right away, we can see that there is a base view class. RedirectView builds on the functionality from View. TemplateView also builds on View, and includes functionality from the ContextMixin and the TemplateResponseMixin. Here’s the code from View. class View: """ Intentionally simple … -
Bitbucket Pipelines and Ansible: Continuous delivery for your Django project
“Bitbucket Pipelines and Ansible: Continuous delivery for your Django project” was originally published as a guest post on Bitbucket Blog. Table of Contents Introduction Let the automation begin! Setup your pipelines Conclusion Introduction Ansible is a powerful tool for the automation of your server administration. It can be used both to install and configure the software needed for your application and to deploy a new version of your application. Bitbucket Pipelines are basically Docker containers, hosted in the Bitbucket infrastructure, that you can launch automatically to build or deploy your code. They can be attached to “events” happening in your repository, usually a push on one branch. These tools are complementary to each other, because on Ansible you can define exactly what you want to do on your target server to deploy your application, while on Bitbucket Pipelines you can configure and launch a container to perform the actual deploy. In a previous post I described how to deploy a Django project using Ansible. Here I explain how to make a step further and deploy a Django project using Bitbucket Pipelines and Ansible. The use-cases for deploying a Django project automatically are many. Even if your Django project has a …