Django community: RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
Lack of Volume Persistence in kartoza/postgis Docker Container
I have been working on a Django project that needs maps integration. I looked into using GeoDjango, but needed to upgrade my current Postgresql database to add PostGIS. This was totally outside what I’ve done before. Here’s how I got it to work. Starting Point My knowledge of Docker comes entirely from Will Vincent’s Django for Professionals book. In there, he builds from the official postgres Docker image within a docker-compose.yml file. version: "3.7" services: web: build: . command: python /code/manage.py runserver 0.0.0.0:8000 volumes: - .:/code ports: - 8000:8000 depends_on: - db db: image: postgres:11 volumes: - postgres_data:/var/lib/postgresql/data/ environment: - "POSTGRES_HOST_AUTH_METHOD=trust" volumes: postgres_data: Learning to Use GeoDjango Luckily, I found a great tutorial over at Real Python to introduce myself to GeoDjango: “Make a Location-Based Web App With Django and GeoDjango” by Ahmed Bouchefra. If you’re totally new to GeoDjango, I highly recommend this article. Ahmed even uses Docker in the article, so super win if you’re coming from Vincent’s book like me. PostGIS is an extension of PostgreSQL, so it’s kind of like a standard postgres database you might use for your Django project, but adds support for geographic objects. In the article, we use a new PostgreSQL image … -
Docker & Django Local Development
At the end of this post you will have: Set up Docker locally on your dev box. Run Django in a Docker container on the same dev box. Put a breakpoint and debugged the code! Pre-reqs Docker installed locally. For the purposes of this proof-of-concept, I used Docker Desktop. Minimal Docker Setup Our minimal docker setup will: run a relational database: Postgres directly run runserver command, which is what should be done for debugging purposes Our minimal docker setup will not: run a webserver, such as Nginx run gunicorn or uwsgi as a glue between the framework (Django code) and the webserver Since the objective is local development with Docker neither are needed. Minimal Docker Understanding If some Docker concepts are still not clear to you, do not worry. I have to search for new stuff myself all the time. When I started, I found this article really helpful: 6 Docker Basics You Should Completely Grasp When Getting Started. This article explains the differences and the relationship between: Containers Images Dockerfiles Volumes Port Forwarding Docker compose A lifesaver if you’re confused with the barrage of new Docker jargon. In this post we’ll be setting up: one Dockerfile, one Docker compose … -
[Django Rest Framework] Build Your First REST API with Python Django Rest Framework. (CRUD Operation)
Django REST framework is a powerful and flexible toolkit for building Web APIs. Some reasons you might want to use REST framework: The Web browsable API is a huge usability win for your developers. Authentication policies including packages for OAuth1a and OAuth2. Serialization that supports both ORM and non-ORM data sources. Customizable all the way down – just use regular function-based views if you don’t need the more powerful features. Extensive -
Django News - New Django 3.0.7 security release - Jun 5th 2020
News Django security releases issued: 3.0.7 and 2.2.13 Two new security patches. As ever, updating to the latest version of Django is highly recommended. Here is how to upgrade per the official docs. djangoproject.com 🖤 Thank you DRF, DSF, PSF, DEFNA, Wagtail, and everyone who showed their solidarity this week. #BlackLivesMatter Django Newsletter Articles Minimal Environment Variables Setup for Django A guide to configuring environment variables in Django projects. untangled.dev Django Class-Based Views in Diagrams Starting with each class-based views (CBVs) dispatch method, these diagrams break down each view until it reaches a response or redirects. brennantymrak.com Spell Check Django Templates, HTML, Markdown, and RST Files in VS Code How to add spellcheck support for HTML, Django/Jinja templates, reStructuredText (Sphinx), and markdown in VS Code. coderedcorp.com So you work in tech and want to support Black Lives Matter I've written this guide for my fellow non-Black folks in tech to help you figure out for yourself what specific actions you can take to help, now and into the future. dev.to Python Community Interview With Russell Keith-Magee Russell is a longtime member of the Django community. realpython.com Sponsored Link Two Scoops of Django 3.x: Best Practices for the Django Web Framework … -
[Django Rest Framework] OperationalError at /api/ no such table: django_session
If you are creating a API using Django Framework than you generally encounter this issue regarding django_session. Error: OperationalError at /api/ no such table: django_session If you want to follow along this blog and want to know how this error has occurred than visit the link below to setup your first Django Rest Framework: Building -
Designing A View - Building SaaS #59
In this episode, I focused on a single view for adding a course to a school year. This view is reusing a form class from a different part of the app and sharing a template. We worked through the details of making a clear CreateView. The stream started with me answering a question about how I design a new feature. I outlined all the things that I think through for the different kinds of features that I need to build. -
Two Scoops of Django - Daniel Feldroy
Two Scoops of Django 3.xDjango Crash CourseDaniel personal sitecookiecutter-djangodjango-crispy-formscookiecutter -
Introducing time-machine, a new Python library for mocking the current time
Whilst writing Speed Up Your Django Tests, I wanted to add a section about mocking the current time. I knew of two libraries such mocking, but I found it hard to pick one to recommend due to the trade-offs in each. So I delayed adding that section and shaved a rather large yak by writing a third library. This post is my introduction to the problem, the trade-offs with the other libraries, and how my new library, time-machine, tries to solve them. The next version of Speed Up Your Django Tests can now include a section on mocking time. The Problem It’s especially common in web projects to have features that rely on changes to the current date and time. For example, you might have a feature where each customer may only make an order once per day. For testing such features, it’s often necessary to mock the functions that return the current date and time. In Python’s standard library, these live in the datetime and time modules. There are various ways of doing this mocking: it can be done generically with unittest.mock, or in a more targeted fashion with a time-mocking library. Using unittest.mock works, but it’s often inaccurate … -
Extracting values from environment variables in tox
[Tox](https://tox.readthedocs.io) is a great tool for automated testing. We use it, not only to run matrix testing, but to run different types of tests in different environments, enabling us to parallelise our test runs, and get better reporting about what types of tests failed. Recently, we started using [Robot Framework](https://robotframework.org) for some automated UI testing. This needs to run a django server, and almost certainly wants to run against a different database. This will require our `tox -e robot` to drop the database if it exists, and then create it. Because we use [dj-database-url](https://pypi.org/project/dj-database-url/) to provide our database settings, our [Codeship](https://codeship.com/) configuration contains an environment variable set to `DATABASE_URL`. This contains the host, port and database name, as well as the username/password if applicable. However, we don't have the database name (or port) directly available in their own environment variables. Instead, I wanted to extract these out of the `postgres://user:password@host:port/dbname` string. My tox environment also needed to ensure that a distinct database was used for robot: {% highlight text %} [testenv:robot] setenv= CELERY_ALWAYS_EAGER=True DATABASE_URL={env:DATABASE_URL}_robot PORT=55002 BROWSER=headlesschrome whitelist_externals= /bin/sh commands= sh -c 'dropdb --if-exists $(echo {env:DATABASE_URL} | cut -d "/" -f 4)' sh -c 'createdb $(echo {env:DATABASE_URL} | cut -d "/" … -
Improving Code Confidently with Test-Driven Development
Looks at an example of how Test-Driven Development improves code quality. -
Stop Using datetime.now!
One of my favorite job interview questions is this: Write a function that returns tomorrow's date This looks innocent enough for someone to suggest this as a solution: import datetime def tomorrow() -> datetime.date: return datetime.date.today() + datetime.timedelta(days=1) This will work, but there is a followup question: How would you test this function? Before you move on.... take a second to think about your answer. One of these pigeons is a mockPhoto by Pedro Figueras Table of Contents Naive Approach Dependency Injection Dependency Injection in The Wild Injecting Functions Injecting Values When to Instantiate Injected Values Dependency Injection in Practice IP Lookup Assigning Responsibility Using a Service Changing Implementations Typing Services Using a Protocol Nondeterminism and Side-Effects Conclusion Naive Approach The most naive approach to test a function that returns tomorrow's date is this: # Bad assert tomorrow() == datetime.date(2020, 4, 16) This test will pass today, but it will fail on any other day. Another way to test the function is this: # Bad assert tomorrow() == datetime.date.today() + datetime.timedelta(days=1) This will also work, but there is an inherent problem with this approach. The same way you can't define a word in the dictionary using itself, you should not … -
Django News - Issue 25 - May 29th 2020
News Pipenv new release A major new release for Pipenv, the first since 2018! pypa.io PSF on Twitter: "The deadline to sign up to vote in @ThePSF upcoming election is May 31 AOE. To vote you need to be a PSF fellow, contributing, supporting and/or managing member. Membership info can be found here: https://t.co/W2H2ShVTDr" The PSF's annual elections are this June. If you are not a member and want to be one, sign up before May 31 AOE to vote this year. twitter.com Articles A tour of Django server setups See what Django in production visually looks like with diagrams. mattsegal.dev Abusing the Django Admin app Ken Whitesell walks us through automatically registering all of the models in our app while providing a custom ModelAdmin class to make their display useful. This technique is useful when dealing with a large number of models or a legacy database when you don't want to create 200 ModelAdmins. dev.to Handling SEO with Wagtail CMS Adonis Simo walks us through how to make Wagtail more SEO friendly. adonissimo.com How to Get Hired as a Django Developer Practical advice on finding work as a professional Python/Django programmer. learndjango.com Waiting in asyncio While not directly written … -
Book Review: Speed Up Your Django Tests
-
Book Review: Speed Up Your Django Tests
-
Bread and Butter Django - Building SaaS #58
In this episode, I worked on a views and templates. There are a number of core pages that are required to flesh out the minimal interface for the app. We’re building them. I began by showing the page that we were going to work on. I outlined the changes I planned to make, then we started. The first thing we added was data about the school year, the main model on display in the page. -
Django and Robot Framework
One of my colleagues has spent a bunch of time investigating and then implementing some testing using [Robot Framework](https://robotframework.com). Whilst at times the command line feels like it was written by someone who hasn't used unix much, it's pretty powerful. There are also some nice tools, like several Google Chrome [plugins](https://chrome.google.com/webstore/detail/robotcorder/ifiilbfgcemdapeibjfohnfpfmfblmpd) [that](https://chrome.google.com/webstore/detail/chrome-robot/dihdbpkpgdkioobahfpnkondnekhbmlo) will record what you are doing and generate a script based upon that. There are also other tools to [help build testing scripts](https://chrome.google.com/webstore/detail/page-modeller-selenium-ro/ejgkdhekcepfgdghejpkmbfjgnioejak). There is also an existing [DjangoLibrary](https://pypi.org/project/robotframework-djangolibrary/) for integrating with [Django](https://www.djangoproject.com/). It's an interesting approach: you install some extra middlewary that allows you to perform requests directly to the server to create instances using [Factory Boy](https://factoryboy.readthedocs.io/), or fetch data from Querysets. However, it requires that the data is serialised before sending to the django server, and the same the other way. This means, for instance, that you cannot follow object references to get a related object without a bunch of legwork: usually you end up doing another `Query Set` query. There are some things in it that I do not like: * A new instance of the django `runserver` command is started for each Test Suite. In our case, this takes over 10 seconds to start … -
Django and Robot Framework
One of my colleagues has spent a bunch of time investigating and then implementing some testing using [Robot Framework](https://robotframework.org). Whilst at times the command line feels like it was written by someone who hasn't used unix much, it's pretty powerful. There are also some nice tools, like several Google Chrome [plugins](https://chrome.google.com/webstore/detail/robotcorder/ifiilbfgcemdapeibjfohnfpfmfblmpd) [that](https://chrome.google.com/webstore/detail/chrome-robot/dihdbpkpgdkioobahfpnkondnekhbmlo) will record what you are doing and generate a script based upon that. There are also other tools to [help build testing scripts](https://chrome.google.com/webstore/detail/page-modeller-selenium-ro/ejgkdhekcepfgdghejpkmbfjgnioejak). There is also an existing [DjangoLibrary](https://pypi.org/project/robotframework-djangolibrary/) for integrating with [Django](https://www.djangoproject.com/). It's an interesting approach: you install some extra middleware that allows you to perform requests directly to the server to create instances using [Factory Boy](https://factoryboy.readthedocs.io/), or fetch data from Querysets. However, it requires that the data is serialised before sending to the django server, and the same the other way. This means, for instance, that you cannot follow object references to get a related object without a bunch of legwork: usually you end up doing another `Query Set` query. There are some things in it that I do not like: * A new instance of the django `runserver` command is started for each Test Suite. In our case, this takes over 10 seconds to start … -
JWTs and AI - David Sanders
David Sanders personal sitedjango-rest-framework-simplejwtDjangoCon US 2014 - JSON Web TokensDjangoCon US 2018 - Finally Understand Authentication with Django REST FrameworkPlease Stop Using LocalStorageEthereumUnsupervisedAI Superpowers book -
Saving GeoPoints Using Django Form
A while ago I was working on a project where I had a map which was part of a simple form. User can select a point on the map and submit. Form’s responsibility was to get the submitted data, validate it and save into database if everything is fine. I was using MySQL with GIS support. During the development I faced a couple of issues that I’d be addressing here and how did I fix them. Let’s begin! Consider the below example from django.contrib.gis.db import models class Location(models.Model): coordinate = models.PointField(blank=True, null=True) # many more fields If you see the Django generated migration file for this model, you will notice that the default value of srid parameter is 4326 although we never provided that explicitly in the model definition. This is how migration will look like. operations = [ migrations.CreateModel( name='Location', fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('coordinate', django.contrib.gis.db.models.fields.PointField(blank=True, null=True, srid=4326)), ], ), ] The default value of srid is being propagated from a base class BaseSpatialField, the PointField has been inherited from. We can always change this value as per requirements but in most cases default value would be sufficient. Let’s try to save some Geo coordinates through the … -
Debugging a Containerized Django App in VS Code
In this article, we'll show you how to configure Visual Studio Code (VS Code) to debug a Django app running inside of Docker. -
Creating a Kubernetes Cluster on DigitalOcean with Python and Fabric
This tutorial demonstrates how to automate the setup of a Kubernetes cluster with Python and Fabric on DigitalOcean. -
Django News - Virtual Meetups Galore - May 22nd 2020
News Test pip’s alpha resolver and help us document dependency conflicts – Bernard Tyers The pip team needs your help testing the dependency resolver. They specifically are looking for projects with complex dependencies which are prone to fail. If this sounds like you, please help them out. ei8fdb.org Admin accessibility - Google Groups Via Adam Johnson, "Interested in helping increase Django's accessibility? Chime in on this discussion started by Tom Carrick." google.com Articles Second-guessing the modern web A critical look at the SPA + API backend pattern currently en vogue. macwright.org Leverage the InnoDB architecture to optimize Django model design What every developer should know about InnoDB. medium.com Optimizing Django ORM Queries An under-the-hood look at Django's ORM. schegel.net The Fast Way to Test Django transaction.on_commit() Callbacks Performance tips on Django tests from Adam Johnson. adamj.eu Sponsored Link Two Scoops of Django 3.x: Best Practices for the Django Web Framework The long-awaited update covers various tips, tricks, patterns, code snippets, and techniques of Django best practices. feldroy.com Videos PyCon 2020 - Finite State Machine (FSM) in Django Calvin Hendryx-Parker on using django-fsm ti build quick, lightweight business workflows for Django applications. youtube.com PyCon 2020 - Big O No Chris Seto … -
Django Celery Tutorial Series
This Django Celery tutorial series teaches you how to use Celery with Django better. -
Django Celery Tutorial Series
This Django Celery tutorial series teaches you how to use Celery with Django better. -
Wagtail query for scheduled pages
Wagtail has "scheduled" pages that are not visible on the site. I think the interface is not ideal as you need to click the Publish button after setting a publication date on the Settings tab. I'm not sure how exactly the data models work, but the actual publication is handled by a management command and putting the publication date into the future after the post was published doesn't seem to unpublish it. I wanted to get a list of pages that were scheduled for publication, the query below might not handle all edge cases but shows how I got what I needed. Raw MyPageModel.objects.filter(go_live_at__isnull=False).not_live()