Django community: RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
From MySQL to PostgreSQL
In this article I will guide you through the steps I had to take to migrate Django projects from MySQL to PostgreSQL. MySQL database has proven to be a good start for small and medium scale projects. It is known and used widely and has good documentation. Also there are great clients for easy management, like phpMyAdmin (web), HeidiSQL (windows), or Sequel Pro (macOS). However, in my professional life there were unfortunate moments, when databases from different projects crashed because of large queries or file system errors. Also I had database integrity errors which appeared in the MySQL databases throughout years because of different bugs at the application level. When one thinks about scaling a project, they have to choose something more suitable. It should be something that is fast, reliable, and well supports ANSI standards of relational databases. Something that most top Django developers use. And the database of choice for most professionals happens to be PostgreSQL. PostgreSQL enables using several vendor-specific features that were not possible with MySQL, e.g. multidimensional arrays, JSON fields, key-value pair fields, special case-insensitive text fields, range fields, special indexes, full-text search, etc. For a newcomer, the best client that I know - pgAdmin … -
Really simple Django view function timer decorator
I use this sometimes to get insight into how long some view functions take. Perhaps you find it useful too: def view_function_timer(prefix='', writeto=print): def decorator(func): @functools.wraps(func) def inner(*args, **kwargs): try: t0 = time.time() return func(*args, **kwargs) finally: t1 = time.time() writeto( 'View Function', '({})'.format(prefix) if prefix else '', func.__name__, args[1:], 'Took', '{:.2f}ms'.format(1000 * (t1 - t0)), args[0].build_absolute_uri(), ) return inner return decorator And to use it: from wherever import view_function_timer @view_function_timer() def homepage(request, thing): ... return render(request, template, context) And then it prints something like this: View Function homepage ('valueofthing',) Took 23.22ms http://localhost:8000/home/valueofthing It's useful when you don't want a full-blown solution to measure all view functions with a middleware or something. It can be useful also to see how a cache decorator might work: from django.views.decorators.cache import cache_page from wherever import view_function_timer @view_function_timer('possibly cached') @cache_page(60 * 60 * 2) # two hours cache @view_function_timer('not cached') def homepage(request, thing): ... return render(request, template, context) That way you can trace that, with tail -f or something, to see how/if the cacheing decorator works. There are many better solutions that are more robust but might be a bigger investment. For example, I would recommend markus which, if you don't have a statsd … -
Django version 2.0 // A Few Key Features
I'll be updating this post fro... -
Accessing Model's/Object's Verbose Name in Django Template
Let's say you have a model: from django.db import models class Snippet(models.Model): .... class Meta: verbose_name = 'Snippet' verbose_name_plural = 'Snippets' And you want to print model's verbose name in a Django template. Try to do it as {{ object._meta.verbose_name }} and you will fail - Django template won ... Read now -
My rules for releasing open source software
My rules for releasing open source software I maintain and help maintain quite a few Open Source Python packages. Possibly well-known packages include django-debug-toolbar, django-ckeditor, django-mptt, and FeinCMS resp. feincms3. Open source development used to stress me greatly. I was always worrying whether the code is polished enough, whether I didn’t introduce new bugs and whether the documentation is sufficient. These days I still think about these things, but I do not worry as much as I used to. The reason for this are the following principles: A fully passing test suite on Travis CI is a sufficient quality guarantee for a release. Do not worry about release notes, but always keep the CHANGELOG up to date. Put out patch releases even for the smallest bugfixes and feature additions (as long as they are backwards compatible). Nobody wants to wait for the next big release, it always takes longer than intended. Good enough is perfection. -
Django 2.0 Window expressions tutorial
Django 2.0 was released recently and among the most exciting things for me is support for Window expressions, which allows adding an OVER clause to querysets. We will use Window expressions to analyze the commits data to the Django repo. So what is an over clause? An over clause is of this format SELECT depname, empno, salary, avg(salary) OVER (PARTITION BY depname) FROM empsalary; Compare this to a similar GROUP BY statement SELECT depname, avg(salary) FROM empsalary GROUP BY depname; The difference is a GROUP BY has as many rows as grouping elements, here number of depname. An over clause adds the the aggregated result to each row of the select. Postgres documentation says, "A window function performs a calculation across a set of table rows that are somehow related to the current row. This is comparable to the type of calculation that can be done with an aggregate function. But unlike regular aggregate functions, use of a window function does not cause rows to become grouped into a single output row — the rows retain their separate identities. Behind the scenes, the window function is able to access more than just the current row of the query result." This … -
Django 2.0 Windows expressions tutorial
Django 2.0 was released recently and among the most exciting things for me is support for Window expressions, which allows adding an OVER clause to querysets. We will use Window expressions to analyze the commits data to the Django repo. So what is an over clause? An over clause is of this format SELECT depname, empno, salary, avg(salary) OVER (PARTITION BY depname) FROM empsalary; Compare this to a similar GROUP BY statement SELECT depname, avg(salary) FROM empsalary GROUP BY depname; The difference is a GROUP BY has as many rows as grouping elements, here number of depname. An over clause adds the the aggregated result to each row of the select. Postgres documentation says, "A window function performs a calculation across a set of table rows that are somehow related to the current row. This is comparable to the type of calculation that can be done with an aggregate function. But unlike regular aggregate functions, use of a window function does not cause rows to become grouped into a single output row — the rows retain their separate identities. Behind the scenes, the window function is able to access more than just the current row of the query result." This … -
Container Runtimes Part 1: An Introduction to Container Runtimes
One of the terms you hear a lot when dealing with containers is "container runtime". "Container runtime" can have different meanings to different people so it's no wonder that it's such a confusing and vaguely understood term, even within the container community. This post is the first in a series that will be in four parts: 1. Part 1: Intro to Container Runtimes: why are they so confusing? 2. Part 2: Deep Dive into Low-Level Runtimes 3. Part 3: Deep Dive into High-Level Runtimes 4. Part 4: Kubernetes Runtimes and the CRI This post will explain what container runtimes are and w[...] -
Container Runtimes Part 1: An Introduction to Container Runtimes
One of the terms you hear a lot when dealing with containers is "container runtime". "Container runtime" can have different meanings to different people so it's no wonder that it's such a confusing and vaguely understood term, even within the container community. This post is the first in a series that will be in four parts: 1. Part 1: Intro to Container Runtimes: why are they so confusing? 2. Part 2: Deep Dive into Low-Level Runtimes 3. Part 3: Deep Dive into High-Level Runtimes 4. Part 4: Kubernetes Runtimes and the CRI This post will explain what container runtimes are and w[...] -
Caktus is Excited about Django 2.0
Did you know Django 2.0 is out? The development team at Caktus knows and we’re excited! You should be excited too if you work with or depend on Django. Here’s what our Cakti have been saying about the recently-released 2.0 beta. -
Setup & Install Ionic
Ionic is a framework for build... -
Configure Django to log exceptions in production
Django default logging behaviour for unhandled exceptions is: With DEBUG=True (Development) Log the exception on console/stream. Show the exception on page, i.e in http response. With DEBUG=False (Production) Do not log the exception on console/stream. Do not show the exception in response. Send an email to admin if email settings are configured correctly. Usually not logging the exception on console isn't a problem since an exception email is sent to you which can help you know the source of exception. But this assumes that email settings are configured correctly else you will not receive the exception email. You might not have email settings configured correctly and don't want to get into that right away. You might instead want to log the exception on console even with DEBUG=False. This post would help you in such scenario. Default logging configuration Django's default logging setting is: DEFAULT_LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'filters': { 'require_debug_false': { '()': 'django.utils.log.RequireDebugFalse', }, 'require_debug_true': { '()': 'django.utils.log.RequireDebugTrue', }, }, 'formatters': { 'django.server': { '()': 'django.utils.log.ServerFormatter', 'format': '[%(server_time)s] %(message)s', } }, 'handlers': { 'console': { 'level': 'INFO', 'filters': ['require_debug_true'], 'class': 'logging.StreamHandler', }, 'django.server': { 'level': 'INFO', 'class': 'logging.StreamHandler', 'formatter': 'django.server', }, 'mail_admins': { 'level': 'ERROR', 'filters': ['require_debug_false'], … -
Building a CRUD Application with Django Rest Framework and Vue.js
In this tutorial, you will learn how to use Django and Vue.js to build a modern CRUD (Create, read, update and delete operations are essential for the majority of web applications) web application. You'll also learn how to integrate Django Rest Framework with Vue.js and how to make HTTP calls using vue-resource (you can also use Axios or the browser's fetch API). In nutshell, Django is a back-end framework for building web applications with Python. Vue.js is a user interface library for creating JavaScript applications in the front-end. Django Rest Framework is a Django module to create Rest-based APIs that can be then consumed from browsers or mobile devices. You can use any database management system such as MySQL or PostgreSQL etc. Since the Django ORM can abstracts away all the differences between database systems and let work with any database without writing new code. The integration part simply consists of using an HTTP client like Axios, vue-resource or even better the browser's fetch API to call APIs exposed by DRF from the Vue.js application. Both server and client applications are decoupled so you can swap any part with any other library in the future. You can also create mobile … -
Building a CRUD Application with Django Rest Framework and Vue.js
In this tutorial, you will learn how to use Django and Vue.js to build a modern CRUD (Create, read, update and delete operations are essential for the majority of web applications) web application. You'll also learn how to integrate Django Rest Framework with Vue.js and how to make HTTP calls using vue-resource (you can also use Axios or the browser's fetch API). In nutshell, Django is a back-end framework for building web applications with Python. Vue.js is a user interface library for creating JavaScript applications in the front-end. Django Rest Framework is a Django module to create Rest-based APIs that can be then consumed from browsers or mobile devices. You can use any database management system such as MySQL or PostgreSQL etc. Since the Django ORM can abstracts away all the differences between database systems and let work with any database without writing new code. The integration part simply consists of using an HTTP client like Axios, vue-resource or even better the browser's fetch API to call APIs exposed by DRF from the Vue.js application. Both server and client applications are decoupled so you can swap any part with any other library in the future. You can also create mobile … -
Django Logging, The Right Way
Good logging is critical to debugging and troubleshooting problems. Not only is it helpful in local development, but in production it's indispensable. When reviewing logs for an issue, it's rare to hear somebody say, "We have too much logging in our app." but common to hear the converse. So, with that in mind, let's get started. A Crash Course in Python Loggers At the top of every file, you should have something like this: import logging logger = logging.getLogger(__name__) __name__ will evaluate to the dotted Python path of the module, e.g. myproject/myapp/views.py will use myproject.myapp.views. Now we can use that logger throughout the file like so: # A simple string logged at the "warning" level logger.warning("Your log message is here") # A string with a variable at the "info" level logger.info("The value of var is %s", var) # Logging the traceback for a caught exception try: function_that_might_raise_index_error() except IndexError: # equivalent to logger.error(msg, exc_info=True) logger.exception("Something bad happened") Note: Python's loggers will handle inserting variables into your log message if you pass them as arguments in the logging function. If the log does not need to be output, the variable substitution won't ever occur, helping avoid a small performance hit for … -
Django Logging, The Right Way
Good logging is critical to debugging and troubleshooting problems. Not only is it helpful in local development, but in production it's indispensable. When reviewing logs for an issue, it's rare to hear somebody say, "We have too much logging in our app." but common to hear the converse. So, with that in mind, let's get started. A Crash Course in Python Loggers At the top of every file, you should have something like this: python import logging logger = logging.getLogger(__name__) __name__ will evaluate to the dotted Python path of the module, e.g. myproject/myapp/views.py will use myproject.myapp.views. Now we can use that logger throughout the file like so: python # A simple string logged at the "warning" level logger.warning("Your log message is here") # A string with a variable at the "info" level logger.info("The value of var is %s", var) # Logging the traceback for a caught exception try: function_that_might_raise_index_error() except IndexError: # equivalent to logger.error(msg, exc_info=True) logger.exception("Something bad happened") Note: Python's loggers will handle inserting variables into your log message if you pass them as arguments in the logging function. If the log does not need to be output, the variable substitution won't ever occur, helping avoid a small performance … -
Install OpenCV 3 for Python on Mac
<div class='alert alert-warnin... -
Django Tips & Tricks #9 - Auto Register Models In Admin
Inbuilt admin interface is one the most powerful & popular feature of Django. Once we create the models, we need to register them with admin, so that it can read metadata and populate interface for it. If the django project has too many models or if it has a legacy database, then adding all those models to admin becomes a tedious task. To automate this process, we can programatically fetch all the models in the project and register them with admin. from django.apps import apps models = apps.get_models() for model in models: admin.site.register(model) This works well if we are just auto registering all the models. However if we try some customisations and try to register them in admin.py files in our apps, there will be conflicts as Django doesn't allow registering the same model twice. So, we need to make sure this piece of code runs after all admin.py files are loaded and it should ignore models which are already registered. We can safely hook this code in appconfig. from django.apps import apps, AppConfig from django.contrib import admin class CustomApp(AppConfig): name = 'foo' def ready(self): models = apps.get_models() for model in models: try: admin.site.register(model) except admin.sites.AlreadyRegistered: pass Now all models … -
Django Template language Intro
Django follows the MVC (Model = database-logic, View=business-logic Controller=presentation logic ). We can write the required logics based on programming[python] syntax in models and views but, when we want to write simple logics we should follow the django's template syntax, because programming[python] syntax is not allowed. We have the Template syntax as follows: Syntax Explanation Example {{ variable name }} To display the variable value. Just like " print " as in python input: Welcome "{{ request.user.username }}" output: Welcome "Abhi Ram" {% if condition %} --- logic ---- {% endif %} It is a conditional expression same as " if " condition in python input: {% if request.user.can_edit %} <button> Edit </button> {% endif %} output: if user has attribute "can_edit" as True then it will give result " <button> Edit </button> " nothing otherwise. {% if condition1 %} --logic-- {% elif condition2 %} -- logic-- {% else %} -- logic -- {% endif %} same as python's " if...elif....else " just like above {% for iterable object %} --logic-- {% endfor %} takes an iterable object returns an iterator object in every iteration. To … -
Large scale search for social sciences - Wouter van Atteveldt
(Summary of a talk at a Dutch Python meetup). Full title: large scale search and text analysis with Python, Elastic, Celery and a bit of R. Wouter teaches political sciences at the university and uses a lot of text analysis. He showed an example of text analysis of the Gaza-Isreal conflict, comparing USA media talking about Isreal and Chinese media talking about Israel. You saw big differences! The USA media talks more about rocket attacks by Hamas on Israel. Chinese media talks more about the results on the ground: invasion, deaths, etc. Text analysis is very important for social sciences. There's a flood of digital information, online and archived. Major newspapers in the Netherlands have been digitalized by the royal library, for instance. Lots of text to work with. You see the same with facebook and so: they can extract lots of info from the texts people type in! Facebook once did an experiment on positive/negative tweaks to timelines. Totally bullshit from a scientific viewpoint. So we cannot leave social research to the likes of facebook. So.... there is a need for a good tool, especially for education. They build it in python. Why python? Open source, platform independent, relatively … -
Building robust commandline tools with click and flask - Wojtek Burakiewicz
(Summary of a talk at a Dutch Python meetup). Wojtek likes simple tools. His presentation is about a recent tool they build: an internal tool that deploys python applications to servers with virtualenvs. It also configures nginx. They based the architecture around microservices. So you basically write a simple script that talks to a couple of APIs that in turn talk to other APIs. They started out with stash, artifactory, ansible, jenkins, supervisord and JIRA. All except JIRA have a nice API. Their deploy tool talked to those APIs. One problem was authentication. One tool needs user/pass, the other an auth token, the next an ssh key... Another problem was network segmentation. You don't want every laptop to talk to your production environment... The solution was to use one intermediary API. So the command line tool talks to the intermediary API which in turns talks to the APIs mentioned above. Another advantage of an intermediary API is that you can unify the concepts. You can just talk about "host" and "application", even though in Jenkins/ansible/etc it might be a "job" or a "machine. You can also exchange components! You can switch from Stash to Bitbucket without the user of the … -
Extract GPS & Exif Data from Images using Python
Smartphones & DSLRS attach all... -
Securing Django Application with multi factor authentication using Django-MFA
What is MFA? Multifactor authentication (MFA) is a security system that requires more than one method of authentication to verify the user’s identity for a login or other transaction. Why go for MFA? One of the major problems with traditional user ID and password login is the need to maintain a password database. Whether encrypted or not, if the database is captured it provides the hacker with a source to verify his guesses at speeds limited only by his hardware resources. Given enough time, a captured password database will fall. To avoid this break we do prefer multifactor authentication. Multifactor Authentication Technologies: There are multiple ways we could get the MFA like using hardware devices that the user carries to authorize access to a network service. Software-based security token applications that generate a single-use login PIN. Soft tokens are often used for multifactor mobile authentication, in which the device itself – such as a smartphone – provides the possession factor or SMS messages and phone calls sent to a user as an out-of-band method, smartphone OTP apps. In the current blog post, we see how to implement MFA in Django. How can we implement MFA in Django: We do have … -
How to monitor django application live events with sentry
In the previous tutorial you saw how to setup sentry, let us now learn how to track exceptions and events in SENTRY. We will setup client and server end tracking for Django Project. Note: My domain is set as "http://sentry.domain.com". Now after first login the screen should be similar to this. As per Sentry's Internal Architecture, Team will have access to projects. Create a team and assign a project to team, every member in team will now have access to it. As per your convineance. any combination of members can grouped as teams (which can be managed in Team Settings option in Dashboard page). Creating Team and Project Click on Create Team button in Dashboard. decide a Team Name and save changes create a Project. In Project DashBoard> Settings > Client Keys, you can find DSN (reffered in this tutorial as dsn) and DSN (Public) - (Reffered in this tutorial as public-dsn) Modifications for Django Application The recieving end is sentry but error tracker and logger is RAVEN, Install raven in Django Project environment pip install raven open settings.py include the below lines in settings.py RAVEN_CONFIG = { 'dsn': '<your-dsn-here>', } LOGGING = { 'version': 1, 'disable_existing_loggers': True, 'root': { 'level': 'WARNING', 'handlers': ['sentry'], }, 'formatters': { 'verbose': … -
Python Hashes and Equality
Most Python programmers don’t spend a lot of time thinking about how equality and hashing works. It usually just works. However there’s quite a bit of gotchas and edge cases that can lead to subtle and frustrating bugs once one starts to customize their behavior – especially if the rules on how they interact aren’t understood.