Django community: RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
Your last one-on-one: what to do instead of an exit interview
Say you agree with me that exit interviews are dangerous for employees, but you’re a kind, empathetic manager who’s built a team with high trust. You’d never dream of retaliation, are open to feedback – even if it’s harsh. You don’t want to put your departing direct in an uncomfortable or threatening position, but you’d like to know what they have to say! What then? -
Amsterdam python meetup
(Summaries of a talk at the April 2022 py.amsterdam meetup). Interesting takeaways from book 'Test Driven Development With Python' - Rok Klancar The book "test driven development with python" is available for free onine at https://www.obeythetestinggoat.com/ . The subtitle is test driven development (TDD) for the Web, with Python, Selenium, Django, JavaScript and pals... It is a quite thick book, but the author is very enthousiastic about programming and the subject of testing, so it actually very readable. The book consists of three parts: The basics of test driven development and django. So he starts out with explaining web development with django, just enough for having something real to test. Web development sine qua non. He's serious, at the end of part two you'll actually have deployed a web application. (And you'll have tested it, of course.) More advanced topics in testing. The core idea of test driven development is that you first write a (failing!) test and only then you write the code to get the tests to pass. The first test in the book is whether you have installed django (which you haven't yet). That sets the tone for the rest of the book :-) The whole time … -
Exit Interviews Are a Trap
It’s tempting to air your grievances at your exit interview. Don’t. There’s almost no upside to speaking up, and tremendous potential downside. Avoid exit interviews if you can. If you must go, be totally bland; say nothing negative. -
How to sort case insensitively with empty strings last in Django
Imagine you have something like this in Django: class MyModel(models.Models): last_name = models.CharField(max_length=255, blank=True) ... The most basic sorting is either: queryset.order_by('last_name') or queryset.order_by('-last_name'). But what if you want entries with a blank string last? And, you want it to be case insensitive. Here's how you do it: from django.db.models.functions import Lower, NullIf from django.db.models import Value if reverse: order_by = Lower("last_name").desc() else: order_by = Lower(NullIf("last_name", Value("")), nulls_last=True) ALL = list(queryset.values_list("last_name", flat=True)) print("FIRST 5:", ALL[:5]) # Will print either... # FIRST 5: ['Zuniga', 'Zukauskas', 'Zuccala', 'Zoller', 'ZM'] # or # FIRST 5: ['A', 'aaa', 'Abrams', 'Abro', 'Absher'] print("LAST 5:", ALL[-5:]) # Will print... # LAST 5: ['', '', '', '', ''] This is only tested with PostgreSQL but it works nicely. If you're curious about what the SQL becomes, it's: SELECT "main_contact"."last_name" FROM "main_contact" ORDER BY LOWER(NULLIF("main_contact"."last_name", '')) ASC or SELECT "main_contact"."last_name" FROM "main_contact" ORDER BY LOWER("main_contact"."last_name") DESC Note that if your table columns is either a string, an empty string, or null, the reverse needs to be: Lower("last_name", nulls_last=True).desc(). -
How to sort case insensitively with empty strings last in Django
Imagine you have something like this in Django: class MyModel(models.Models): last_name = models.CharField(max_length=255, blank=True) ... The most basic sorting is either: queryset.order_by('last_name') or queryset.order_by('-last_name'). But what if you want entries with a blank string last? And, you want it to be case insensitive. Here's how you do it: from django.db.models.functions import Lower, NullIf from django.db.models import Value if reverse: order_by = Lower("last_name").desc() else: order_by = Lower(NullIf("last_name", Value("")), nulls_last=True) ALL = list(queryset.values_list("last_name", flat=True)) print("FIRST 5:", ALL[:5]) # Will print either... # FIRST 5: ['Zuniga', 'Zukauskas', 'Zuccala', 'Zoller', 'ZM'] # or # FIRST 5: ['A', 'aaa', 'Abrams', 'Abro', 'Absher'] print("LAST 5:", ALL[-5:]) # Will print... # LAST 5: ['', '', '', '', ''] This is only tested with PostgreSQL but it works nicely. If you're curious about what the SQL becomes, it's: SELECT "main_contact"."last_name" FROM "main_contact" ORDER BY LOWER(NULLIF("main_contact"."last_name", '')) ASC or SELECT "main_contact"."last_name" FROM "main_contact" ORDER BY LOWER("main_contact"."last_name") DESC Note that if your table columns is either a string, an empty string, or null, the reverse needs to be: Lower("last_name", nulls_last=True).desc(). -
Django News - Django Day Copenhagen next week! - Apr 1st 2022
News PyCon US Keynote Speakers Keynote Speakers were announced for PyCon US, a hybrid event both online and in Salt Lake City, Utah, from April 27 to May 5, 2022. pycon.org Sponsored Ad Django Styleguide - A styleguide for Django projects, big and small. github.com Events Django Day Copenhagen 2022 A full day of Django talks in-person and with free streaming on April 8, 2022. djangoday.dk Articles You Probably Don’t Need Django’s get_user_model() Adam Johnson's take on a vanilla User import vs get_user_model(). adamj.eu Trailing URL Slashes in Django Why it's a good idea to add them and how Django works under-the-hood. learndjango.com The Real Reason To ‘Learn To Code’? Automating Your Life Don’t learn to code — learn to automate. medium.com Django Cheat Sheet An A-Z overview of setting up a new Django project. dev.to How to Make Django Raise an Error for Missing Template Variables Four techniques to check for missing template variables in Django. adamj.eu Updating A Many-To-Many Relationship In Django When working with Django's Many-To-Many relationship, forgetting to use the add method is a common gotcha. dev.to Tutorials Putting a Django site in maintenance mode How to create a Django maintenance mode middleware. medium.com Setting up … -
How to Be a Teapot in Django
HTCPCP, or Hyper Text Coffee Pot Control Protocol, was published as an April Fool’s joke 24 years ago today. It’s an HTTP extension for controlling coffee pots, and whilst it is a joke, it appears in various places around the web. HTCPCP adds one HTTP response code, “418 I’m a teapot”, which teapots can respond with if they are asked to brew coffee. As a registered status code, it has even made its way into the Python standard library’s HTTPStatus: In [1]: from http import HTTPStatus In [2]: HTTPStatus.IM_A_TEAPOT Out[2]: <HTTPStatus.IM_A_TEAPOT: 418> You can use HTTPStatus with Django to create a teapot page on your site. This will let you follow Google’s google.com/teapot page, so you can be one step closer to web scale! Throw a view in your project like: from http import HTTPStatus from django.shortcuts import render def teapot(request): return render( request, "teapot.html", status=HTTPStatus.IM_A_TEAPOT, ) Add a URL definition, such as at teapot/, and make a snazzy template. For a demo, I downloaded a sweet teapot GIF from gifcities.org: Great. That will be very useful, I’m sure. Fin May your tea always be perfectly brewed, —Adam -
PDF Courses Report - Building SaaS with Python and Django #132
In this episode, we added a final PDF report to the PDF bundle. This report was different than the other because there wasn’t a pre-existing HTML report to mimic. I built a report that shows all the completed course tasks for each student. -
PDF Courses Report - Building SaaS #132
In this episode, we added a final PDF report to the PDF bundle. This report was different than the other because there wasn’t a pre-existing HTML report to mimic. I built a report that shows all the completed course tasks for each student. -
Django the Good Parts - James Bennett
James’s personal websiteDjango in Depth talk at PyCon Montreal 2015Django the Good Parts at PyCon 2014 and slidesLet’s Build a Web Framework by Jacob Kaplan Moss at PyCon 2017How I’m Testing in 2020Against service layers in DjangoSupport the ShowThis podcast does not have any ads or sponsors. To support the show, please consider purchasing a book, signing up for Button, or reading the Django News newsletter. -
Django authentication with Nuxt
In this tutorial you'll learn how to integrate a Nuxt application with Django Rest Framework session based authentication. In a previous post I explained how to setup a Django Rest Framework project for session-based authentication. If you didn't read it already, I suggest to take a look at that post before proceeding: Django Rest Framework authentication: the easy way Read more... Here we'll make one step further and we'll integrate a Nuxt web application with the backend we created in that tutorial. I created an example project on GitHub you can use to follow along with this tutorial. Table of Contents Create a simple Nuxt application Use docker-compose with Traefik to serve both frontend and backend from the same domain Add a login page to the web app Use Vuex store to handle user authentication Require authentication in other pages Conclusions Create a simple Nuxt application For the sake of this tutorial I'll create a simple Nuxt application from scratch. $ yarn create nuxt-app drf-authentication-frontend yarn create v1.22.18 [1/4] Resolving packages... [2/4] Fetching packages... [3/4] Linking dependencies... [4/4] Building fresh packages... success Installed "create-nuxt-app@4.0.0" with binaries: - create-nuxt-app [###############################################################] 343/343 create-nuxt-app v4.0.0 ✨ Generating Nuxt.js project in drf-authentication-frontend ? Project … -
How to Make Django Raise an Error for Missing Template Variables
It’s all too easy to forget to pass a variable to your template, or make a typo in a variable name. Unfortunately, it can be quite hard to debug such mistakes, since Django’s default behaviour is to ignore the problem and render an empty string. In this post we’ll look at several techniques to check for missing template variables: Using the string_if_invalid option - the built-in option, but a bit cumbersome. With a Logging Filter that Raises Exceptions - this is my preferred technique. But it could break templates in your project that rely on the default missing variable beahviour. With a Logging Filter That Promotes Messages to Errors - you can use this to phase in the above exception-raising behaviour. With Jinja’s StrictUndefined - if you can switch your templates to render with Jinja, you can enable this option, as well as gain some other advantages. Alright, there’s plenty of soil to till, so let’s dig in! With The string_if_invalid Option The DTL has an option called string_if_invalid. Templates render this for missing variables, and it defaults to the empty string. The docs have more detail on how this works. You can configure it in your settings like so … -
Moving from Flask to FastAPI
This article, which is aimed for those interested in moving from Flask to FastAPI, compares and contrasts common patterns in both Flask and FastAPI. -
You Probably Don’t Need Django’s get_user_model()
Django’s authentication system, django.contrib.auth, provides a built-in User model class. You can swap this to a different class with the AUTH_USER_MODEL setting, most easily at the start of the project. django.contrib.auth also provides a function called get_user_model(). This retrieves the current user model class, whether it is the built-in one or a swapped one. You can use this function like so: from django.contrib.auth import get_user_model User = get_user_model() def some_function(): for user in User.objects.all(): ... But in most Django code, you do not need to use get_user_model(), and you can instead use a vanilla import: from example.core.models import User def some_function(): for user in User.objects.all(): ... A vanilla import has several advantages; It’s less code It’s consistent with other model imports Your editor may be able to use it for autocomplete Linters and type checkers can use the class to guard against more errors get_user_model() is intended for use in reuseable Django apps, such as those you install from PyPI. Such apps cannot know what the user model class is, so they need to use get_user_model() to refer to it. Within a project, where you know what your user model class is, you can directly import it. Internally, get_user_model() performs … -
Django News - Python 3.10.4 and 3.9.12 released - Mar 25th 2022
News Python 3.10.4 and 3.9.12 released Python 3.10.4 and 3.9.12 fixes a regression in builds for Red Hat Enterprise Linux 6. python.org bugs.python.org is moving to GitHub Issues The migration from bugs.python.org to GitHub Issues is scheduled to start on Friday 25th in the evening (UTC). python.org Celery Docs Migrated The Celery Project's Docs are now hosted on: https://docs.celeryq.dev celeryq.dev Sponsored Ad Django Styleguide - A styleguide for Django projects, big and small. github.com Articles Responsive table with Django and htmx This article is a nice introduction to a lot of technologies including django-tables2, django-filter, htmx, and django-htmx. dev.to PyTest with Django REST Framework: From Zero to Hero An in-depth, highly opinionated guide on writing tests with PyTest with Django REST Framework. It's a treat. dev.to The 10 Most-Used Django Packages | LearnDjango.com A look at the top 10 Django-related downloads according to PyPI. learndjango.com Videos Building SaaS with Python and Django #130 - PDF Progress Report Another episode from Matt Layman focused on adding a report to a PDF bundle and refactors. youtube.com Sponsored Link Error monitoring for Django Developers. Track and debug exceptions in record time so you can get back to doing what you love. honeybadger.io Projects … -
PDF Attendance Report - Building SaaS with Python and Django #131
In this episode, we added a third PDF report to the PDF bundle. The patterns from previous reports made this one go faster, but I hit some small snags with the output of the PDF being crushed together. -
PDF Attendance Report - Building SaaS #131
In this episode, we added a third PDF report to the PDF bundle. The patterns from previous reports made this one go faster, but I hit some small snags with the output of the PDF being crushed together. -
Flask vs Django (2022)
[Flask](https://palletsprojects.com/p/flask/) and [Django](https://www.djangoproject.com) are the two most popular [Python-based](https://www.python.org) web frameworks. Both are mature, open-source, and have many happy users. A natural question therefore is: which one to use? In … -
Trailing URL Slashes in Django
Among Django's many built-in features is [APPEND_SLASH](https://docs.djangoproject.com/en/4.0/ref/settings/#append-slash), which by default is set to `True` and automatically appends a slash `/` to URLs that would otherwise [404](https://en.wikipedia.org/wiki/HTTP_404). **Note**: The Chrome web … -
Django Best Practices: Models
Properly defining database models is arguably __the__ most important part of a new project, however Django provides us with tremendous flexibility around *how* we structure our models. There _is_ an … -
The 10 Most-Used Django Packages
Inspired by a past article on [The 22 Most-Used Python Packages in the World](https://medium.com/better-programming/the-22-most-used-python-packages-in-the-world-7020a904b2e), I teamed up with [Jeff Triplett](https://jefftriplett.com/about/) to investigate the top 10 Django packages based on [PyPI](https://pypi.org/) … -
Django News - Python Web Conference and Wagtail Space US next week - Mar 18th 2022
News Python 3.10.3, 3.9.11, 3.8.13, and 3.7.13 security and bugfix releases New maintenance releases with lots of security and bugfixes. python.org Python removes ‘dead batteries’ from standard library A slew of obsolete modules are slated to be dropped from Python, a possible prelude to a process for keeping the standard library cleaner. infoworld.com Sponsored Ad Django Styleguide - A styleguide for Django projects, big and small. github.com Events DjangoCon Europe 2022 Super early bird tickets are ending soon! All tickets include access to all 3 conference days (September 21 - 23) and the 2 sprints days (September 24 - 25). djangocon.eu Last call for PWC 2022 Last call for Python Web Conference (PWC) 2022 is next week, March 21-25, 2022. ti.to Last call for Wagtail Space US 2022 Wagtail Space US is virtual via Zoom or in-person at the Cleveland Public Library March 24-26. wagtail.space Django Day Copenhagen April 8th, 2022 Venue tickets are sold out but there will be a free streaming feed available. Sign up for a free ticket to be notified of the feed. djangoday.dk PyCon AU 2023 An announcement on the postponement of PyCon AU in 2022. The plan is for PyCon AU 2023 to be … -
PDF Progress Report - Building SaaS with Python and Django #130
In this episode, we added the next report to the PDF bundle. In the process of adding this report, I cleaned up some of the PDF generation code and refactored to some common patterns. -
PDF Progress Report - Building SaaS #130
In this episode, we added the next report to the PDF bundle. In the process of adding this report, I cleaned up some of the PDF generation code and refactored to some common patterns. -
Production APIs - Calvin Hendryx-Parker
Python Web Conference 2022Six Feet UpFLASHAmazon Fargatedjango-rest-knoxdjango-sesameSupport the ShowThis podcast does not have any ads or sponsors. To support the show, please consider purchasing a book, signing up for Button, or reading the Django News newsletter.