Django community: Community blog posts RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
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: … -
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 … -
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 … -
Python Webviews with PyWebView
Have you ever wanted to use your python and web development skills to build cross platform desktop GUI apps? if yes then welcome to this tutorial where we'll show you how to use PyWebView to turn your web application built using python and client side technologies such as HTML, CSS and JavaScript into a standalone cross platform GUI application that runs under major operating systems such as Linux, Windows and MAC. You can also use any web framework based on python such as the so popular django framework or also the lightweight web development framework flask to build business logic of your app and then wrap the whole app, server and client side, into a desktop app . What is PyWebView? Before getting started with PyWebView let's first introduce it. PyWebView is an open source, cross platform and lightweight wrapper which provides you with a WebKit/MSHTML based webview inside a native GUI window which allows you to render HTML and CSS and execute JavaScript code just like any normal web browser. It's in fact a headless web browser that you can use to run a unique and default web app so the user gets the feeling of having an ordinary … -
Django News - Issue 4 - Jan 2nd 2020
News DjangoCon Europe 2020 The official site is now live! The dates are May 27-31 in Porto, Portugal. A call for talk submissions is coming soon... djangocon.eu Articles Improve your tests in Django with fakes and factories An introduction into unit testing in Django using faker and factories. hacksoft.blog Django Testing Tutorial A beginner-friendly guide to adding tests to any Django project. wsvincent.com Videos EuroPython 2019: Advanced pytest Explore pytest plugins to take your testing to the next level. youtu.be DjangoCon US 2019: The Unspeakable Horror of Discovering You Didn't Write Tests Melanie Crutchfield discusses real-world testing. youtu.be Podcasts Profitable Python Podcast: Professional Developer Resources for Django An interview with William Vincent on how to learn Django, using it in production, and more. anchor.fm Performance | Django Chat An overview of common and uncommon techniques for improving performance in a Django application. djangochat.com Projects model_bakery - Object factory for Django Model Bakery offers you a smart way to create fixtures for testing in Django. github.com django-test-migrations for testing schema and data migrations Test django schema and data migrations, including migrations' order. github.com django-test-plus - Useful additions to Django's default TestCase Useful additions to Django's default TestCase github.com This RSS feed … -
Our Top 19 Blogs of 2019
During the last year we gave our popular technical blog an official name: Developer Access. We published 32 posts on the blog, including technical how-to’s, conference information, web development best practices and detailed guides. Among all those posts, 19 rose to the top of the popularity list (based on total pageviews): 1. A Guide To Creating An API Endpoint With Django REST Framework: Our most popular blog post was published on February 1, and the title is self-explanatory. Adding an API endpoint can take considerable time, but with the Django REST Framework tools, it can be done more quickly. 2. How to Use Django Bulk Inserts for Greater Efficiency: If you have an application that needs to insert a lot of data into a Django model, it pays to "chunk" those updates to the database. 3. How to Switch to a Custom Django User Model Mid-Project: Django documentation recommends starting projects with a custom user model, but what if you didn’t? See how to add a custom user model to an existing project on Django 2.0+. 4. Coding for Time Zones & Daylight Saving Time — Oh, the Horror: It’s difficult to program correctly when using times, dates, time zones, … -
Our Top 19 Blogs of 2019
During the last year we gave our popular technical blog an official name: Developer Access. We published 32 posts on the blog, including technical how-to’s, conference information, web development best practices and detailed guides. Among all those posts, 19 rose to the top of the popularity list (based on total pageviews): 1. A Guide To Creating An API Endpoint With Django REST Framework: Our most popular blog post was published on February 1, and the title is self-explanatory. Adding an API endpoint can take considerable time, but with the Django REST Framework tools, it can be done more quickly. 2. How to Use Django Bulk Inserts for Greater Efficiency: If you have an application that needs to insert a lot of data into a Django model, it pays to "chunk" those updates to the database. 3. How to Switch to a Custom Django User Model Mid-Project: Django documentation recommends starting projects with a custom user model, but what if you didn’t? See how to add a custom user model to an existing project on Django 2.0+. 4. Coding for Time Zones & Daylight Saving Time — Oh, the Horror: It’s difficult to program correctly when using times, dates, time zones, … -
Adding Tags Field to Your Django App