Django community: RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
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 […] -
Sure, MinIO looks realy nice!
Sure, MinIO looks realy nice! -
Custom Application Metrics with Django, Prometheus, and Kubernetes
Why are custom metrics important?While there are volumes of discourse on the topic, it can't be overstated how important custom application metrics are. Unlike the core service metrics you'll want to collect for your Django application (application and web server stats, key DB and cache operational metrics), custom metrics are data points unique to your domain with bounds and thresholds known only by you. In other words, it's the fun stuff.How might these metrics be useful? Consider:You run an ecomm website and track average order size. Suddenly that order size isn't so average. With solid application metrics and monitoring you can catch the bug before it breaks the bank.You're writing a scraper that pulls the most recent articles from a news website every hour. Suddenly the most recent articles aren't so recent. Solid metrics and monitoring will reveal the breakage earlier.I 👏 Think 👏 You 👏 Get 👏 The 👏 Point 👏Setting up the Django ApplicationBesides the obvious dependencies (looking at you pip install Django), we'll need some additional packages for our pet project. Go ahead and pip install django-prometheus-client. This will give us a Python Prometheus client to play with, as well as some helpful Django hooks including middleware … -
Custom Application Metrics with Django, Prometheus, and Kubernetes
While there are volumes of discourse on the topic, it can’t be overstated how important custom application metrics are. Unlike the core service metrics you’ll want to collect for your Django application (application and web server stats, key DB and cache operational metrics), custom metrics are data points unique to your domain with bounds and thresholds known only by you. In other words, it’s the fun stuff. How might these metrics be useful? Consider: You run an ecomm website and track average order size. Suddenly that order size isn’t so average. With solid application metrics and monitoring you can catch the bug before it breaks the bank.You’re writing a scraper that pulls the most recent articles from a news website every hour. Suddenly the most recent articles aren’t so recent. Solid metrics and monitoring will reveal the breakage earlier.I Think You Get The Point Setting up the Django Application Besides the obvious dependencies (looking at you pip install Django), we’ll need some additional packages for our pet project. Go ahead and pip install django-prometheus-client. This will give us a Python Prometheus client to play with, as well as some helpful Django hooks including middleware and a nifty DB wrapper. Next we’ll run … -
Django Fellow - Mariusz Felisiak
Django IRC channel is #django irc.freednote.net Django Developers List Django Fellows Report - August 2019 django-docker-box SHAMELESS PLUGS William's books on Django Carlton's website Noumenal -
How to deploy Django project to Heroku using Docker
In this Django Heroku guide, I will talk about how to deploy Django project to Heroku using Docker. -
Profiling & Optimizing Bottlenecks In Django
In the previous article, we have learnt where to start with performance optimization in django application and find out which APIs to optimize first. In this article, we will learn how to optimize those selected APIs from the application. Profling APIs With django-silk django-silk provides silk_profile function which can be used to profile selected view or a snippet of code. Let's take a slow view to profile and see the results. from silk.profiling.profiler import silk_profile @silk_profile() def slow_api(request): time.sleep(2) return JsonResponse({'data': 'slow_response'}) We need to add relevant silk settings to django settings so that required profile data files are generated and stored in specified locations. SILKY_PYTHON_PROFILER = True SILKY_PYTHON_PROFILER_BINARY = True SILKY_PYTHON_PROFILER_RESULT_PATH = '/tmp/' Once the above view is loaded, we can see the profile information in silk profiling page. In profile page, silk shows a profile graph and highlights the path where more time is taken. It also shows cprofile stats in the same page. This profile data file can be downloaded and used with other visualization tools like snakeviz. By looking at the above data, we can see most of the time is spent is time.sleep in our view. Profling APIs With django-extensions If you don't want to … -
Profiling & Optimizing Bottlenecks In Django
In the previous article, we have learnt where to start with performance optimization in django application and find out which APIs to optimize first. In this article, we will learn how to optimize those selected APIs from the application. Profling APIs With django-silk django-silk provides silk_profile function which can be used to profile selected view or a snippet of code. Let's take a slow view to profile and see the results. from silk.profiling.profiler import silk_profile @silk_profile() def slow_api(request): time.sleep(2) return JsonResponse({'data': 'slow_response'}) We need to add relevant silk settings to django settings so that required profile data files are generated and stored in specified locations. SILKY_PYTHON_PROFILER = True SILKY_PYTHON_PROFILER_BINARY = True SILKY_PYTHON_PROFILER_RESULT_PATH = '/tmp/' Once the above view is loaded, we can see the profile information in silk profiling page. In profile page, silk shows a profile graph and highlights the path where more time is taken. It also shows cprofile stats in the same page. This profile data file can be downloaded and used with other visualization tools like snakeviz. By looking at the above data, we can see most of the time is spent is time.sleep in our view. Profling APIs With django-extensions If you don't want to … -
Testing WizardView in Django
Sometimes, you just have to resort to a multi-step view to simplify data entry for a user. [Django](https://www.djangoproject.com) had an "included" package for this: `django.contrib.formtools`, until some years ago, when it moved into [it's own repository](https://django-formtools.readthedocs.io/en/latest/index.html). It's still very useful though, although there is a bit of a learning curve. I have some multi-step wizard views, for things where there are a bunch of different (sometimes dependent, but not necessarily) things we need to get from the user, and commit in an all-or-nothing way. Writing tests for these views is quite cumbersome, but I've recently come up with a couple of things that can make this less painful. The reason it's somewhat difficult is that the wizard, and the current step form both have form data, so to ensure there are no clashes between name fields, the forms are all prefixed. However, prefixes on forms mean that the data you need to pass to the form must also be prefixed in the correct way. {% highlight python %} class TestWizard(TestCase): def test_wizard_view(self): # user has been created... self.client.force_login(user) # url is the url for this wizard view. response = self.client.get(url) self.assertEqual(200, response.status_code) response = self.client.post(url, { 'wizard-current_step': 'first', 'first-field_a': 'value', … -
From Django 0.9 to Present - Russell Keith-Magee
PyCon Australia 2015: Money, Money, Money - Writing software, in a rich (wo)man’s world BeeWare Django 2008 Keynote Keynote: Why I Hate Django by Cal Henderson PyCon Australia 2017 - Red User, Blue User, MyUser, auth.User by Russell Keith-Magee django-improved-user PyCon 2019 Keynote Django for Beginners book DjangoX Starter Project PyCon Australia 2019 - Just Add Await: Retrofitting Async Into Django by Andrew Godwin SHAMELESS PLUGS William's books on Django Carlton's website Noumenal -
Revisions
Mistakes happen, and that’s part of a learning process. In a large project like Django, it can be hard to spot a mistake. Thanks to it being open source, anyone can see the code and fix the mistakes they see. In this post, I’ll explain how I found a vulnerability in contrib.postgres.fields.JSONField that allowed SQL injections to be performed. If you’re familiar with Python’s DB-API, you may have used the . -
Build a Python Jupyter Notebook Server with Docker & Heroku
Jupyter notebooks have become ... -
Джанго в роли микрофреймворка
Известно мнение, что Джанго не подходит для написания "наколеночных" проектиков, которым не нужно ничего особенно, кроме вывода HTML'а. Создание скелета проекта на Джанго считается работой, достаточно существенной для того, чтобы не заниматься ею ради пары функций. Мне всегда было "очевидно", что это не так, но недавно по оброненной коллегой фразе я понял, что это не значит, что это очевидно всем. Многие просто не задумываются об этом. Ну, вы знаете, как это бывает… Поэтому я хочу продемонстрировать создание Джанго-проекта минимальными усилиями. Упрощение Нужно отказаться от команды startproject. Она создаёт хороший скелет проекта с аннотированным файлом настроек, но если вы Джанго-проект видите не впервые, этим можно пренебречь. Файл manage.py — тоже не нужен, это просто локальный хелпер для django-admin.py Не нужно думать о проекте, как о контейнере для приложений, а следовательно не нужно оформлять свой код, как подключаемое приложение. Приложения — одна из самых сильных архитектурных черт Джанго, но для мелких задач эта гибкость никогда не понадобится. В итоге мы имеем что-то такое: settings.py: DEBUG = True ROOT_URLCONF = 'urls' TEMPLATE_DIRS = ['.'] urls.py: from django.conf.urls.defaults import * import views urlpatterns = patterns('', (r'^$', views.index), (r'^test/(\d+)/$', views.test), ) views.py: from django.shortcuts import render def index(request): return render(request, 'index.html', {}) def test(request, id): … -
MySQL and Security - Adam Johnson
Adam Johnson personal site django-mysql MariaDB Django Hosting Options Django’s Test Case Classes and a Three Times Speed-Up DjangoCon 2019 Europe - Django and Web Security Headers SHAMELESS PLUGS William's books on Django Carlton's website Noumenal