Django community: RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
Ruby on Rails & Django- David Heinemeier Hansson
David Heinemeier Hansson personal siteRuby on Rails -
How to use Heroku Pipeline
In this Heroku tutorial, I will talk about how to setup Heroku pipeline and how to use it. -
Embedding Videos Into Django Project
Have you ever tried to embed a video into your Django website so find that it wouldn’t work? Well, I have. Let’s go over the solution I found to this problem. The problem I’ve been making an app for some other people that I mentor. I decided it would be easier on me and more beneficial to my mentees if I answered their questions in video instead of writing; speeds things along and I can increase my output while conveying my non-verbal communication. I expected to just hold a Vimeo URL in the database and plug it into a set embed code in my template. Long story short: that didn’t work. So I went searching for a new answer. Answer: django-embed-video I found the solution in a lovely plugin package, django-embed-video. Install the package pip install django-embed-video Change settings.py INSTALLED_APPS = ( ... 'embed_video', ) Add the field to your model # app_name/models.py from django.db import models from embed_video.fields import EmbedVideoField class ModelName(models.Model): video = EmbedVideoField(blank=True) # an empty field is fine for me Load the tags at the top of your template {# for me, this was app_name/model_name_detail.html #} {% load embed_video_tags %} Call the video in the template … -
DjangoCon US 2019: Python & Django in San Diego!
We are back to San Diego!! Our team will be joining DjangoCon US's conference, one of the biggest Django events in the world. For this year, we'll be giving two talks: Pull Requests: Merging good practices into your project and Building effective Django queries with expressions Here is the slide from the talk we gave during the conference: Pull Re -
Deploying Django on Docker to Heroku with OpenCV
Building off [this post](https... -
Learning Django Past the Documentation
After you’ve read documentation from a bunch of different projects, you’ll come to realize the Django Documentation is fantastic. It’s concise and contains lots of examples and suggested use cases, which I have found invaluable as a n00b. But sometimes it still leaves you with wtf feels. When you get to a point where something you think you need to know is far outside your scope, what do you do? Realize you probably don’t need it Most of the books, well-written documentation, and tutorials you’ve read and watched cover the parts of Django that are most useful or best practice. If you haven’t heard about something, it’s because most people aren’t using it. And there’s usually a reason for that. You may want to ask a professional’s opinion. Google search There may be a rando somewhere who truly DOES think this is useful to know, and they may have written something about it somewhere. There are two main types of search results in my head: Private sites, such as this one Stack Overflow questions A third, less likely scenario is that your google search actually points you to a “hidden-to-you” part of the documentation that actually answers your question. Take … -
Django on Docker - A Simple Introduction
This is a simple tutorial but ... -
Making Your Own Email Templates in Django
I’m looking to make my app notify the user when their object is updated by an admin. Problem is that I had no idea how to use my own email templates. But I got it working! Here’s how you can put it into your Django project. I’m using Django 2.2.5. Email settings in settings.py There are two useful ways that I know of to send emails: Logging directly to the console window Sending via SMTP The first is much quicker, but doesn’t actually send emails. It’s good for testing. The second is what you need if you need to actually send emails to a user’s inbox. Send to console To log emails to the console, you just have to set EMAIL_BACKEND in settings.py. EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' Here’s the documentation for reference. Send via SMTP There are a few settings you’ll need to take care of if you want to send live SMTP emails: EMAIL_BACKEND EMAIL_HOST EMAIL_PORT EMAIL_HOST_USER EMAIL_HOST_PASSWORD EMAIL_USE_TLS or EMAIL_USE_SSL If you’re ready to give this a try, I would recommend making an account on SendGrid. Even when your free trial expires, you’ll have 100 free emails to send every day. Not enough to run a big project, but … -
Working around memory leaks in your Django application
Several large Django applications that I’ve worked on ended up with memory leaks at some point. The Python processes slowly increased their memory consumption until crashing. Not fun. Even with automatic restart of the process, there was still some downtime. Memory leaks in Python typically happen in module-level variables that grow unbounded. This might be an lru_cache with an infinite maxsize, or a simple list accidentally declared in the wrong scope. Leaks don’t need to happen in your own code to affect you either. For example, see this excellent write-up by Peter Karp at BuzzFeed where he found one inside Python’s standard library (since fixed!). Workarounds The below workarounds all restart worker processes after so many requests or jobs. This is a simple way to clear out any potential infinitely-accumulating Python objects. If your web server, queue worker, or similar has this ability but isn’t featured, let me know and I’d add it! Even if you don’t see any memory leaks right now, adding these will increase your application’s resilience. Gunicorn If you’re using Gunicorn as your Python web server, you can use the --max-requests setting to periodically restart workers. Pair with its sibling --max-requests-jitter to prevent all your workers … -
Simple Docker Container Tutorial
Docker is all the rage. I cove... -
Django 3.0 Preview
Django 3.0 alpha downloadASGI (Asynchronous Server Gateway Interface)PyCon AU 2019 - Just Add Await: Retrofitting Async into Django by Andrew GodwinDjangoCon Europe 2019 - Sketching out a Django Redesign by Tom Christieforum.djangoproject.com -
Postgres ENUM types in Django
Postgres has the ability to create custom types. There are several kinds of `CREATE TYPE` statement: * composite types * domain types * range types * base types * enumerated types I've used a metaclass that is based on Django's Model classes to do Composite Types in the past, and it's been working fairly well. The current stuff I have been working on made sense to use an Enumerated Type, because there are four possible values, and having a human readable version of them is going to be nicer than using a lookup table. In the first iteration, I used just a TEXT column to store the data. However, when I then started to use an `enum.Enum` class for handling the values in python, I discovered that it was actually storing `str(value)` in the database, rather than `value.value`. So, I thought I would implement something similar to my Composite Type class. Not long after starting, I realised that I could make a cleaner implementation (and easier to declare) using a decorator: {% highlight python %} @register_enum(db_type='change_type') class ChangeType(enum.Enum): ADDED = 'added' CHANGED = 'changed' REMOVED = 'removed' CANCELLED = 'cancelled' ChangeType.choices = [ (ChangeType.ADDED, _('hours added')), (ChangeType.REMOVED, _('hours subtracted')), (ChangeType.CHANGED, … -
Django 3.0 Alpha Released
Big news! The alpha version of Django 3.0 has been released. This is not suitable for production! BUT, this is suitable for sandboxing. Feel free to install it and see if it breaks anything. And if you notice something, make sure to submit a bug! Or you can try to get up and running with ASGI. How exciting! Here’s a link to the official release statement. And here’s a link to the Django 3.0 release notes. -
Limit Access to Only Superusers in Django
So in my other life as a strength and conditioning coach, I have a few online mentees. They basically email me questions and I answer them. I’m using Django to make an interface for them to submit questions and see all the other questions that other members have asked. Plus adding some searching so the old stuff doesn’t just die. I am using the generic UpdateView to answer the questions. This view is marking the question “published” and storing my answer in the database. My issue is that I don’t want the users to be able to see this because then they could be adding answers to questions and people are paying for ME to answer the question. This entails two major parts: Adding a link to the UpdateView in the DetailView Blocking permissions to access the form with UserPassesTestMixin Let’s walk through it. Add link to UpdateView Order doesn’t particularly matter here. You’ll have to do each step. I like to start with the URL, so first, we need to add the URL to our UpdateView to our urls.py file. # app_name/urls.py from .views import AppNameUpdateView # make sure to import your view urlpatterns = [ # other URL … -
Django Has Changed Over Time
Repetition is king for learning. Working with models, views, and templates over and over gets repetitive, but that’s a good thing. Have you ever seen a tutorial online and thought, “That code doesn’t look very familiar.” No repetition, no knowledge. Usually these differences are not WAY off. The similarities give you sense for what you have to do. But after you give it a shot, you get an error. One of the biggest reasons to start your web development journey by learning Django is because it is OLD. Old things are set in their ways. They won’t change so much on you as you slog along. In fact, this was one of the biggest reasons I decided to pick Django as my learning language. You can be sure that you won’t lose much on your investment because what you learn will still be relevant in the future. But still, things have changed in Django. And this makes it difficult to learn on the internet. There’s a lot of wasted time. And time is more valuable than money. To help save you time, I want to outline some of the major differences I’ve run into while learning Django. Note When reading … -
How "Export to Excel" Almost Killed Our System
A few weeks ago we had some trouble with an "Export to Excel" functionality in one of our systems. In the process of resolving this issue, we made some interesting discoveries and came up with original solutions. This article is inspired by the actual issue we used to track this incident over a period of two days. We go through the process of identifying the problem, experimenting and benchmarking different solutions until eventually deploying to production. These are the main takeaways described in this article: Generating xlsx files can consume significant amount of resources. Under some circumstances better performance can be gained by not using prefetch_related. pyexcelerate is a fast package for creating simple Excel files. tablib (and django-import-export) can be patched to use pyexcelerate and produce excel files faster. Table of Contents Exporting a QuerySet to Excel Using django-import-export Finding the Best File Format Improving the Query Replacing prefetch_related with Subquery and OuterRef Using an Iterator Simplifying the Query Manual Prefetch Trouble in Paradise Using a Different Excel Writer A Faster Excel Writer in Python Patching tablib Results Summary Seifa How a server must feel when asked to produce an Excel file A few weeks ago we started getting … -
Django shell_plus with Pandas and Jupyter Notebook
The Django shell provides an environment where developers can interact with the database via Django's ORM. While the shell is great for basic interactions, it quickly becomes laborious to work in, be it due to manual imports, having to scroll through shell history to repeat commands, or working with / viewing queries returning more than, say, 20 records. These issues, and more, can be remedied by interacting with the ORM in Jupyter notebooks, using Pandas. Our environment will be setup inside a virtual environment using Django 2.2. Once inside the virtual environment, install IPython, Jupyter, Pandas, django-extensions, and django-pandas. pip install ipython jupyter pandas django-extensions django-pandas django-extensions needs to be placed into INSTALLED_APPS in your settings.py file. INSTALLED_APPS = [ ... 'django_extensions', ] Once installed, then run python manage.py shell_plus --notebook shell_plus is Django shell with autoloading of the apps database models and subclasses of user-defined classes. The --notebook argument tells shell_plus to open in a Jupyter notebook. You will notice that a new Jupyter tab opens in your browser. In the upper right corner click on the New drop-down and select Django Shell-Plus. A new tab will open and you will see your new Jupyter notebook. Import … -
How to deploy Python project to Heroku in Gitlab CI
In this Heroku tutorial, I will talk about how to deploy python project to Heroku in Gitlab CI -
Why I Wanted to Learn to Code
Every answer is at your fingertips, but time is scarce. What will you choose to do? I encourage everyone I meet to take the mindset of the “lifelong learner”. If you aren’t a little embarrassed by who you were ten years ago, then you’re probably not improving. And the best way to help the world is by first helping yourself. But why coding? Backstory My first love was actually ice hockey. I grew up in Michigan, USA, where hockey is a big deal. I’ve been on ice since I was three years old. Hockey taught me work ethic. I couldn’t be the best if I didn’t work hard. But it also taught me what it’s like to feel superior, to win. And I had to learn how to lose. The hard way. Ego fragility paved the path of lifelong learning. The only reason you would seek to change is if you’d like to see something different about yourself. Practice I got a heavier dose of repeated practice just before high school when I picked up my first guitar. How cool would it be to make those sounds that all my favorite bands make. There’s that ego again. Guitar is much … -
Django Heroku Tutorial Series
In this Django Heroku tutorial series, I will talk about how to deploy, scale, monitor and optimize Django project on Heroku. -
A Single File Asynchronous Django Application
Django 3.0 alpha 1 came out this week. It introduces ASGI support thanks to lots of hard work by Andrew Godwin. Thanks to a question from Emiliano Dalla Verde Marcozzi on the shiny new Django forums, I decided to play with it a bit. I adapted my “single file Django app” to ASGI. Here’s how to try it yourself. First, create a new virtual environment (I’m using Python 3.7.4) and install requirements: $ python -m venv venv $ source venv/bin/activate $ pip install django==3.0a1 daphne==2.3.0 Then create a new file app.py: import html import os import sys from django.conf import settings from django.core.asgi import get_asgi_application from django.http import HttpResponse from django.urls import path from django.utils.crypto import get_random_string settings.configure( DEBUG=(os.environ.get("DEBUG", "") == "1"), # Disable host header validation ALLOWED_HOSTS=["*"], # Make this module the urlconf ROOT_URLCONF=__name__, # We aren't using any security features but Django requires this setting SECRET_KEY=get_random_string(50), ) def index(request): name = request.GET.get("name", "World") return HttpResponse(f"Hello, {html.escape(name)}!") urlpatterns = [path("", index)] application = get_asgi_application() if __name__ == "__main__": from django.core.management import execute_from_command_line execute_from_command_line(sys.argv) Then run it under Daphne: $ daphne app:application Visit http://localhost:8000/?name=Django%20user in your browser to see “Hello, Django user!” Async support in Django 3.0 is the first … -
How I Import Python's datetime Module
Python’s datetime module risks a whole bunch of name confusion: It contains a class also called datetime. When you read the word datetime, you can’t be sure it’s the module or the class. It contains a time class. This can be confused with the time module, or the time module’s time() function. It contains a class called timezone. In a Django project this can be confused with the django.utils.timezone module For these reasons, I use this import idiom and recommend you do too: import datetime as dt Rather than any of: import datetime from datetime import datetime # or time, timezone Then in your code, dt.datetime, dt.time, and dt.timezone will be unambiguous. Fin Hope this helps you save time understanding code, —Adam -
Learning to Love Django Tests - Lacey Williams Henschel
@laceynwilliams on TwitterLacey Williams Henschel personal siteGoodby Print Statement, Hello Debugger! - Nina Zakharenko @ Pycon AU 2019OpenSource.com articlesDjango Admin/Templates courses on TreehouseHow to Add Django Models to the Wagtail AdminSHAMELESS PLUGSWilliam's books on DjangoCarlton's website Noumenal -
Django shell_plus with Pandas and Jupyter Notebook
The Django shell provides an environment where developers can interact with the database via Django's ORM. While the shell is great for basic interactions, it quickly becomes laborious to work in, be it due to manual imports, having to scroll through shell history to repeat commands, or working with / viewing queries returning more than, say, 20 records. These issues, and more, can be remedied by interacting with the ORM in Jupyter notebooks, using Pandas. Our environment will be setup inside a virtual environment using Django 2.2. Once inside the virtual environment, install IPython, Jupyter, Pandas, django-extensions, and django-pandas. pip install ipython jupyter pandas django-extensions django-pandas django-extensions needs to be placed into INSTALLED_APPS in your settings.py file. INSTALLED_APPS = [ ... 'django_extensions', ] Once installed, then run python manage.py shell_plus --notebook shell_plus is Django shell with autoloading of the apps database models and subclasses of user-defined classes. The --notebook argument tells shell_plus to open in a Jupyter notebook. You will notice that a new Jupyter tab opens in your browser. In the upper right corner click on the New drop-down and select Django Shell-Plus. A new tab will open and you will see your new Jupyter notebook. Import … -
Mercurial mirrors updates
Time to clean up my mercurial mirrors as Django 3.0 has just had its first alpha released. Keeping an eye on the supported versions, I did the following changes: Added mirror for 3.0 branch : https://bitbucket.org/orzel/django-3.0-production/ Removed mirror for 1.8 branch Removed mirror for 1.9 branch Removed mirror for 1.10 branch Branch 2.0 is officially […]