Django community: RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
Building Modern Web Apps with Python, Django Rest Framework and Angular 2+
In this tutorial we are going to learn how to get started building modern web applications with Python, Django and Django Rest Framework as the back-end stack and the new Google Framework, Angular 2+, to build JavaScript client side applications, as the front-end technology. We'll see how to integrate both frameworks in the development and the production environments and how to use the Angular HTTP module to make API calls or Ajax requests to our REST API back-end. This tutorial is using the old Angular HTTP client for making HTTP calls. This module is deprecated in Angular 5 and will be replaced by the new Angular HttpClient module available in Angular 4.3+ which comes with many new features such as the HTTP interceptors. The two modules have the same API interface so you should be able to easily migrate to the new one. In future tutorials we will be using HttpClient so stay tuned! The Angular CLI is a command line utility which allows you to quickly generate and build Angular 2+ apps without the hassle of WebPack configuration. The CLI takes care of the configuration and let you focus on build your next Angular app. It's a really great … -
Building APIs with Django and GraphQL
This tutorial will introduce you to GraphQL with Python, Django and Graphene. We'll see how to create a simple Django project to demonstrate how to build an API server based on GraphQL (instead of REST) then we'll see how to use graphiql_django, an interface for testing GraphQL queries and mutations before building your front-end application, to send GraphQL Queries (for getting data) and Mutations (for posting and updating data). In this part we'll be dealing with building the backend. In the next tutorials we will see how to use frameworks and libraries such as Angular and React to build a front-end application that consumes and updates our GraphQL server and advanced use cases such as user authentication, permissions and Relay Make sure to follow me on twitter (@techiediaries) to be notified once the next tutorial parts are ready. GraphQL is a modern API standard for building Web APIs, invented and used internally by Facebook for its native mobile applications then later open sourced. GraphQL provides a better, powerful and flexible alternative to REST. Before we dive into GraphQL concepts, let's understand what's REST: REST stands for Representational State Transfer and it's an architectural pattern for designing client/server distributed systems. Unlike … -
Building Django HTTP APIs with GraphQL and Graphene
This tutorial will introduce you to GraphQL with Python, Django and Graphene. We'll see how to create a simple Django project to demonstrate how to build an API server based on GraphQL (instead of REST) then we'll see how to use graphiql_django, an interface for testing GraphQL queries and mutations before building your front-end application, to send GraphQL Queries (for getting data) and Mutations (for posting and updating data). In this part we'll be dealing with building the backend. In the next tutorials we will see how to use frameworks and libraries such as Angular and React to build a front-end application that consumes and updates our GraphQL server and advanced use cases such as user authentication, permissions and Relay Make sure to follow me on twitter (@techiediaries) to be notified once the next tutorial parts are ready. GraphQL is a modern API standard for building Web APIs, invented and used internally by Facebook for its native mobile applications then later open sourced. GraphQL provides a better, powerful and flexible alternative to REST. Before we dive into GraphQL concepts, let's understand what's REST: REST stands for Representational State Transfer and it's an architectural pattern for designing client/server distributed systems. Unlike … -
Building APIs with Django, GraphQL and Graphene
This tutorial will introduce you to GraphQL with Python, Django and Graphene. We'll see how to create a simple Django project to demonstrate how to build an API server based on GraphQL (instead of REST) then we'll see how to use graphiql_django, an interface for testing GraphQL queries and mutations before building your front-end application, to send GraphQL Queries (for getting data) and Mutations (for posting and updating data). In this part we'll be dealing with building the backend. In the next tutorials we will see how to use frameworks and libraries such as Angular and React to build a front-end application that consumes and updates our GraphQL server and advanced use cases such as user authentication, permissions and Relay Make sure to follow me on twitter (@techiediaries) to be notified once the next tutorial parts are ready. GraphQL is a modern API standard for building Web APIs, invented and used internally by Facebook for its native mobile applications then later open sourced. GraphQL provides a better, powerful and flexible alternative to REST. Before we dive into GraphQL concepts, let's understand what's REST: REST stands for Representational State Transfer and it's an architectural pattern for designing client/server distributed systems. Unlike … -
Django Quiz 2017
Yesterday evening I gave a quiz at the London Django Meetup Group for the second year running. Here it is so you can do it at home (no cheating!). Answers are at the bottom. Part 1: Trivia 1. What species is Django’s unofficial spirit animal? Pegasus Unicorn Pony Seal Dolphin Elephant 2. Djangocon EU this year was in… Bologna Genoa Venice Florence 3. What does LTS stand for? Long Tail Support Long Term Support Life Time Support Life Term Support 4. What does WSGI stand for? Web Socket Gateway Interface Web Server Gateway Interface Web Server Gated Interface WebS GuardIan 5. What does ACID stand for? Atomicity Consistency Integrity Durability Atomicity Concurrency Isolation Durability Atomicity Consistency Isolation Durability All Carefully Inserted Data 6. When was the first commit on Django? One point for year, one for month, one for day 7. When was the first commit in Python? One point for year, one for month, one for day 8. What is the name of the current Django fellow? One point for first name, one for last Part 2: Coding with Django 1. What’s the import for the new Django 2.0 URL syntax? from django.paths import url from django.urls import path … -
Django REST Framework Authentication by Example with JSON Web Tokens (JWT)
Introduction Django Rest Framework provides multiple mechanisms for authenticating users, in case you are new to this concept then simply put: authentication is the process of verifying the identity of users, while authorization is identifying if the user has authorized access to some server resource. In this tutorial, we are going to see what's the available mechanisms, in DRF, to authenticate users? What is the difference between DRF built-in token-based authentication system and JWT authentication? And how to add JSON Web Tokens authentication to Django Rest Framework? Different Ways to Authenticate Users? The general process of authenticating a user is done by simply checking if any user information or credentials are attached to an incoming (from the client) request. DRF has already three mechanisms to authenticate users. Let's look at each one of them: Basic authentication: It's very easy to setup but it's only recommended for testing purposes not for production. It's implemented in rest_framework.authentication.BasicAuthentication class and works by base64 encoding the user login information i.e the user's name and the password then attach them to an HTTP Authorization Header (can then be retrieved from request.META.HTTP_ AUTHORIZATION). Session based authentication: The traditional authentication mechanism and the default one used by … -
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 …