Django community: RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
Our 2019 Holiday Card
Happy Chanukah and Merry Christmas! We saw this giant gingerbread house at the post office today and knew it had to be our holiday card backdrop. We're off to attempt to bake cookies on our electric griddle now. We burned an earlier batch of snowman cookies and will just have to keep trying until we get them right. -
Testing and Designing Django Applications
Photo by DanganhfotoIn this post we’re going to walk through on how to design Django applications with testing in mind. These concepts are not new by any means, but they’ll probably come in handy for anyone going through an architecture design process using this framework as the system’s core.First, let’s discuss why writing automated tests is important. The importance of automated tests comes from the fact they replace manual testing. Every time you write a feature you end up running your app manually to see if it works, right? You run through the new code changes, but also it’s a good idea to test previous workflows, to check that everything still works correctly, even after your change. Well written automated tests are meant to do exactly that, but at the click of a button, several times faster than any person running through the code manually.Moreover, through Continuous Integration services and tools, we can run our project’s tests on every code change made to a code repository. If any change makes the tests fail, we’ll know instantly that code has a problem and should not reach any production environment. If a team wants to truly be Agile, and have its changes … -
Single Page Application + REST as an Abstraction: The Benefits of Decoupling Your Front & Back Ends
Monolithic, consolidated applications are not bad. These are your Rails apps, your Django apps, etc. — the ones where your server sends back HTML and assets. You’ve faithfully followed the Model-View-Controller (MVC) pattern as best you can and your concerns are “separated.” This design principle is not passé. It just so happens that for a long time, it was your only option. For some time now, there has been an alternative. An alternative. Keep calm and don’t trash your Django app just yet. What is a Decoupled Application? A decoupled application is one in which data is served from a REST or GraphQL API and consumed by a Single Page Application (SPA). They can live in a single codebase, or in two distinct ones (my preference). They can even be run on two entirely separate servers. You can even just plop your SPA into an S3 bucket and, with a bit of configuration, serve it directly from there. The defining feature of a decoupled application is the systemic separation of business logic from client-facing features. Your client-facing code is truly static, and your business logic never sees the light of day except through your API. Suddenly “front-end” and “back-end” can … -
Single Page Application + REST as an Abstraction: The Benefits of Decoupling Your Front & Back Ends
Monolithic, consolidated applications are not bad. These are your Rails apps, your Django apps, etc. — the ones where your server sends back HTML and assets. You’ve faithfully followed the Model-View-Controller (MVC) pattern as best you can and your concerns are “separated.” This design principle is not passé. It just so happens that for a long time, it was your only option. For some time now, there has been an alternative. An alternative. Keep calm and don’t trash your Django app just yet. What is a Decoupled Application? A decoupled application is one in which data is served from a REST or GraphQL API and consumed by a Single Page Application (SPA). They can live in a single codebase, or in two distinct ones (my preference). They can even be run on two entirely separate servers. You can even just plop your SPA into an S3 bucket and, with a bit of configuration, serve it directly from there. The defining feature of a decoupled application is the systemic separation of business logic from client-facing features. Your client-facing code is truly static, and your business logic never sees the light of day except through your API. Suddenly “front-end” and “back-end” can … -
Django News - Issue 2 - Dec 19th 2019
News Django security releases issued: 3.0.1, 2.2.9, and 1.11.27 This is an especially important security release that should be implemented ASAP. It fixes potential account hijacking via the password reset form. djangoproject.com 2020 Django Software Foundation Board Election Results The DSF Board handles legal/financial matters for Django. This year there are 7 members. djangoproject.com 2019 Malcolm Tredinnick Memorial Prize awarded to Jeff Triplett The Board of the Django Software Foundation is pleased to announce that the 2019 Malcolm Tredinnick Memorial Prize has been awarded to Jeff Triplett (@webology). djangoproject.com Articles How Django Works Behind the Scenes This article discusses how Django is structured: the non-profit "in charge" of it legally/financially and the core team of volunteer developers who manage the codebase. wsvincent.com Projects Is Pipenv dead? Pipenv is a popular Python packaging manager but dormant as of late...see also this great PyCon UK talk from Aaron Bassett on Python packaging in general for further context. github.com Looking for a pipenv alternative? Announcing Poetry 1.0.0 Poetry manages Python environments, packaging, and dependency management. python-poetry.org Events Announcing DjangoCongress 2020 DjangoCongress 2020 is happening in June 2020 in Nagano City, Japan. djangocongress.jp Ewa Jodlowska on Twitter: "There are less than 200 early bird … -
Performance
Weekly DjangoChat NewsletterDonald Knuth: The Art of Computer Programmingdjango-debug-toolbarassertNumQueriesEssential Image Optimzationdjango-extensionsDjango Forum Top 5 3rd party packages discussion -
Flask vs Django (2020)
A comparison of the two dominant Python-based web frameworks. -
How Django Works Behind the Scenes
On the Django community, organizations, and people behind the Django web framework. -
A Python and Preact app deployed on Heroku
Heroku is great but it's sometimes painful when your app isn't just in one single language. What I have is a project where the backend is Python (Django) and the frontend is JavaScript (Preact). The folder structure looks like this: / - README.md - manage.py - requirements.txt - my_django_app/ - settings.py - asgi.py - api/ - urls.py - views.py - frontend/ - package.json - yarn.lock - preact.config.js - build/ ... - src/ ... A bunch of things omitted for brevity but people familiar with Django and preact-cli/create-create-app should be familiar. The point is that the root is a Python app and the front-end is exclusively inside a sub folder. When you do local development, you start two servers: ./manage.py runserver - starts http://localhost:8000 cd frontend && yarn start - starts http://localhost:3000 The latter is what you open in your browser. That preact app will do things like: const response = await fetch('/api/search'); and, in preact.config.js I have this: export default (config, env, helpers) => { if (config.devServer) { config.devServer.proxy = [ { path: "/api/**", target: "http://localhost:8000" } ]; } }; ...which is hopefully self-explanatory. So, calls like GET http://localhost:3000/api/search actually goes to http://localhost:8000/api/search. That's when doing development. The interesting thing … -
A Python and Preact app deployed on Heroku
Heroku is great but it's sometimes painful when your app isn't just in one single language. What I have is a project where the backend is Python (Django) and the frontend is JavaScript (Preact). The folder structure looks like this: / - README.md - manage.py - requirements.txt - my_django_app/ - settings.py - asgi.py - api/ - urls.py - views.py - frontend/ - package.json - yarn.lock - preact.config.js - build/ ... - src/ ... A bunch of things omitted for brevity but people familiar with Django and preact-cli/create-create-app should be familiar. The point is that the root is a Python app and the front-end is exclusively inside a sub folder. When you do local development, you start two servers: ./manage.py runserver - starts http://localhost:8000 cd frontend && yarn start - starts http://localhost:3000 The latter is what you open in your browser. That preact app will do things like: const response = await fetch('/api/search'); and, in preact.config.js I have this: export default (config, env, helpers) => { if (config.devServer) { config.devServer.proxy = [ { path: "/api/**", target: "http://localhost:8000" } ]; } }; ...which is hopefully self-explanatory. So, calls like GET http://localhost:3000/api/search actually goes to http://localhost:8000/api/search. That's when doing development. The interesting thing … -
Announcing the Django News Newsletter
Why I'm launching a weekly newsletter on Django. -
Building a Django middleware (injecting data into a view's context)
What is a Django middleware and what is used for? I had an interesting use case recently where I needed to inject dynamic data into a Django view's context. The data didn't come from the database. I needed to serve different objects depending on the request META HTTP_ACCEPT_LANGUAGE, and to make that data accessible from a JavaScript frontend. Building a Django middleware has been the natural solution. A Django middleware is like a plug-in that you can hook into the Django's request/response cycle. In this post you'll learn how to build your own Django middleware and how to inject data into a view's context directly from the middleware. Setting up the project Create a new folder for the Django project and move into it: mkdir make-me-middleware && cd $_ Once inside create a Python virtual environment and activate it: python3 -m venv venv source venv/bin/activate Next up install Django: pip install django and create the new Django project: django-admin startproject make_me_middleware . Finally create a new Django app, I'll call mine middleware_demo: django-admin startapp middleware_demo And now let's get to work! Building the Django middleware A Django middleware can live inside a Python class implementing at least two dunder methods: … -
Security
Weekly DjangoChat NewsletterSecurity in DjangoDjango deployment checklistbleachHTML Escape Tool -
Automate your Telegram channel with a Django Telegram Bot
In this post I will guide you in a very interesting and funny development task: automate your Telegram channel with a Django Telegram Bot. Telegram is a popular instant messaging application. It’s not as widespread as Whatsapp, but its user base is constantly growing and according to Wikipedia: The number of monthly Telegram users as of October 2019 is 300 million people worldwide A very interesting aspect of the Telegram platform is the ability to create channels and bots to automate the broadcasting of messages to large audiences. These features open the possibility to better engage your users using Telegram, for example by sending them news, offers, etc. all from your existing Django web project! Let’s see how to do it. 1. Create a Telegram channel Nothing special here: you can do this from the Telegram app on your phone. Just tap “New Channel” on the Telegram main menu. Choose a name, a description and a nice “permalink” for your channel: 2. Create a Telegram Bot Now the funny part begins: search for a contact named “BotFather” on Telegram This is a bot to manage bots, a sort of meta-bot. 🙂 Start a chat with him and write /help … -
Automate your Telegram channel with a Django Telegram Bot
In this post I will guide you in a very interesting and funny development task: automate your Telegram channel with a Django Telegram Bot. Telegram is a popular instant messaging application. It’s not as widespread as Whatsapp, but its user base is constantly growing and according to Wikipedia: The number of monthly Telegram users as of October 2019 is 300 million people worldwide A very interesting aspect of the Telegram platform is the ability to create channels and bots to automate the broadcasting of messages to large audiences. These features open the possibility to better engage your users using Telegram, for example by sending them news, offers, etc. all from your existing Django web project! Let’s see how to do it. Table of Contents 1. Create a Telegram channel 2. Create a Telegram Bot 3. Make the bot an Admin for your Telegram channel 4. Install python-telegram-bot 5. Edit your Django settings to add Telegram parameters 6. Send a message to the channel from your Django code 7. Conclusions 8. Do you want more power for your Django Telegram bot? 1. Create a Telegram channel Nothing special here: you can do this from the Telegram app on your phone. Just … -
Angular 9 CRUD Tutorial: Consume a Python/Django CRUD REST API
Angular 9 is in pre-release! Read about its new features in this article and how to update to the latest Angular version in this article. You can also get our Angular 8 book for free or pay what you can. This tutorial is designed for developers that want to use Angular 9 to build front-end apps for their back-end REST APIs. You can either use Python & Django as the backend or use JSON-Server to mock the API if you don't want to deal with Python. We'll be showing both ways in this tutorial. Check out the other parts of this tutorial: Adding Routing Building Navigation UI Using Angular Material 8 This tutorial deals with REST APIs and routing but you can also start with basic concepts by following this tutorial (part 1 and part 2) instead which you'll build a simple calculator. If you would like to consume a third-party REST API instead of building your own API, make to check out this tutorial. You will see by example how to build a CRUD REST API with Python. The new features of Angular 9 include better performance and smaller bundles thanks to Ivy. Throughout this tutorial, designed for beginners, … -
E-Commerce - Jacob Rief
Weekly Django Chat Newsletterdjango-cmsdjango-shopdjango-angulardjango-websocket-redisdjango-admin-sortable2django-fsmWeb components -
Monitoring Kubernetes InitContainers with Prometheus
Kubernetes InitContainers are a neat way to run arbitrary code before your container starts. It ensures that certain pre-conditions are met before your app is up and running. For example it allows you to: - run database migrations with Django or Rails before your app starts - ensure a microservice or API you depend on to [is running](https://www.magalix.com/blog/kubernetes-patterns-the-init-container-pattern) - register your app with an external tool, such as Consul for [health checks and service discovery](https://learn.hashicorp.com/consul/getting-started/services) Unfortunately InitContainers can fail and when that happens you probably want to be notified because your app will never start. Kube-state-metrics exposes plenty of of Kubernetes cluster metrics for Prometheus. Combining the two we can monitor and alert whenever we discover container problems. [Recently](https://github.com/kubernetes/kube-state-metrics/pull/762), a pull-request was merged that provides InitContainer data. The metric `kube_pod_init_container_status_terminated_reason` tells us why a specific InitContainer failed to run; whether it's because it timed out or ran into errors. To use the InitContainer metrics deploy Prometheus and kube-state-metrics. Then target the metrics server in your Prometheus scrape_configs to ensure we're pulling all the cluster metrics into Prometheus: ```yaml - job_name: 'kube-state-metrics' static_configs: - targets: ['kube-state-metrics:8080'] ``` `kube_pod_init_container_status_terminated_reason` contains the metric label `reason` that can be in five different states: - … -
De-Google my life - Part 5 of ¯ (ツ)_/¯: Backups
Hello everyone! Welcome to the fifth post of my blog series “De-Google my life”. If you haven’t read the other ones you definitely should! (Part 1, Part 2, Part 3, Part 4). At this point, our server is up and running and everything is working 100% fine, but we can’t always trust that. We need a way to securely backup everything in a place where we can restore quickly if needed. Backup location My backups location was an easy choice. I already had a Wasabi subscription, so why not use it to save my backups as well? I created a new bucket on Wasabi, just for my backups and that was it. There is my bucket, waiting for my sweet sweet backups Security Just uploading everything to Wasabi wasn’t secure enough for me, so I’m encrypting my tar files with GPG. What is GPG? From their website: GnuPG (GNU Privacy Guard) is a complete and free implementation of the OpenPGP standard as defined by RFC4880 (also known as PGP). GnuPG allows you to encrypt and sign your data and communications; it features a versatile key management system, along with access modules for all kinds of public key directories. GnuPG, also … -
De-Google my life - Part 5 of ¯ (ツ)_/¯: Backups
Hello everyone! Welcome to the fifth post of my blog series “De-Google my life”. If you haven’t read the other ones you definitely should! (Part 1, Part 2, Part 3, Part 4). At this point, our server is up and running and everything is working 100% fine, but we can’t always trust that. We need a way to securely backup everything in a place where we can restore quickly if needed. Backup location My backups location was an easy choice. I already had a Wasabi subscription, so why not use it to save my backups as well? I created a new bucket on Wasabi, just for my backups and that was it. There is my bucket, waiting for my sweet sweet backups Security Just uploading everything to Wasabi wasn’t secure enough for me, so I’m encrypting my tar files with GPG. What is GPG? From their website: GnuPG (GNU Privacy Guard) is a complete and free implementation of the OpenPGP standard as defined by RFC4880 (also known as PGP). GnuPG allows you to encrypt and sign your data and communications; it features a versatile key management system, along with access modules for all kinds of public key directories. GnuPG, also … -
Listen Notes - Wenbin Fang
Weekly DjangoChat NewsletterListen NotesThe Boring Tech Behind a One-Person Internet CompanySearch From the Ground Up @DjangoCon US 2019Django Search Tutorial -
Models from JSON(B)
One of the things we'll often try to do is reduce the number of database queries. In Django, this is most often done by using the [`select_related`](https://docs.djangoproject.com/en/2.2/ref/models/querysets/#select-related) queryset method, which does a join to the related objects, thus returning the data from those, and then automatically creates the instances for those objects. This works great if you have a foreign key relationship (for instance, you are fetching Employee objects, and you also want to fetch the Company for which they work). Tt does not work if you are following the reverse of a foreign key, or a many-to-many relation. You can't `select_related` to get all of the employee's locations at which she can work, for instance. In order to get around this, Django also provides a [`prefetch_related`](https://docs.djangoproject.com/en/2.2/ref/models/querysets/#prefetch-related) queryset method, that will do a second query and fetch all of the related objects for all of the objects in the initial queryset. This is evaluated at the same time as the initial queryset, so works pretty well during pagination, for example. But, we don't always want _all_ of the objects: sometimes we might only want the most recent related object. Perhaps we have a queryset of Employee objects, and we want … -
6 ways to speed up your CI
#### Waiting for CI to finish slows down development and can be extremely annoying, especially when CI fails and you have to run it again. Let's take a look into approaches on how to speed up your CI and minimize the inefficient time spent by developers when waiting on CI to finish. We'll go through 6 different methods to speed up CI: - Makisu as your Docker build tool - Caching between Stages - Concurrent Jobs for each Stage - Running Tests across all CPUs - Parallel Tasks - Autoscaling CI Runners ## Building Docker Images with Makisu + Redis KV storage We've tried three approaches; default image building with Docker using `--cache-from`, Google's Kaninko with `--cache=true` and Uber's Makisu with Redis KV storage. Kaniko is Google's image build tool which enables building images without a docker daemon, making it great for building images within a Kubernetes cluster as it is not possible to run a docker daemon in a standard Kubernetes cluster. Makisu is also an image build tool that does not rely on the docker daemon making it work great within a Kubernetes cluster and it also adds a couple of new things as e.g distributed cache support … -
Django asynchronous tasks without Celery
In this blog post I will guide you to implement Django asynchronous tasks without Celery. First of all I will define that I mean with the term “asynchronous task”. What are Django asynchronous tasks? Suppose that you want to perform a long running task in your Django web app, but you want to give an immediate response to the user without waiting for the task to finish. The task could be: sending an email, building a report, making a request to an external web service, etc. The response given to the user usually is a confirmation message that the task has started. Every task that could take some time to complete should not block the request-response cycle between the user and your application. If you Google for “Django asynchronous tasks” probably you’ll be directed to Celery as the way to implement asynchronous tasks with Django. Please don’t get me wrong: there is nothing wrong in Celery. Many successful projects use Celery in production with success. I also used Celery in a couple of projects, and it works well. What I find annoying is the additional effort. Celery is yet another service to configure, launch and maintain. Enter the uWSGI spooler … -
Django asynchronous tasks without Celery
In this blog post I will guide you to implement Django asynchronous tasks without Celery. First of all I will define what I mean with the term “asynchronous task”. Table of Contents What are Django asynchronous tasks? Enter the uWSGI spooler Install uwsgidecorators package Create the directory for the “spool files” Create a tasks module in your Django application Call the asynchronous task from your Django view Configure uWSGI to use the spooler Conclusion What are Django asynchronous tasks? Suppose that you want to perform a long running task in your Django web app, but you want to give an immediate response to the user without waiting for the task to finish. The task could be: sending an email, building a report, making a request to an external web service, etc. The response given to the user usually is a confirmation message that the task has started. Every task that could take some time to complete should not block the request-response cycle between the user and your application. If you Google for “Django asynchronous tasks” probably you’ll be directed to Celery as the way to implement asynchronous tasks with Django. Please don’t get me wrong: there is nothing wrong in …