Django community: Community blog posts RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
How To Show Correct List Item Indexes When Using Pagination in Django
In your Django template put something like this: <ul> {% for object in object_list %} <li>{{ forloop.counter0|add:page_obj.start_index }}. {{ object }}</li> {% endfor %} </ul> Where: object_list - is a list of objects produced by pagination; page_obj - is a page object produced by pagination page_obj ... Read now -
Versioning complex database migrations
Recently, I've been writing lots of raw SQL code that is either a complex ``VIEW``, or a ``FUNCTION``. Much of the time these will be used as the "source" for a Django model, but not always. Sometimes, there are complex functions that need to be run as a trigger in Postgres, or even a rule to execute when attempting a write operation on a view. Anyway, these definitions are all code, and should be stored within the project they belong to. Using Django's migrations you can apply them at the appropriate time, using a ``RunSQL`` statement. Hovewer, you don't really want to have the raw SQL in the migration file. Depending upon the text editor, it may not syntax highlight correctly, and finding the correct definition can be difficult. Similarly, you don't want to just have a single file, because to recreate the database migration sequence, it needs to apply the correct version at the correct time (otherwise, other migrations may fail to apply). Some time ago, I adopted a policy of manually versioning these files. I have a pattern of naming, that seemed to be working well: special_app/ migrations/ __init__.py 0001_initial.py 0002_update_functions.py sql/ foo.function.0001.sql foo.function.0002.sql foo.trigger.0001.sql bar.view.0001.sql The contents … -
How to get a list of all user permissions available in Django based project
Django comes with a simple permissions system. It provides a way to assign permissions to specific users and groups of users. The system adds "change, remove and add" permissions automatically to every model class. And it's pretty easy to add custom permissions to a model class, like this: class ... Read now -
Interview with Rivo Laks about Docker
This is an interview I took from Estonian Django developer Rivo Laks about Docker. Recorded in DjangoCon EU 2017, Florence, Italy. https://djangodeployment.com/wp-content/uploads/2017/05/rivo.mp3 A: Hello everyone, this is Antonis from djangodeployment.com. The date is 7 April 2017 and I am in Florence, Italy, at the European Django conference. I’m sitting with Rivo Laks, who is a developer from Estonia. He has a company called Thorgate, and we are going to be talking about Docker and similar things, mostly about deployment. Hello Rivo, how do you like the conference? R: Hello Antonis. I like it a lot and I think it’s my fifth DjangoCon now, so it’s been a pleasant experience throught the years. A: That’s very interesting because it’s actually my first Django conference. So what do you think it is that brings you here? R: I guess it’s the people, most importantly; and also the fact that this is a good place to keep you up to date with everything that is going on in the Django world. A: Would you like to tell me some examples of Django applications that your company makes? R: Well, it usually begins with some sort of business problem. One recent example is … -
Creating Smaller Docker Images Part #3: Alpine Linux
This is the third post in a series on making smaller Docker images. In the first post I talked about how to create smaller images by writing better Dockerfiles. In the second post I talked about how to squash layers using docker-squash to make smaller images. These methods are great but they won't help us if we choose large base images to start with! Let's look at the example from the second post, the standard `python` image on Docker hub. If we look at the Dockerfile for this image, as of this writing, it's based on a Debian jessie base image. FROM buildpack-deps:jessie [...] -
Creating Smaller Docker Images Part #3: Alpine Linux
This is the third post in a series on making smaller Docker images. In the first post I talked about how to create smaller images by writing better Dockerfiles. In the second post I talked about how to squash layers using docker-squash to make smaller images. These methods are great but they won't help us if we choose large base images to start with! Let's look at the example from the second post, the standard `python` image on Docker hub. If we look at the Dockerfile for this image, as of this writing, it's based on a Debian jessie base image. FROM buildpack-deps:jessie [...] -
On Conference Speaking
I’ve seen quite a bit of the world thanks to being invited to speak at conferences. Since some people are under the impression that serial conference speakers possess some special talents, I’d like to demystify my process by walking you through my latest talk from start to finish. -
How to Stop Worrying and Start Coding: Self-Help Tips for Developing a Web Front-End
During the four years I’ve worked as a front-end developer, I’ve gained a great deal of experience coding HTML pages. Today, I’m going to share some of that with you. Let’s imagine that you have just received a designer’s layout of a landing page — for a business card website, for example, or an administrative panel dashboard. Where do you start and how do you put everything in its proper place? Let’s find out! The post How to Stop Worrying and Start Coding: Self-Help Tips for Developing a Web Front-End appeared first on Distillery. -
Django REST Framework: JSON Web Tokens (JWT)
Our last post was about Authentication and Permissions and we covered the available methods of authentication in Django REST Framework. In that post, we learned how to use the built in Token based authentication in DRF. In this post, we will learn more about JSON Web Tokens aka JWT and we will see if JWT can be a better authentication mechanism for securing our REST APIs. Understanding JSON Web Tokens (JWTs) We have actually written a detailed blog post about JSON Web Tokens earlier. In case you have missed it, you probably should read it first. We have also described how to use JWT with Flask – reading that one might also help better understand how things work. And of course, we will briefly cover the idea of JWT in this post as well. If we want to put it simply – you take some data in JSON format, you hash it with a secret and you get a string that you use as a token. You (your web app actually) pass this token to the user when s/he logs in. The user takes the token and on subsequent requests, passes it back in the “Authorization” header. The web app … -
Django 1.11 : Create or start your first project
Django is a Python based framework which offers developers all they need to create a web apps and websites in a clean ,rapid and a pragmatic way . How do I start a Django project ? is the first quesion which gets asked by a beginner Django developer so lets answer it . Tutorial requirements To better grasp this tutorial ,you need to have some Python experience and also how to work with Linux bash commands or Windows command prompt if you are using Windows for development . Development requirements Before you can create or start a Django project you need to have you development environment setup . Here is what you need to install before continuing with this tutorial : Python 2.7.x or 3.4.x PIP package manager Virtualenv Django Code editor (Sublime,gedit etc.) If you are using a Linux/MAC machine you should have Python already installed . You can check that running the following command on your terminal python -v If you don't have Python installed head over to Python download page PIP is a Python package manager used to easily install Python packages and their dependencies .You can install PIP using curl utility curl "https://bootstrap.pypa.io/get-pip.py" -o "get-pip.py" python … -
Create an Ionic 3 mobile app with Python Django as backend
In this two parts tutorial ,we are going to recreate our previous Ionic 3 product inventory manager application which used a local SQLite database to use a Python Django backend . -
Django 1.11 : Create super user for Admin back office
After you successfully created your first project with Django 1.11 ,created an app and migrated your SQlite database .It's time to access the admin interface or back office generated automatically by Django which you can use to create ,delete ,edit and list your database tables data . You can visit the admin interface from http://localhost:8200/admin You'll be presented with a form to enter your username and password credentials .If you enter any values you'll get a message : Please enter the correct username and password for a staff account. Note that both fields may be case-sensitive. Since you have no staff user created yet but don't worry this can be fixed with one command . Head over to your terminal ane run : python manage.py createsuperuser You'll be prompted for a username and a password (twice) ,enter them and hit Enter . Next run your local server again : python manage.py runserver Then visit your admin interface http://localhost:8200/admin and enter your credentials then hit Login button . You should be successfully logged in . -
Ionic 3 : Create a cross platform mobile application with Python Django and Django Rest Framework (DRF)
In this two parts tutorial ,we are going to recreate our previous Ionic 3 product inventory manager application ,which used a local SQLite database ,to use a Python Django backend . The application we'll be creating in this tutorial uses Ionic 3 as a frontend ,Django as a backend and Django Rest Framework to create Rest API which will be consumed by Ionic . It's a simple product inventory management application which shows you many Django concepts such as How to create a Django project from scratch . How to create a Django app . How to model and create database models . How to migrate your database . How to generate the Admin web interface to create ,update ,delete and view database records . How to create a super user . How to generate a browsable and documented Rest API with Django Rest Framework . What is Django ? Django is a Python based web framework which encourages rapid development which is used by many web developers create web applications .It has a clean and pragmatic design which can help ypu create complete prototypes in a hours .Django packages or apps for organization and reuse .You can either create … -
Django vs Flask in 2017
The Python language has many great frameworks for building web applications among them Django and Flask . Both frameworks are gaining more popularity steadily in time and there are no signs they are becoming less popular or obsolete even if the web has known many important changes since the data they have been created . Django was created in 2005 while Flask was created in 2010 .Today we are in 2017 which means Django is 12 years old and Flask is 7 years old .They are both extremely popular frameworks to build web applications with Python but they are so different in principle from each other .While Django follows a battery included approach making it a full packed ,complete and an opinionated framework ,on the other hand Flask is a micro framework ,an un-opinionated framework that let you choose what tools you can use for building a web app from the ORM to the templating engine . In this post we'll discuss some points you might want to consider if you need to learn Django or Flask or maybe both ? and which framework you should consider using to build your next project ? and in which situations ? They … -
Django vs Flask in 2017
The Python language has many great frameworks for building web applications among them Django and Flask . Both frameworks are gaining more popularity steadily in time and there are no signs they are becoming less popular or obsolete even if the web has known many important changes since the data they have been created . Django was created in 2005 while Flask was created in 2010 .Today we are in 2017 which means Django is 12 years old and Flask is 7 years old .They are both extremely popular frameworks to build web applications with Python but they are so different in principle from each other .While Django follows a battery included approach making it a full packed ,complete and an opinionated framework ,on the other hand Flask is a micro framework ,an un-opinionated framework that let you choose what tools you can use for building a web app from the ORM to the templating engine . In this post we'll discuss some points you might want to consider if you need to learn Django or Flask or maybe both ? and which framework you should consider using to build your next project ? and in which situations ? They … -
Django 1.11 : Create super user for Admin back office
After you successfully created your first project with Django 1.11 ,created an app and migrated your SQlite database .It's time to access the admin interface or back office generated automatically by Django which you can use to create ,delete ,edit and list your database tables data . You can visit the admin interface from http://localhost:8200/admin You'll be presented with a form to enter your username and password credentials .If you enter any values you'll get a message : Please enter the correct username and password for a staff account. Note that both fields may be case-sensitive. Since you have no staff user created yet but don't worry this can be fixed with one command . Head over to your terminal ane run : python manage.py createsuperuser You'll be prompted for a username and a password (twice) ,enter them and hit Enter . Next run your local server again : python manage.py runserver Then visit your admin interface http://localhost:8200/admin and enter your credentials then hit Login button . You should be successfully logged in . -
Django 1.11 : Create or start your first project
Django is a Python based framework which offers developers all they need to create a web apps and websites in a clean ,rapid and a pragmatic way . How do I start a Django project ? is the first quesion which gets asked by a beginner Django developer so lets answer it . Tutorial requirements To better grasp this tutorial ,you need to have some Python experience and also how to work with Linux bash commands or Windows command prompt if you are using Windows for development . Development requirements Before you can create or start a Django project you need to have you development environment setup . Here is what you need to install before continuing with this tutorial : Python 2.7.x or 3.4.x PIP package manager Virtualenv Django Code editor (Sublime,gedit etc.) If you are using a Linux/MAC machine you should have Python already installed . You can check that running the following command on your terminal python -v If you don't have Python installed head over to Python download page PIP is a Python package manager used to easily install Python packages and their dependencies .You can install PIP using curl utility curl "https://bootstrap.pypa.io/get-pip.py" -o "get-pip.py" python … -
How to Configure Mailgun To Send Emails in a Django Project
In this tutorial you will learn how to setup a Django project to send emails using the Mailgun service. Previously I’ve published in the blog a post about how to configure SendGrid to send emails. It’s a great service, but they don’t offer free plans anymore, nowadays it’s just a 30 days free trail. So, I thought about sharing the whole email setup with a better option to get started. Mailgun is great and super easy to setup. The first 10,000 emails you send are always free. The only downside is that if you don’t provide a payment information (even though you are only going to use the first 10,000 free emails), there will be some limitations, such as requiring to configure “Authorized Recipients” for custom domains, which pretty much makes it useless, unless you know beforehand the email addresses you will be sending emails. Anyway, let’s get started. Initial Setup Go to www.mailgun.com and create a free account. Sign in with your Mailgun account, click on Domains and then Add New Domain. I will setup the Mailgun service for a domain I own, “www.bottlenose.co”. For the setup, it’s advised to use the “mg” subdomain, so you will need to … -
8 Reasons to Affirm Your Legacy in a Blog
-
Django efficient implementation of Amazon s3 and Cloudfront CDN for faster loading.
Django by default to store the files in your local file system. To make your files load quickly and secure we need to go for any third party storage systems. AWS s3 is one of the storage service for the Internet. It is designed to make web-scale computing easier for developers. django has a package called django-storages which can be used to store the files in the amazon s3. and serve them from its cloud front service. Implementing django-storages with amazon s3 as storage service in your django application: To implment django-storages in your django application you need to follow the following steps. 1. Install django-storages: We can install django-storages with the following command. pip install django-storages 2. Keep storages in your installed apps. INSTALLED_APPS = ( ... 'storages', ... ) 3. Change your DEFAULT_FILE_STORAGE setting in your settings.py file: Django by default comes with a setting called DEFAULT_FILE_STORAGE which deals with file storage. By default its value is 'django.core.files.storage.FileSystemStorage'. By keeping this value to DEFAULT_FILE_STORAGE, django, by default tries to store the files on your local machine. To store your files in amazon s3 just change that value to 'storages.backends.s3boto3.S3Boto3Storage' DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage' 4. Configure your AWS ACCESS_KEY … -
Interview with Andrew Godwin about deployment
This is an interview I took from Django core developer Andrew Godwin about deployment. Recorded in DjangoCon EU 2017, Florence, Italy. https://djangodeployment.com/wp-content/uploads/2017/05/godwin.mp3 ANT: Hello everyone! This is Antonis from djangodeployment.com. It’s 5 April 2017 and I am at the European Django Conference in Florence. I am sitting with Andrew Godwin, who is a Django core developer mostly known as the creator of migrations and now Django Channels, and we are going to have a little chat about deployment. Hello Andrew! AND: Hello! It’s lovely to talk to you! ANT: Thanks! Some years ago you tried to create a deployment service which was called epio. So, could you tell me a little bit more about that? AND: Of course! As a bit of background history, we’re talking quite a few years now when Heroku was just for Ruby at the time, and so I was sitting in DjangoCon US and me and a friend had the idea to, why not do Heroku for Python?, which of course was an idea common to several startups at that time. We took on the challenge of making a platform-as-a-service, which is what Heroku of course is. I think it was two or three … -
How to Deploy a Django Application on RHEL 7
In this tutorial, you will learn how to deploy a Django application with PostgreSQL, Nginx, Gunicorn on a Red Hat Enterprise Linux (RHEL) version 7.3. For testing purpose I’m using an Amazon EC2 instance running RHEL 7.3. Recently I had to deploy an existing Django project running on Ubuntu 16.04 to a new environment, RHEL 7.3. It gave me some headache because I don’t have much server administration skills and I wasn’t familiar with Security Enhanced Linux (SELinux) distributions, so I thought about sharing the details of the deployment so it could help someone in the same position I was. If you are just getting started with Django deployment, and doesn’t have a good reason to be using RHEL, I suggest you use Ubuntu instead. It requires less configuration, and the process is fairly easier than using RHEL. Perhaps you could check this past tutorial: How to Deploy a Django Application to Digital Ocean. Anyway, for this tutorial I will deploy the following Django application: github.com/sibtc/urban-train. It is just an empty Django project to demonstrate the deployment process. So, every time you see urban-train, change it for your project name. Initial Setup First, let’s install all the needed resources and … -
3 Reasons to Upgrade to the Latest Version of Django
When considering a website upgrade, many business stakeholders probably think about the frontend, i.e., how the website looks or the features users interact with. Perhaps less often considered is the importance of upgrading the backend; that is, the databases, applications, and servers powering all the behind-the-scenes activity. Infrastructure support and upgrades are necessary but often performed as a separate project from any improvements to design or user experience, rather than as part of a holistic update project. With that in mind, it helps to have an understanding of why upgrading the backend should be considered a necessary part of any website upgrade project. We offer 3 reasons, focusing on our specialty of Django-based websites. Upgrading: increases security, reduces development and maintenance costs, and ensures support for future growth. Read on for details about each of these factors, or get in touch to speak with us about them. Increase the security of your site The Django framework is continually being improved and certain releases are designated as “Long Term Support” (LTS) versions. LTS versions receive security updates and bug fixes for a three-year period, as opposed to the usual 18 months. When your website uses an unsupported version of Django, newly … -
DroidShoter — Making Application Screenshots With Various Resolutions, Using The Same Device
As you know, there are many different devices using Android. As a result, there are also an incredible number of displays with different combinations of resolution and pixel density to take into account, which makes the entire development process significantly more complex. At one point, we at Distillery decided to make the testing process on various displays easier and avoid running the application on different devices and virtual machines. After performing a thorough analysis of the problem, the solution became obvious — it is one which could be implemented using Adb and only one connected device. As a result, we managed to develop cross-platform utility software that automatically saves application screenshots with various parameters of the display without any need to change the source code of the application (it also works with various emulators and virtual machines). After running the utility software, the developer gets several screenshots of the application, which reflect the appearance of it on devices with different configurations. The developer can use these screenshots in order to find application artifacts and to analyze the level of layout optimality. The post DroidShoter — Making Application Screenshots With Various Resolutions, Using The Same Device appeared first on Distillery. -
How to Document API Requests using Django Rest Swagger
In Developing an application, we'll write our required API's. To Document these API's we use Django Rest Swagger. Django Rest Swagger is used to provide Documentation for all API's which are used in your application with brief description about each API individually. It is Open Source so you can contribute to the project. If your API's available to the users with description, so UI developers can understand and test your API's and use it accordingly. First we need to install the swagger. pip install django-rest-swagger Add "rest_framework_swagger" to Django settings INSTALLED_APPS INSTALLED_APPS = ( ... 'rest_framework_swagger', ) Include the rest_framework_swagger URLs to a path of your choice patterns = ('', ... url(r'^docs/$', schema_view, name="schema_view"), ) In your API Views from rest_framework.decorators import api_view, permission_classes, renderer_classes from rest_framework.permissions import AllowAny from rest_framework_swagger.renderers import OpenAPIRenderer, SwaggerUIRenderer @api_view() @permission_classes((AllowAny, )) @renderer_classes([OpenAPIRenderer, SwaggerUIRenderer]) def schema_view(request): generator = schemas.SchemaGenerator(title='Rest Swagger') return Response(generator.get_schema(request=request)) If you are not authorized, This document List all the API which doesnt reguire any permission to get authorize. If you want to use Rest Swagger in your application to authorize user, include below settings in your settings.py file. By this …