Django community: Community blog posts RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
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
-
Adding Tags Field to Your Django App
A detailed post describing how to add tagging functionality to your django app If you’ve been browsing tutorials about Django on YouTube, you must have stumbled across dozens of video series teaching you how to create a blog app using Django. You might have already finished watching that series, and now you might be wondering about some of the more advanced functionalities you can add to your blog app, first thing that would come to your mind would be Tags, since you’ve been reading dozens of blog post about Django, and one thing that you would have seen at the end of the article would be tags. Today I will be teaching you how to add tags field to your blog app in Django. Django Taggit We’ll be using a package named django-taggit, activate your project’s virtualenv and install this package as follows: $ pip install django-taggit Add taggit to your project’s INSTALLED_APPS inside settings.py Run python managy.py migrate, this will create tables for the models that comes with this package. Then to any model you want tagging on, do the following: from taggit.managers import TaggableManager class Post: title = models.CharField(max_length=255) content = models.TextField() tags = TaggableManager() NOTE: If you … -
Django News - Issue 3 - Dec 26th 2019
News Happy holidays everyone! As 2019 comes to a close, we'd like to wish everyone a wonderful holiday season and a happy new year! Thanks, Jeff and William PS: We are excited about 2020 and have big plans. Django Newsletter Python 3.8.1, 3.7.6, 3.6.10, and 3.9.0a2 are now available! python.org When is the last release of Python 2.7 coming out? On January 1, 2020, Python 2.7.18 will enter "code freeze" and will officially be released in April 2020 per PEP 373. This will be the last official version of Python 2.7. readthedocs.io Django Channels 2.4.0 is out! Channels 2.4 brings compatibility with Django 3.0s new async features. Please note: support was dropped for all Django versions before 2.2, which is the current LTS. readthedocs.io Wagtail 2.7 LTS wagtail.io Sponsored Links Test-Driven Development with Django, Django REST Framework, and Docker An advanced-beginner course on Django. testdriven.io Articles When to switch to Python 3.8 pythonspeed.com Flask vs Django (2020) A comparison of the two most popular Python-based web frameworks. wsvincent.com Automate your Telegram channel with a Django Telegram Bot Have you ever wanted to create your own chat bot with Django and Telegram? Learn how with this guide. guguweb.com Podcasts The Django … -
Tox, Travis CI and Coveralls: my config
There are already many tutorials on getting Tox, Travis CI and Coveralls to work well together... so I've gone and written another one. Read more to find out why (Hint: I was very keen on DRY). -
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. -
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. -
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