Django community: Community blog posts RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
How much faster is Cheerio at parsing depending on xmlMode?
Cheerio is a fantastic Node library for parsing HTML and then being able to manipulate and serialize it. But you can also just use it for parsing HTML and plucking out what you need. We use that to prepare the text that goes into our search index for our site. It basically works like this: const body = await getBody('http://localhost:4002' + eachPage.path) const $ = cheerio.load(body) const title = $('h1').text() const intro = $('p.intro').text() ... But it hit me, can we speed that up? cheerio actually ships with two different parsers: parse5 htmlparser2 One is faster and one is more strict. But I wanted to see this in a real-world example. So I made two runs where I used: const $ = cheerio.load(body) in one run, and: const $ = cheerio.load(body, { xmlMode: true }) in another. After having parsed 1,635 pages of HTML of various sizes the results are: FILE: load.txt MEAN: 13.19457640586797 MEDIAN: 10.5975 FILE: load-xmlmode.txt MEAN: 3.9020372860635697 MEDIAN: 3.1020000000000003 So, using {xmlMode:true} leads to roughly a 3x speedup. I think it pretty much confirms the original benchmark, but now I know based on a real application. -
Django News - 2022 Malcolm Tredinnick Memorial Prize awarded to Paolo Melchiorre - Dec 2nd 2022
News 2022 Malcolm Tredinnick Memorial Prize awarded to Paolo Melchiorre The Django Software Foundation Board is pleased to announce that the 2022 Malcolm Tredinnick Memorial Prize has been awarded to Paolo Melchiorre. Check out Paolo's blog post too. djangoproject.com 2023 DSF Board Election Results There were 30 candidates this year and the election results are in for next year. djangoproject.com Sponsored Ad Django Hosting by CodeRed Cloud At CodeRed, we’re striving to build the world’s easiest Django hosting platform. Go from polls tutorial to production in just a few minutes. Get started with a free account which includes a MariaDB or Postgres database, static + media hosting, and everything you need to run a Django site. No AWS, S3, Docker, or 3rd-party services required! codered.cloud Articles Test factory functions in Django A very well done guide to test factory functions in Django, why to use them, custom factory functions, what not to do, and more. lukeplant.me.uk Django Settings Patterns to Avoid Sage advice from Adam Johonson on some potential mistakes made with Django settings that you can avoid. adamj.eu The Essential Django Deployment Guide A comprehensive overview of what it takes to get your Django app into production and the … -
Learn Python By Example - Ghost Gobble Arcade Game
Learn Python By Example shows a simple Python exercise from Exercism. This problem illustrates booleans in Python. -
Blackjack and Bugs - Building SaaS with Python and Django #151
In this episode, we started with a Python exercise on Exercism that explored Blackjack. We wrote functions that handled some of Blackjack’s core rules. After the Python exercise, we focused on a bug in the Django app that fixed an edge case with some the teacher checklist feature. -
Programmatically control the matrix in a GitHub Action workflow
If you've used GitHub Actions before you might be familiar with the matrix strategy. For example: name: My workflow jobs: build: strategy: matrix: version: [10, 12, 14, 16, 18] steps: - name: Set up Node ${{ matrix.node }} uses: actions/setup-node@v3 with: node-version: ${{ matrix.node }} ... But what if you want that list of things in the matrix to be variable? For example, on rainy days you want it to be [10, 12, 14] and on sunny days you want it to be [14, 16, 18]. Or, more seriously, what if you want it to depend on how the workflow is started? Let's explain this with a scoped example You can make a workflow run on a schedule, on pull requests, on pushes, on manual "Run workflow", or as a result on some other workflow finishing. First, let's set up some sample on directives: name: My workflow on: workflow_dispatch: schedule: - cron: '*/5 * * * *' workflow_run: workflows: ['Build and Deploy stuff'] types: - completed The workflow_dispatch makes it so that a button like this appears: The schedule, in this example, means "At every 5th minute" And workflow_run, in this example, means that it waits for another workflow, in … -
Handy tool: pyupgrade
An advantage of open source is that, ideally, you get to work with other programmers from different companies and different backgrounds and different toolchains. They do things differently and so you can pick out the choice bits and start using them yourself. As an example, z3c.dependencychecker. A small tool I started in 2009 based on someone else's script. The goal is to look at all the imports in your python code (or apps in django settings files or...) and compare them to the list of requirements in setup.py. What's missing? What can be removed? The last few weeks, gforcada did some nice modernization/cleanup. Removing support for older python versions, for instance. And a move from travis-ci to github actions. So I looked at the new action workflow and saw something new. Black, isort, flake8: I know and use them. But pyupgrade? What is that? That's how I discovered pyupgrade. Handy tool to upgrade your code to newer python versions. By default, it removes python 2.x stuff that is no longer needed, like # -*- coding: utf-8 -*- at the top of your file. super() calls no longer need to mention the super class. The is not that flake8 tells you … -
Deserializing Django objects and many to many relationships
I recently had to restore some data from backups, and was using Django serializers to get the job done. Objects of one class were not deleted, but the relationships were set to None when the related objects were deleted. Here's how I restored the relationship information. -
Learn Python By Example - Currency Exchange
Learn Python By Example shows a simple Python exercise from Exercism. This problem illustrates numbers in Python and how to use the built-in math operators. Since this is the first video in this series, I also take a bit of time to show how to use Exercism along the way in case you would like to learn using Exercism as well. -
Django News - Django-related Deals for Black Friday and Cyber Monday 2022 - Nov 25th 2022
News Django-related Deals for Black Friday and Cyber Monday 2022 Eight different Django content creators or having deals right now. Check it out! adamj.eu Join DEFNA! Board Member Recruitment DEFNA (Django Events Foundation North America), not to be confused with the DSF (Django Software Foundation), is looking for another board member. Applications are due December 3rd. defna.org Sponsored Ad Lifetime License of SaaS Pegasus 50% Off This Week Only SaaS Pegasus is a Django boilerplate with built-in teams, billing, a modern front end, instant deployment and more. This week only you can get a lifetime license for Pegasus on unlimited sites for 50% off. See why more than 500 django developers have chosen Pegasus, and launch your next app faster than you dreamed possible. saaspegasus.com Events PyCon US on Twitter: Join the #PyConUS 2023 team by volunteering for our Online Experience Committee! Have some extra time to give? Join the #PyConUS 2023 team by volunteering for our Online Experience Committee! We are looking for helpful humans to help us run the #PyConUS 2023 virtual platform, Hubilo. twitter.com Articles PyDev of the Week: Sarah Abderemane Sarah is very active in Django and has recently been working to add a dark mode … -
2022 Malcolm Tredinnick Memorial Prize
The Django Software Foundation awarded me the 2022 Malcolm Tredinnick Memorial Prize. -
Django Settings Patterns to Avoid
This post is an adapted extract from my book Boost Your Django DX, available now. It has a 50% discount for the Black Friday sale, until the end of Cyber Monday (28th Nov), plus a further 50% off for those outside the GDP top 50. Here are some potential mistakes made with Django settings that you can avoid. Don’t Read Settings at Import Time Python doesn’t make a distinction between import time and run time. As such, it’s possible to read settings at import time, but this can lead to subtle bugs. Reading a setting at import time will use or copy its initial value, and will not account for any later changes. Settings don’t often change, but when they do, you definitely want to use the new value. For example, take this views file: from django.conf import settings ... page_size = settings.PAGE_SIZE def book_list(request): paginator = Paginator(Book.objects.all(), page_size) ... page_size is set to the value of the setting once, at import time. It won’t account for any changes made in tests, such as with override_settings: from django.test import TestCase, override_settings class BookListTests(TestCase): def test_pagination(self): with override_settings(PAGE_SIZE=2): response = self.client.get(...) The previous view code would not use the changed PAGE_SIZE … -
Butter CMS - Jake Lumetta
ButterCMSButterCMS Job Openings@jakelumettaSupport 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-related Deals for Black Friday and Cyber Monday 2022
Here are some Django-related deals for this year’s Black Friday (25th Nov) and Cyber Monday (28th Nov), including my own. My books My two books, Boost Your Django DX and Speed Up Your Django Tests, have 50% off their base prices. The deal stacks with the regional discount that offers a 50% discount for those living outside the GDP top 50. If you live in such a country, email me to get a total of 75% off the base prices. This discount is available until the end of Cyber Monday, “Anywhere on Earth” (AoE). That is, it’s live as long as it’s still Cyber Monday or before, in one time zone, anywhere on earth. Buy now: Boost Your Django DX Speed Up Your Django Tests (You can buy both in one transaction by clicking “Buy this” on one, then “Continue shopping” at the top right, which will let you add the other to your cart.) William Vincent’s Three Book Bundle William Vincent is the author of three fantastic Django books: Django for Beginners Django for APIs Django for Professionals He’s offering a 50% discount on the three book bundle, from $97 to $49. Buy it on Gumroad testdriven.io Four Course … -
Django News - 2023 Django Software Foundation Board Candidates - Nov 18th 2022
News 2023 DSF Board Candidates There are thirty candidates for the upcoming 2023 Django Software Foundation Board, the non-profit behind Django. DSF Individual Members vote on them. djangoproject.com Wagtail 4.1.1 Wagtail 4.1.1 fixes almost a dozen bugs. github.com Django Technical Board: Project Ideas, and beginning GSoC 2023. The 2023 Google Summer of Code has been announced and Django Fellow Carlton Gibson is soliciting input on project ideas. google.com Python 3.12.0 alpha 2 released Python 3.12.0a2 is the second of seven planned alpha releases. python.org Acceptable Use Policy · PyPI The Python Package Index (PyPI) added an Acceptable Use Policy. pypi.org Sponsored Ad Now Hiring Software Engineers Are you interested in building the next generation MLOPS Platform in Django? Apply today! Ampsight, Inc. is a small but quickly growing government services technology company located in Ashburn, Virginia, recently recognized by Inc. 5000 as one of the fastest growing companies in the United States. trinethire.com Articles Contributing to Django or how I learned to stop worrying and just try to fix an ORM Bug From attending the DjangoCon US sprints to tackling an ORM bug, Ryan Cheley walks us through the entire process. ryancheley.com Towards Inbox Zero: An update on Django's open … -
Development Cached Templates - Building SaaS with Python and Django #150
In this episode, I ran through a Python exercise on Exercism that focused on strings. Then we moved onto the homeschool app and fixed an issue with templates that were cached during development because of changes in Django 4.1. -
Intervista Pythonista - Ep 31
Python Milano presents Intervista Pythonista. We give a voice to Italian Pythonistas and get to know them closely with short interviews. Presented by Marco Santoni and Cesare Placanica. -
First impressions trying out Rome to format/lint my TypeScript and JavaScript
Rome is a new contender to compete with Prettier and eslint, combined. It's fast and its suggestions are much easier to understand. I have a project that uses .js, .ts, and .tsx files. At first, I thought, I'd just use rome to do formatting but the linter part was feeling nice as I was experimenting so I thought I'd kill two birds with one stone. Things that worked well It is fast My little project only has 28 files, but time rome check lib scripts components *.ts consistently takes 0.08 seconds. The CLI looks great You get this nice prompt after running npx rome init the first time: Suggestions just look great Easy to understand and needs no explanation because the suggested fix tells a story that means it's immediately easy to understand what the warning is trying to say. It is smaller If I run npx create-next-app@latest, say yes to Eslint, and then run npm I -D prettier, the node_modules becomes 275.3 MiB. Whereas if I run npx create-next-app@latest, say no to Eslint, and then run npm I -D rome, the node_modules becomes 200.4 MiB. Editing the rome.json's JSON schema works in VS Code I don't know how this … -
unittest’s new context methods in Python 3.11 (with backports)
Python 3.11 only made one change to unittest, but it’s a good one: context manager methods. These methods can simplify setup and teardown logic in many cases, such as dynamic use of unittest.mock. In this post we’ll look at a couple fo examples using the new methods, and a backport you can copy-paste into your projects, in plain and Django flavours. A time-saving example Take this test case, which uses my time-mocking library time-machine to fix the current microsecond to 0 (for whatever reason): import datetime as dt from unittest import TestCase import time_machine class ExampleTests(TestCase): def setUp(self): now = dt.datetime.now().replace(microsecond=0) self.traveller = time_machine.travel(now) self.traveller.start() self.addCleanup(self.traveller.stop) super().setUpClass() def test_microsecond(self): self.assertEqual(dt.datetime.now().microsecond, 0) It takes three steps to set up the mocking: Create a “traveller” by calling time_machine.travel(). Make it mock time by calling its start() start method. Schedule un-mocking by passing its stop() method to addCleanup(). This is verbose. Plus, it’s all too easy to too easy to forget the cleanup step, leading to test pollution. You can merge those three steps into one with Python 3.11’s enterContext() method: import datetime as dt from unittest import TestCase import time_machine class ExampleTests(TestCase): def setUp(self): now = dt.datetime.now().replace(microsecond=0) self.traveller = self.enterContext(time_machine.travel(now)) super().setUpClass() def … -
Python Type Checking
This article looks at what type hints are and how they can benefit you. We'll also dive into how you can use Python's type system for type checking. -
Moving data including deletions between the same Django app running in different environments
Moving data including deletions between the same Django app running in different environments Many projects use different environments to stabilize the code; they have a production environment which is actually seen by users, a stage where code is tested in an environment close to production and maybe several additional environments where more experimental code is published or developed, either on internal or protected webservers or maybe on the workstations of developers themselves. The same process may be desired for content; an environment where the content lives which is seen by users and other environments where the content can be changed without impacting the production environment. When using Django and the Django admin site there is no ready-made solution for doing this and maybe there shouldn’t be. Django’s serialization framework and its shortcomings for the use case outlined above Django comes with a serialization framework and with the dumpdata and loaddata management commands which can be used to dump and load data, e.g. to and from JSON. It’s also easily possible to specify a list of primary keys on the command line if you only want to dump a subset of the existing data. When loading this subset data which isn’t … -
Django News - Detecting N+1 Issues - Nov 11th 2022
Sponsored Ad Now Hiring Software Engineers Are you interested in building the next generation MLOPS Platform in Django? Apply today! Ampsight, Inc. is a small but quickly growing government services technology company located in Ashburn, Virginia, recently recognized by Inc. 5000 as one of the fastest growing companies in the United States. trinethire.com Events Python Web Conf 2023 CFP Open until Nov. 15 The 5th annual Python Web Conf will be hosted virtually from March 13-17, 2023. With sessions for mid-level and advanced developers alike, international experts will share cutting-edge presentations on topics including AI/ML, Big Data, CI/CD, Serverless, Security, Containers and more. Speakers of all backgrounds are welcome and encouraged to submit a talk. papercall.io PyCon US Online Experience Committee Volunteer Application PyCon US is introducing the Online Experience Committee to enhance the PyCon US virtual experience and boost engagement for our online attendees. If you are interested in volunteering, they are looking for people who are excited about online events. google.com DjangoCon US 2022 Official Photos Many, many photos of the event and all its participants. flickr.com Articles How Sentry uncovered an N+1 issue in djangoproject.com Using Sentry to detect N+1 issues in Python code and specifically on … -
Deploying a Django App to Fly.io
This tutorial looks at how to deploy a Django application to Fly.io. -
DjangoCon US 2022 Recap
DjangoCon US 2022DjangoCon Europe 2022 VideosAbout my proposal for the Django Core SprintsPersonal Thoughts on the Django Software Foundation Board’s Future Benchmarks for SQLite 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. -
Deploy Django + PostgreSQL on Fly.io
In this guide we will develop a Django Todo application locally and then deploy it on [Fly.io](https://fly.io) with a [Postgres production database](https://fly.io/docs/reference/postgres/). There are a number of steps needed to … -
Professionalism: You should maintain a transition file
When you change jobs, ideally you’ll have the opportunity to brief your successor directly. But that isn’t always possible: you might get fired or laid off, you might leave for another job without a clear successor named before your last day, you might have to take sudden medical leave, etc. Situations like that will be disruptive, it’s unavoidable, but a transition file will help minimize that disruption.