Django community: Community blog posts RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
Machine learning empowered blackkiwi
A blackkiwi which tells your mood. Blackkiwi is a django powered website which uses machine learning to tell if you wrote happy or sad things on your latest Facebook status. And it tends to be wayyy positive. But we will get there… The genesis of blackkiwi There were two main things combined with the genesis of blackkiwi. The first it was this curiosity about Natural Text processing and classificaiton techniques. In particular I wanted to write some classifers to see how well they were performing and I also wanted to try to do and test something new. But I needed some kind of application. This is usually a good trick in programming in general. If you build towards something, it is always easier to stay motivated and actually getting it done, instead of giving up the hobby and end up playing World of tanks on the play :). The second ingredient was to try to test the release process via gitlab, using automatic push via CI to a server. As stack I wanted to use a classic dokku stack which I’m very happy to use, beccause it basically brings the nice and easy way to deploy similar to heroku/gondor style … -
Django REST framework (DRF) with Angular 2+ tutorial (Part 1)
This is the first part of a tutorial series about getting started with Django framework and Angular 2 . The final objective is to create a restful web application which has two separate parts .The first part is the back end which is created with Django and the Django REST framework ,the second part is the front end which is built using Angular 2+ framework . Django REST framework (DRF) with Angular 2+ tutorial (Part 1) Django REST framework (DRF) with Angular 2+ tutorial (Part 2) Django REST framework (DRF) with Angular 2+ tutorial (Part 3) We are going to use Django 1.11 ,Django REST framework 3.6.2 and Angular 2+ . Before starting ,this tutorial has a bunch of development requirements : You need to have Python installed and configured PIP and Virtualenv installed Node.js and Angular 2 CLI installed Objectives of the part 1 In the first part of these tutorial series ,we are going to cover basic introductions to the two server side used frameworks Django and DRF ,and their initial setup . Getting started with Django and DRF . The custom project anatomy and structure (front end and back end) Getting started with Django and DRF Django … -
Django REST framework (DRF) and Angular 2 tutorial (Part 1)
This is the first part of a tutorial series about getting started with Django framework and Angular 2 . The final objective is to create a restful web application which has two separate parts .The first part is the back end which is created with Django and the Django REST framework ,the second part is the front end which is built using Angular 2 framework . We are going to use Django 1.11 ,Django REST framework 3.6.2 and Angular 2 . Before starting ,this tutorial has a bunch of development requirements : You need to have Python installed and configured PIP and Virtualenv installed Node.js and Angular 2 CLI installed Objectives of the part 1 In the first part of these tutorial series ,we are going to cover basic introductions to the two server side used frameworks Django and DRF ,and their initial setup . Getting started with Django and DRF . The custom project anatomy and structure (front end and back end) Getting started with Django and DRF Django is a web framework based on Python which allows developers to quickly prototype and build web applications .It has a powerful ORM that abstracts how you can interact with popular … -
Make More Room: How to Resolve Database Record Limitations
Sometimes in the process of designing a database, developers can underestimate the scope of a project. If that project were to become successful, it would result in significant loads and put undue strain on the system and it’s developers down the line. Eventually, people realize that they are using an INT identity key with a maximum value of 2,147,483,647 as the primary key of a huge table. The post Make More Room: How to Resolve Database Record Limitations appeared first on Distillery. -
How To Write Custom migrations in Django
Migrations are mainly for keeping the data model of your database up-to-date. Django will never run same migration file more than once on the same database. All this is managed by table named "django_migrations" which gets created when we first apply the migrations. For each migration(file), a new record(row) is inserted into this table. There are also a few “special” operations of Migration class which can be used to add our custom migrations. RunSQL This allows us to execute raw SQL. Example: from __future__ import unicode_literals from django.db import migrations, models class Migration(migrations.Migration): dependencies = [ ... dependencies goes here ] operations = [ migrations.RunSQL("INSERT INTO product (title) VALUES ('Product1');"), ] RunPython This can be used to run custom Python code. Here code should be separate function that accepts two arguments - first is an instance of django.apps.registry.Apps containing historical models that match the operation’s place in the project history, and the second is an instance of SchemaEditor. Example: from __future__ import unicode_literals from django.db import migrations def create_countires(apps, schema_editor): Country = apps.get_model("app_name", "Country") Country.objects.bulk_create([ Country(name="India"), Country(name="USA"), ]) class Migration(migrations.Migration): dependencies = [ ... dependencies goes here ] operations = [ migrations.RunPython(create_countires), ] Once we have the function we can … -
Django efficient implementation of Amazon s3 and Cloufront 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 … -
How To Add A Custom Managers In Django
Django Custom Managers A Manager is used to query database operations which will be provided to Django models. Every model in Django application consists at least one Manager by default. Regarding the Manager names: By default, Django adds a Manager with the name "objects" to every Django model class. However, if you want to use "objects" as a field name (or) if you want to use a name instead of "objects" for the Manager, you can rename it on a per-model basis. To rename the Manager for a given class, define a class attribute of type models.Manager() on that model. For example: from django.db import models class Employee(models.Model): gender_choices = ( ("M", "Male"), ("F", "Female") ) roles_choices = ( ("J", "Junior"), ("S", "Senior"), ) first_name = models.CharField(max_length=200) last_name = models.CharField(max_length=200) email = models.CharField(max_length=250) gender = models.CharField(max_length=1, choices=gender_choices) role = models.CharField(max_length=120, choices=roles_choices, default="J") active = models.BooleanField(default=True) # custom manager replaces objects manger all_employees = models.Manager() def __str__(self): return str(self.first_name) + str(self.last_name) Using the above example model, Employee.objects will throw you an AttributeError exception, but Employee.all_employees.all() will provide a list of all Employee objects. Now, here we go for the Custom Managers: You can use a custom Manager in a particular model … -
Custom validations for serializer fields Django Rest Framework
why we use validators ? Validators are used to validate the data whether it is semantically valid or not. Validation simplifies the data processing Validation avoids the data redundancy Custom Validation for serializer fields Django REST supports both serializers and model serializers. Serializers provides basic validation for fields, In some cases we need to write custom validations for fields . Let's take an example for validation for serializer fields. Case: A company want's recruit for the position of "django developer". Company set age restriction on applicant that his/her age should be greater than twenty and less than thirty years. Method-1: from datetime import date from rest_framework import serializers def age_restriction(dob): today = date.today() age = today.year - dob.year - ((today.month, today.day) < (dob.month, dob.day)) if (not(20 < age < 30)): raise serializers.ValidationError("You are no eligible for the job") return dob class EligibilitySerializer(serializers.Serializer): email = serializers.EmailField() name = serializers.CharField(max_length=200) date_of_birth = serializers.DateField(validators=[age_restriction]) "serializers.Serializer" apply primary validations on the field when we call method "is_valid". Serializer converts the value of the field into python object. After it checks for the attribute "validate_<field_name>" if it has the attribute it will the attribute(method). After this validation it will check for "validators" attribute for the field. … -
Python Memcached Implementation for Django project
A fundamental trade-off in dynamic websites is they’re dynamic. Every time a user requests a page, the Web server fetches data from database then applies logic and renders in templates. This will affect page load time due to time taken by server to fetch data and apply the business logic, That’s where caching comes in. To cache something is to save the result of an expensive calculation so that you don’t have to perform the calculation next time. In This Blog Post, Lets see how to use Memcached for server-side application caching. Memcached is an in-memory key-value pair store, that helps in caching dynamic websites. Django uses python-memcached binding for communication between our web application and Memcached Server. apt-get install memcached #to install memcached-server pip install python-memcached Add the following settings to settings.py or django settings CACHES = { 'default': { 'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache', 'LOCATION': 'unix:/tmp/memcached.sock', } } Now you're all set use caching. from django.core.cache import cache cache.set(cache_key, result, cache_time) # to cache a value cache.set_many({'a': 1, 'b': 2, 'c': 3}) # to cache many keys cache.get(cache_key) # to retive a key from cache cache.get_many(['a', 'b', 'c']) # to retrive many keys from cache To use as Decorator: from django.views.decorators.cache import … -
ShipIt Day Recap Q2 2017
Once per quarter, Caktus employees have the opportunity to take a day away from client work to focus on learning or refreshing skills, testing out ideas, or working on open source contributions. The Q2 2017 ShipIt Day work included building apps, updating open source projects, trying out new tools, and more. Keep reading for the details. PostgreSQL Performance Erin used ShipIt Day to watch a tutorial on Postgres performance by Craig Kerstiens and test the Caktus website with some of the things she learned. She used the free pgAdmin III tool to try out some of Craig’s suggested database queries for performance monitoring. While drilling down into our website, she explored cache and index hit rates, reviewed query performance on our blog, and tested with pg_stat_statements to find the most expensive queries in aggregate across database. Erin plans to use her findings to inform decisions impacting website performance. GitHub Pull Requests Tool Dan built a tool to help with GitHub pull requests. The tool watches a pull request until it’s ready to merge, then merges it for him. He built it from scratch using the requests library and GitHub API. The tool works by reloading the page occasionally to see … -
How to deploy a Django project in 15 minutes with Ansible
In this tutorial I will assume that you are a Django developer and you have built and tested a project locally. It’s time to deploy the project on a public server to let users access your awesome application. So you need a VPS with an SSH access, then you will access the server, install and configure all necessary software (web server, application server, database server), create a database user, configure Django to use it, copy your Django project on the server, migrate the database, collect static files, trial and error, fix, trial and error, … All this boring stuff will take some good hours that you should definitely spend in a more profitable way, don’t you think? The good news is that you can automate almost all the work needed to go from a vanilla VPS to a fully deployed server hosting your Django project. Follow this tutorial and I’ll show you how to leverage the power of Ansible to automate all the needed steps in 15 minutes. Are you ready? Check the time on your clock and follow me! 1. Setup the SSH access to your VPS There are plenty of good VPS providers out there, and the choice … -
How to deploy a Django project in 15 minutes with Ansible
In this tutorial I will assume that you are a Django developer and you have built and tested a project locally. It’s time to deploy the project on a public server to let users access your awesome application. So you need a VPS with an SSH access, then you will access the server, install and configure all necessary software (web server, application server, database server), create a database user, configure Django to use it, copy your Django project on the server, migrate the database, collect static files, trial and error, fix, trial and error, … All this boring stuff will take some good hours that you should definitely spend in a more profitable way, don’t you think? The good news is that you can automate almost all the work needed to go from a vanilla VPS to a fully deployed server hosting your Django project. Follow this tutorial and I’ll show you how to leverage the power of Ansible to automate all the needed steps in 15 minutes. Are you ready? Check the time on your clock and follow me! 1. Setup the SSH access to your VPS There are plenty of good VPS providers out there, and the choice … -
How to deploy a Django project in 15 minutes with Ansible
In this tutorial I will explain how to deploy a Django project in 15 minutes with Ansible. I will assume that you are a Django developer and you have built and tested a project locally. It’s time to deploy the project on a public server to let users access your awesome application. If you are new in deploying Django on a production server you can read my post Django – NGINX: deploy your Django project on a production server to have a basic introduction on the steps needed. So you need a VPS with an SSH access, then you will access the server, install and configure all necessary software (web server, application server, database server), create a database user, configure Django to use it, copy your Django project on the server, migrate the database, collect static files, trial and error, fix, trial and error, … All this boring stuff will take some good hours that you should definitely spend in a more profitable way, don’t you think? The good news is that you can automate almost all the work needed to go from a vanilla VPS to a fully deployed server hosting your Django project. Follow this tutorial and I’ll … -
HTTPS behind your reverse proxy
We have a setup that looks (simplified) like this: HTTP/HTTPS connections from browsers ("the green cloud") go to two reverse proxy servers on the outer border of our network. Almost everything is https. Nginx then proxies the requests towards the actual webservers. Those webservers also have nginx on them, which proxies the request to the actual django site running on some port (8000, 5010, etc.). Until recently, the https connection was only between the browser and the main proxies. Internally inside our own network, traffic was http-only. In a sense, that is OK as you've got security and a firewall and so. But... actually it is not OK. At least, not OK enough. You cannot trust in only a solid outer wall. You need defense in depth. Network segmentation, restricted access. So ideally the traffic between the main proxies (in the outer "wall") to the webservers inside it should also be encrypted, for instance. Now, how to do this? It turned out to be pretty easy, but figuring it out took some time. Likewise finding the right terminology to google with :-) The main proxies (nginx) terminate the https connection. Most of the ssl certificates that we use are wildcard … -
Building a Custom Block Template Tag
Building custom tags for Django templates has gotten much easier over the years, with decorators provided that do most of the work when building common, simple kinds of tags. One area that isn't covered is block tags, the kind of tags that have an opening and ending tag, with content inside that might also need processing by the template engine. (Confusingly, there's a block tag named "block", but I'm talking about block tags in general). A block tag can do pretty much anything, which is probably why there's not a simple decorator to help write them. In this post, I'm going to walk through building an example block tag that takes arguments that can control its logic. Django Documentation There are a couple of pages in the Django documentation that you should at least scan before continuing, and will likely want to consult while reading: Custom template tags and filters is the place to start. It documents the general approach and the machinery we use for writing custom tags. The Django template language: for Python programmers has two sections of interest to us: Rendering a context and Playing with Context objects. What our example tag will do Let's write a … -
Continuous Integration and Deployment with Drone, Docker, Django, Gunicorn and Nginx - Part 2
The Introduction This is the second part of a multi-part tutorial covering a simple(ish) setup of a continuous integration/deployment pipeline using Drone.io. Since Part 1, I’ve added a GitHub project outlining a simple Django application that you can use as a reference. In Part 2, we will be adding a publish step to our application’s drone.yml in order to push our image to Docker Hub. Having our image in Docker Hub allows us to easily pull our app’s image onto our staging/production server to allow easy automated deployment. After the publish step we will add a deploy step to the drone.yml that will be responsible for SSHing into an EC2 instance (or wherever your app lives) to pull our newly pushed image and update our app. Let’s get started! Step 1: Publish our app’s image to Docker Hub after a successful pull request. If you recall, our drone.yml should successfully be running our application’s test suite on push and pull events and reporting back to GitHub if our test suite failed or succeeded. After a successful build, we would like to push our app’s Docker image to Docker Hub. Sign up/Log in to Docker Hub Having an account on Docker Hub is free and it … -
Painless Travis CI/CD for Android Project
If you still want your fancy new mobile projects to be continuously tested and pull-requests checked automatically without wanting to spend your entire evening resolving Travis issues, we at Distillery have a solution. The post Painless Travis CI/CD for Android Project appeared first on Distillery. -
Python F-Strings Are Fun!
In python 3.6 we saw the adoption of Literal String Interpolation, or as they are known more commonly, f-strings. At first I was hesitant because... well... we've got multiple string tools already available: one, two = 1, 2 _format = '{},{}'.format(one, two) _percent = '%s,%s' % (one, two) _concatenation = str(one) + ',' + str(two) _join = ','.join((str(one),str(two))) assert _format == _percent == _concatenation == _join Adding f-strings to this mix didn't seem all that useful: _fstring = f'{one},{two}' assert _fstring == _format == _percent == _concatenation == _join I was doubtful, but then I tried out f-strings on a non-trivial example. Now I'm hooked. Be it on local utility scripts or production code, I now instinctively gravitate toward their usage. In fact, f-strings are so useful that going back to earlier versions of Python now feels cumbersome. The reason why I feel this way is that f-strings are concise but easy to understand. Thanks to intuitive expression evaluation I can compress more verbose commands into smaller lines of code that are more legible. Take a look: _fstring = f'Total: {one + two}' # Go f-string! _format = 'Total: {}'.format(one + two) _percent = 'Total: %s' % (one + two) … -
Python F-Strings Are Fun!
In python 3.6 we saw the adoption of Literal String Interpolation, or as they are known more commonly, f-strings. At first I was hesitant because... well... we've got multiple string tools already available: one, two = 1, 2 _format = '{},{}'.format(one, two) _percent = '%s,%s' % (one, two) _concatenation = str(one) + ',' + str(two) _join = ','.join((str(one),str(two))) assert _format == _percent == _concatenation == _join Adding f-strings to this mix didn't seem all that useful: _fstring = f'{one},{two}' assert _fstring == _format == _percent == _concatenation == _join I was doubtful, but then I tried out f-strings on a non-trivial example. Now I'm hooked. Be it on local utility scripts or production code, I now instinctively gravitate toward their usage. In fact, f-strings are so useful that going back to earlier versions of Python now feels cumbersome. The reason why I feel this way is that f-strings are concise but easy to understand. Thanks to intuitive expression evaluation I can compress more verbose commands into smaller lines of code that are more legible. Take a look: _fstring = f'Total: {one + two}' # Go f-string! _format = 'Total: {}'.format(one + two) _percent = 'Total: %s' % (one + two) … -
Two Scoops of Django 1.11 is Out!
When we started the Two Scoops of Django project back in 2012, I never thought it would become a book series. Well, it's turned into just that, and so I'm pleased to announce the "Early Release" or "BETA" release of the Two Scoops of Django: Best Practices for Django 1.11 PDF ebook. Co-authored with Audrey Roy Greenfeld, the 1.11 edition of Two Scoops of Django is filled to the brim with knowledge to help make Django projects better. We introduce various tips, tricks, patterns, code snippets, and techniques that we've picked up over the years. What we didn't know or weren't certain about, once again we found the best experts in the world and asked them for the answers. Then we packed the result into a 530+ page book. What's Next? We'll be adding more material to the 1.11 edition in the near future, hence the term, "Early Release". Everyone who buys the 1.11 ebook from us gets all 1.11 ebook updates. Once we're happy with the ebook, we'll release a print paperback edition, scheduled for May or June. We're selling the book in PDF format, as well as taking pre-orders for the print edition. Order You can purchase the … -
Two Scoops of Django 1.11 is Out!
When we started the Two Scoops of Django project back in 2012, I never thought it would become a book series. Well, it's turned into just that, and so I'm pleased to announce the \"Early Release\" or \"BETA\" release of the Two Scoops of Django: Best Practices for Django 1.11 PDF ebook. Co-authored with Audrey Roy Greenfeld, the 1.11 edition of Two Scoops of Django is filled to the brim with knowledge to help make Django projects better. We introduce various tips, tricks, patterns, code snippets, and techniques that we've picked up over the years. What we didn't know or weren't certain about, once again we found the best experts in the world and asked them for the answers. Then we packed the result into a 530+ page book. What's Next? We'll be adding more material to the 1.11 edition in the near future, hence the term, \"Early Release\". Everyone who buys the 1.11 ebook from us gets all 1.11 ebook updates. Once we're happy with the ebook, we'll release a print paperback edition, scheduled for May or June. We're selling the book in PDF format, as well as taking pre-orders for the print edition. Order You can purchase the … -
Newsletter #6
Welcome to the latest news about Two Scoops Press, Daniel Roy Greenfeld (pydanny), and Audrey Roy Greenfeld (audreyr). Two Scoops of Django 1.11 Early Release Is Out We just released the early release (BETA) of the fourth edition of Two Scoops of Django. This supports the Django 1.11 Long Term Support (LTS) release, which will be maintained until at least April of 2020. In late May, we plan to release the print paperback version. Today you can purchase the early release PDF as well as pre-order the print edition by itself or as a bundle with the PDF. Two Scoops of Django 1.11 in our shop If you have any questions, please read the Two Scoops of Django 1.11 FAQ. Two Scoops Press Legacy Sale For the first time since 2013, we're running a sale! We uncovered two boxes, one that contained several hardcover editions of Two Scoops of Django 1.6, and the other was a set of Two Scoops of Django 1.8 misprints (slight alignment oddities, minor cover coloration issues, or light scuffing). They are on sale for as long as our supplies last. Two Scoops of Django 1.6 Hardcover ($30 off) Two Scoops of Django 1.8 Misprint + … -
Newsletter #6
Welcome to the latest news about Two Scoops Press, Daniel Roy Greenfeld (pydanny), and Audrey Roy Greenfeld (audreyr). Two Scoops of Django 1.11 Early Release Is Out We just released the early release (BETA) of the fourth edition of Two Scoops of Django. This supports the Django 1.11 Long Term Support (LTS) release, which will be maintained until at least April of 2020. In late May, we plan to release the print paperback version. Today you can purchase the early release PDF as well as pre-order the print edition by itself or as a bundle with the PDF. Two Scoops of Django 1.11 in our shop If you have any questions, please read the Two Scoops of Django 1.11 FAQ. Two Scoops Press Legacy Sale For the first time since 2013, we're running a sale! We uncovered two boxes, one that contained several hardcover editions of Two Scoops of Django 1.6, and the other was a set of Two Scoops of Django 1.8 misprints (slight alignment oddities, minor cover coloration issues, or light scuffing). They are on sale for as long as our supplies last. Two Scoops of Django 1.6 Hardcover ($30 off) Two Scoops of Django 1.8 Misprint + … -
Newsletter #6
Welcome to the latest news about Two Scoops Press, Daniel Roy Greenfeld (pydanny), and Audrey Roy Greenfeld (audreyr). Two Scoops of Django 1.11 Early Release Is Out We just released the early release (BETA) of the fourth edition of Two Scoops of Django. This supports the Django 1.11 Long Term Support (LTS) release, which will be maintained until at least April of 2020. In late May, we plan to release the print paperback version. Today you can purchase the early release PDF as well as pre-order the print edition by itself or as a bundle with the PDF. Two Scoops of Django 1.11 in our shop If you have any questions, please read the Two Scoops of Django 1.11 FAQ. Two Scoops Press Legacy Sale For the first time since 2013, we're running a sale! We uncovered two boxes, one that contained several hardcover editions of Two Scoops of Django 1.6, and the other was a set of Two Scoops of Django 1.8 misprints (slight alignment oddities, minor cover coloration issues, or light scuffing). They are on sale for as long as our supplies last. Two Scoops of Django 1.6 Hardcover ($30 off) Two Scoops of Django 1.8 Misprint + … -
Newsletter #6
Welcome to the latest news about Two Scoops Press, Daniel Roy Greenfeld (pydanny), and Audrey Roy Greenfeld (audreyr). Two Scoops of Django 1.11 Early Release Is Out We just released the early release (BETA) of the fourth edition of Two Scoops of Django. This supports the Django 1.11 Long Term Support (LTS) release, which will be maintained until at least April of 2020. In late May, we plan to release the print paperback version. Today you can purchase the early release PDF as well as pre-order the print edition by itself or as a bundle with the PDF. Two Scoops of Django 1.11 in our shop If you have any questions, please read the Two Scoops of Django 1.11 FAQ. Two Scoops Press Legacy Sale For the first time since 2013, we're running a sale! We uncovered two boxes, one that contained several hardcover editions of Two Scoops of Django 1.6, and the other was a set of Two Scoops of Django 1.8 misprints (slight alignment oddities, minor cover coloration issues, or light scuffing). They are on sale for as long as our supplies last. Two Scoops of Django 1.6 Hardcover ($30 off) Two Scoops of Django 1.8 Misprint + …