Django community: Community blog posts RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
django-translated-fields – localized model fields without magic
django-translated-fields – localized model fields without magic There are many ways to save and retrieve multilingual content in a database; countless blog posts, emails and software packages have been written discussing or helping with this problem. Two main approaches exist to tackle the problem: Use a table for the language-independent content, and a table for language-specific content. The latter most often has a foreign key to the former and a language field. There will be a record in the latter table for each record in the former, or less if some dataset isn’t available in all languages. django-hvad, django-parler and also FeinCMS 1’s translations module follow this approach. Use only one table, but use several fields to store the localized data. django-modeltranslation is probably the best known app implementing this approach. (Other ways of course exist. Among the more interesting packages (to me) are django-nece using Postgres’ JSONB fields and django-vinaigrette using gettext.) Why write another package? The features they provide are at costly to implement and hard to maintain. For example, django-modeltranslation supports adding translations to third party apps which themselves do not support any translations, but to do this it has to not only provide properties for attribute … -
File checksums in Python: the hard way - Shane Kerr
(Summary of a talk at the Amsterdam python meetup) Shane is data hoarder. He hates losing data. He has lots of data. He could move it to the cloud, but he doesn't trust that: cloud companies go away after a time. And: disks are cheap now, so he doesn't need to. But: bad things happen to good data. Disks go corrupt. You can use checksums to detect problems. In the ideal work, the os/hardware detects problems and everything just works. But it isn't true for linux RAID (which doesn't do checks). ext4 doesn't do checking at all. ZFS could work, but the license is deliberately linux-GPL-unfriendly. So: he wants proper checksums. You could use sha1sum in a shell script and pipe the checksums to a file. But the simple case doesn't handle metadata. And it isn't parallel. So... he started a python program. Python is "batteries included": it has a great standard library. For instance os.walk() which goes through all the directories and files. With os.path.join() and os.path.normpath() you get usable full filepaths. With hashlib.sha224() he could get a hash per file. He tried it out with various python versions: python3 (cpython3) is the base. python 2: works, but for … -
RESTful APIs and Django - Emad Mokhtar
(Summary of a talk at the Amsterdam python meetup) Ehmad Mokhtar used Django a lot. And he loves web APIs: you can have many intergrations. You build one back-end and it can be used by many other services and front-ends. It is also core for microservices and "service oriented architectures". So: APIs = business! Without APIs, you have no intergrations. So often you won't have as much business. What is a RESTful API? It is an architectural style that uses HTTP: you work on URLs ("resources") and you use HTTP verbs ("GET", "DELETE", "POST"). If you build an API, developers are your API customers. So you need, for instance, great documentation. You need to keep that in mind all the time. If you use django and you want to build an API: use django rest framework (DRF). DRF feels like django itself. It uses the ORM. And it is perhaps even better documented than Django itself. A tip he has is to use two views per model. In django, you can have a separate list view, a delete view, a regular view, etc. In DRF you are best off having two: One for listing items and creating new ones. (/customers/) … -
The origins of Python: the ABC language - Rolf Zwart
(Summary of a talk at the Amsterdam python meetup) How to compare python to ABC? ABC is the unsuccessful language that was the inspiration for python. Guido van Rossum was originally the main implementor of the ABC language. ABC was intended for the casual user. Home users. But also experts in other fields, but that were not programming experts. The focus was on friendliness of the language and on programmer productivity, not program efficiency. It also was used as a teaching/research language. Research on ABC was started before 1980. The first version was released in 1987. In the end of 1989, Guido started developing python, with the 0.9.0 version being released to Usenet in february 1991. His research group needed an easy-to-use scripting language for the distributed OS they were working on, that was why he started building Python. (He chose the name because he was a fan of the English TV series monty python's flying circus). ABC was, like python, developed at the CWI (centrum voor wiskunde en informatica) in the Netherlands. The claim of ABC was that the programs build with it were typically around a quarter of the size of the equivalent Pascal or C programs. Some … -
Beginner's Angular 4|5 Tutorial Series for Django Developers
In the previous tutorial, we've learned how to integrate Angular 4 with Python & Django. This tutorial will be dedicated to how to get started with Angular 4|5. Throughout this beginner's series, you'll learn how you can use Angular 4|5 to build client side web applications for mobile and desktop with a Django backend. This tutorial is a part of a tutorial series that contains the following tutorials: Building Modern Web Apps with Python, Django Rest Framework and Angular 2+ Django REST framework (DRF) with Angular 2+ tutorial Getting Started with The Angular CLI (this one) The Angular 4|5 Router: Component Routing: the router module is one of the most important blocks of the Angular framework because it allows you to build apps with multiple pages and add routing between them. So in this part we will cover the Angular router in depth and by examples. Angular Components Explained: : in this tutorial we will learn the basics of components in Angular and the new additions in Angular 5. Getting Started with The Angular HTTP Client: in this tutorial we'll learn how to use the new HttpClient API, available only for Angular 4+ to make HTTP requests in Angular 4+ … -
Authenticating via JWT using Django, Axios, and Vue
Getting Django Rest Framework, JWT, Axios, and Vue.js to play nice isn't easy. Here's my quick-and-dirty cheatsheet that I wrote while glueing the pieces together. Note: My architecture doesn't use django-webpack-loader. Instead, I'm running Django and Vue.js as two separate projects. I do this because I much prefer generating new projects with vue create over configuring webpack. The Back-End First, install some Django parts using the installer of your choice: pip install Django pip install djangorestframework pip install django-cors-headers pip install djangorestframework-jwt Then, configure Django in settings.py: INSTALLED_APPS = ( ... 'rest_framework', 'rest_framework.authtoken', 'rest_framework.authtoken', 'corsheaders', ) MIDDLEWARE_CLASSES = ( ... 'corsheaders.middleware.CorsMiddleware', ) CORS_ORIGIN_ALLOW_ALL = False CORS_ALLOW_CREDENTIALS = True CORS_ORIGIN_WHITELIST = ( # TODO - set this properly for production 'http://127.0.0.1:8080', 'http://127.0.0.1:8000', ) REST_FRAMEWORK = { 'DEFAULT_PERMISSION_CLASSES': ( # By default we set everything to admin, # then open endpoints on a case-by-case basis 'rest_framework.permissions.IsAdminUser', ), 'TEST_REQUEST_RENDERER_CLASSES': ( 'rest_framework.renderers.MultiPartRenderer', 'rest_framework.renderers.JSONRenderer', 'rest_framework.renderers.TemplateHTMLRenderer' ), 'DEFAULT_AUTHENTICATION_CLASSES': ( 'rest_framework_jwt.authentication.JSONWebTokenAuthentication', 'rest_framework.authentication.SessionAuthentication', ), 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination', 'PAGE_SIZE': 20, } from datetime import timedelta JWT_AUTH = { 'JWT_ALLOW_REFRESH': True, 'JWT_EXPIRATION_DELTA': timedelta(hours=1), 'JWT_REFRESH_EXPIRATION_DELTA': timedelta(days=7), } Once that's done, it's time to do modify the urls.py: from django.conf.urls import include, url ... urlpatterns = [ ... # JWT auth url(r'^api/v1/auth/obtain_token/', obtain_jwt_token), … -
The final INTERNAL_IPS fix for development hosts
The final INTERNAL_IPS fix for development hosts Django’s INTERNAL_IPS setting is an ongoing source of frustration and confusion (not only, but also) for users of django-debug-toolbar, especially when using non-local addresses. This is very useful for testing a website using mobile devices if you do not have a very fast internet connection where it does not matter whether you connect to a host through the local network or via the internet, for example using localtunnel. For some time we had a utility function which automatically added all detected network interface IPs to INTERNAL_IPS. However, this does not work when using virtualization software such as Docker or Vagrant with port forwarding, because the VM’s (or container’s) IP isn’t what you want – you want the host IP. Once I took a step back I saw a different, but much simpler solution. INTERNAL_IPS can be replaced with an object which simply answers True to all __contains__-type questions: if DEBUG: # `debug` is only True in templates if the vistor IP is in INTERNAL_IPS. INTERNAL_IPS = type(str('c'), (), {'__contains__': lambda *a: True})() -
PyCon IT 2018 “Nove”
PyCon Italia is the national conference where professionals, researchers and enthusiasts of the most beautiful programming language gather together. -
How to Apply Image Filters in OpenCV with Python
Add your own image filters on ... -
Goodbye manage.py
Every Django project starts with a manage.py file in its root. It's a convenience script that allows you to run administrative tasks like Django's included django-admin. In our last post, we discussed the merits of including a setup.py file in your project to make it installable as a true Python package. We'll build on that and show you how to properly setup a console script in the package. Hello Entry Points The setup function provided by setuptools accepts an entry_points keyword argument that is used to dynamically create scripts from a provided Python function on install. If we crack open the existing manage.py and strip it down to its essentials, we can see it isn't really doing much: import os import sys os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings") from django.core.management import execute_from_command_line execute_from_command_line(sys.argv) It sets a default environment variable and then calls a function passing in the provided command line arguments. This can easily be turned into an entry point by simply making it a function inside our project. I typically add it to myproject/__init__.py like so: import os import sys def manage(): os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings") from django.core.management import execute_from_command_line execute_from_command_line(sys.argv) Now we adjust our setup.py to add the entry point: setup( ... entry_points={'console_scripts': [ … -
Goodbye manage.py
Every Django project starts with a manage.py file in its root. It's a convenience script that allows you to run administrative tasks like Django's included django-admin. In our last post, we discussed the merits of including a setup.py file in your project to make it installable as a true Python package. We'll build on that and show you how to properly setup a console script in the package. Hello Entry Points The setup function provided by setuptools accepts an entry_points keyword argument that is used to dynamically create scripts from a provided Python function on install. If we crack open the existing manage.py and strip it down to its essentials, we can see it isn't really doing much: python import os import sys os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings") from django.core.management import execute_from_command_line execute_from_command_line(sys.argv) It sets a default environment variable and then calls a function passing in the provided command line arguments. This can easily be turned into an entry point by simply making it a function inside our project. I typically add it to myproject/__init__.py like so: python import os import sys def manage(): os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings") from django.core.management import execute_from_command_line execute_from_command_line(sys.argv) Now we adjust our setup.py to add the entry point: python setup( … -
Django - Referencing the User Model
There are 3 different ways to refer to the User model in Django. This post explains the differences and which approach to use. -
Offline messages for Django
Offline messages for Django django.contrib.messages is awesome. Its API surface is small, it’s fast and it does what it does really well. However, it sometimes happens that you would want to send messages to users outside the request-response cycle, for example from a background process or even from a post_save signal handler. Adding a full-blown notifications-and-messages-system is of course possible. Sending a one-off message to the user would be sufficient though, but there is no way to do this with Django’s messages framework. There are a few packages around solving this particular problem. Almost all of them solve this by saving undelivered messages in the database. My solution, django-user-messages follows the same approach, but contrary to many others it does not reimplement the message storage nor replace any other functionality of django.contrib.messages. It only adds a few additional utilities for adding messages (e.g. user_messages.api.info(...) instead of messages.info(...)) and a context processor which concatenates Django’s messages with those provided by django-user-messages. Instead of passing the request to messages.info(...) you would pass a user instance, or even only a user ID to api.info(...). Easy enough, and works well. Despite the 0.5 version number the package has been stable and essentially unchanged since … -
3rd Party Haar Cascades in OpenCV
Building on [face recognition ... -
How to add a watermark with overlays using OpenCV & Python
Creating a watermark with Open... -
Demystifying encodings — part 3
Do you ever have trouble with encodings? I used to in the past, but now I rarely do, because I do a simple thing. While knowing the things I explained in part 1 and part 2 of this series is necessary, otherwise I wouldn’t be able to fix problems, such problems rarely arise, because the first thing I do when I setup a GNU/Linux machine is set the system locale to UTF-8. The “locale” is the regional settings, among which the character encoding used. The procedure to set it up is this: Open the file /etc/locale.gen in an editor and make sure the line that begins with “en_US.UTF-8” is uncommented. Enter the command locale-gen; this will (re)generate the locales. Open the file /etc/default/locale in an editor, and make sure it contains the line LANG=en_US.UTF-8. Changes in this file require logout and login to take effect. Let me now explain what all this is about. The locale consists of a language, a country, and a character encoding; “en_US.UTF-8” means English, United States, UTF-8. This tells programs to show messages in American English; to format items such as dates in the way it’s done in the United States; and to use encoding … -
OpenCV & Python: Face Recognition and Identification
I'm really starting to enjoy w... -
Save an Auto Generated PDF File to Django model
To save a auto-generated PDF f... -
For hire
Since a slightly-wider circle of people know this now, it’s time to just go public with the news: my last day at now-former employer was toward the end of January. At the time I told a few friends, but wasn’t in a huge rush to find something else immediately; I’d been getting ready to depart for a little while, and there were some things (a bit of travel, catching up on some open-source projects, and ... Read full entry -
Efficient many-to-many field lookup in Django REST Framework
The basic setup Suppose you have these models: from django.db import models class Category(models.Model): name = models.CharField(max_length=100) class Blogpost(models.Model): title = models.CharField(max_length=100) categories = models.ManyToManyField(Category) Suppose you hook these up Django REST Framework and list all Blogpost items. Something like this: # urls.py from rest_framework import routers from . import views router = routers.DefaultRouter() router.register(r'blogposts', views.BlogpostViewSet) # views.py from rest_framework import viewsets class BlogpostViewSet(viewsets.ModelViewSet): queryset = Blogpost.objects.all().order_by('date') serializer_class = serializers.BlogpostSerializer What's the problem? Then, if you execute this list (e.g. curl http://localhost:8000/api/blogposts/) what will happen, on the database, is something like this: SELECT "app_blogpost"."id", "app_blogpost"."title" FROM "app_blogpost"; SELECT "app_category"."id", "app_category"."name" FROM "app_category" INNER JOIN "app_blogpost_categories" ON ("app_category"."id" = "app_blogpost_categories"."category_id") WHERE "app_blogpost_categories"."blogpost_id" = 1025; SELECT "app_category"."id", "app_category"."name" FROM "app_category" INNER JOIN "app_blogpost_categories" ON ("app_category"."id" = "app_blogpost_categories"."category_id") WHERE "app_blogpost_categories"."blogpost_id" = 193; SELECT "app_category"."id", "app_category"."name" FROM "app_category" INNER JOIN "app_blogpost_categories" ON ("app_category"."id" = "app_blogpost_categories"."category_id") WHERE "app_blogpost_categories"."blogpost_id" = 757; SELECT "app_category"."id", "app_category"."name" FROM "app_category" INNER JOIN "app_blogpost_categories" ON ("app_category"."id" = "app_blogpost_categories"."category_id") WHERE "app_blogpost_categories"."blogpost_id" = 853; SELECT "app_category"."id", "app_category"."name" FROM "app_category" INNER JOIN "app_blogpost_categories" ON ("app_category"."id" = "app_blogpost_categories"."category_id") WHERE "app_blogpost_categories"."blogpost_id" = 1116; SELECT "app_category"."id", "app_category"."name" FROM "app_category" INNER JOIN "app_blogpost_categories" ON ("app_category"."id" = "app_blogpost_categories"."category_id") WHERE "app_blogpost_categories"."blogpost_id" = 1126; SELECT "app_category"."id", "app_category"."name" FROM "app_category" INNER JOIN … -
Django, Sitemaps and alternates
Django, Sitemaps and alternates Django’s own sitemaps module is great for quickly generating sitemaps, but it unfortunately does not support more than the bare minimum of attributes. Also, it uses the template engine to create XML and this makes me sad. We had to add a sitemap.xml file to a customers’ website. The site supports several languages and runs on several domains, so we had to carefully specify alternate language pages and canonical URLs so that the website would be punished for duplicate content. Therefore, a year ago I set out to build a sitemaps app for Django which supports Django’s sitemaps, but also allows adding entries with additional attributes. The result of this work was django-sitemaps. from django_sitemaps import Sitemap def sitemap(request): sitemap = Sitemap(build_absolute_uri=request.build_absolute_uri) sitemap.add_django_sitemap(SomeSitemap, request=request) sitemap.add( url, changefreq='weekly', priority=0.5, lastmod=datetime.now(), alternates={ 'en': '...', 'en-ch': '...', 'en-gb': '...', 'de': '...', ... }, ) return sitemap.response(pretty_print=True) Today, I also added support for generating the most simple robots.txt files possible: All user agents, and only Sitemap: <absolute url> entries. The recommended usage is now (still using url() instead of path() because I’m old and rusty): from django_sitemaps import robots_txt from app.views import sitemap urlpatterns = [ url(r'^sitemap\.xml$', sitemap), url(r'^robots\.txt$', robots_txt(timeout=86400)), … -
Our approach to configuring Django, Webpack and ManifestStaticFilesStorage
Our approach to configuring Django, Webpack and ManifestStaticFilesStorage I spent some time finding out how Django and webpack should be configured so that they work well together both in development and in production. We are now using this setup on dozens of websites, so now is a good time to write down the approach. Requirements for development were: Hot module reloading (especially of CSS, but also of JavaScript code). Ability to use SCSS, ES6 and ES7. PostCSS support (although I currently only use the autoprefixer, nothing else). Requirements for production: Filenames depending on the content of assets, so that we can add far future expiry headers without worrying about cache busting, and not only for webpack-generated assets but also for everything else, so ManifestStaticFilesStorage is a must. Separate delivery of CSS and JavaScript code – we want to add CSS to the <head>, and JavaScript at the end of the <body>. Working webpack code splitting is a plus if possible. There are a few problems that have to be solved: Django has to know about asset URLs generated by webpack. Some assets are used both in HTML and in CSS/JavaScript code, e.g. a logo image. The obvious way to be … -
First Rotterdam (NL) Python meetup: my summaries
Welcome to the first Python Rotterdam meetup - Thijs Damsma He searched for python meetups in Rotterdam, but didn't find any. So he clicked on the "do you want to start one?" button. Today's meetup is at Van Oord (a big family owned marine contractor), but anywhere else in Rotterdam is fine. And you can help out. Everything is fine, from talks to workshops, as long as it is python-related. The goal is to have a few meetups per year. Jupyter lab - Joost Dobken Joost is a data engineer. He uses python daily, mostly jupyter notebooks. Jupyter notebooks are ideal for getting to know python, btw. Jupyter lab is the next iteration of notebooks. It is in beta, but usable. Normally, you still work in notebooks, but you can also start a regular python prompt or a terminal. It starts to look like an IDE: file browser, generic editor, etc. And very nice for data analysis, for instance with a fast CSV browser. He demoed the markdown integration. A source file with an (live updating) output window side by side. And a terminal window at the bottom. Very fancy: there is an extension that works with google docs. So … -
How to Record Video in OpenCV & Python
OpenCV makes it simple to reco... -
Open source activity (April 2018 edition)
It’s time for a post about recently released Django and Python packages by yours truly. Open source activity (April 2018 edition) django-imagefield A more opinionated version of django-versatileimagefield, which keeps the amount of code in production at a minumum has a strong preference towards generating images in advance, not on demand (which means it stays fast whatever the storage backend may be) fails early when invalid images are uploaded instead of crashing the website later speckenv speckenv helps keep configuration and secrets in the environment, not in the code. It knows how to parse .env files, and how to read structured values from the environment (not only strings, but also bools, lists, dictionaries – in short, Python literals) django-curtains It is useful to give clients protected access to a website in development. django-curtains allows keeping the work in progress secret by only allowing authenticated access (using either Django’s authentication system, or HTTP Basic authorization) Of course, activity on older projects hasn’t ceased either. New releases of xlsxdocument, django-user-messages, django-http-fallback-storage, html-sanitizer, feincms3, django-cabinet, django-authlib, etc. are available on PyPI.