Django community: RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
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. -
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. -
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 + … -
SSL/TLS Settings for Django
Let's make your Django project... -
S3 Static & Media Files for Django
Using Amazon Web Services (AWS... -
The luxury of a creative community
The last few months of Evennia development have mainly focused around a series of very nice pull requests from a growing cadre of contributors. I have myself mainly been working away in Evennia's 'devel' branch which will lead up to Evennia 0.7.Contributed goodnessPeople have put in a lot of good work to boost Evennia, both by improving fixing existing things and by adding new features (small selection). Thanks a lot everyone!Contrib: Turn-based combat system - this is a full, if intentionally bare-bones implementation of a combat system, meant as a template to put in your particular game system into.Contrib: Clothing sytem - a roleplaying mechanic where a character can 'wear' items and have this show in their descriptions. Worn items can also be layered to hide that underneath. Had plenty of opportunities for extensions to a given game.Contrib: An 'event' system is in the works, for allowing privileged builders to add dynamic code to objects that fires when particular events happen. The PR is not yet merged but promises the oft pondered feature of in-game coding without using softcode (and notably also without the security of softcode!). A lot of PRs, especially from one user, dealt with cleanup and adherence … -
Django Framework Nedir?
Web Programçılığı Web sayfalarının giderek yaygınlaşması ve kullanıcı ile etkileşmeye ihtiyacının olması nedeniyle CGI(Common Gateway Interface) ile başladığını söyleyebiliriz. CGI yönetemi ile etkileşimli web sayfaları üretmek oldukça pahalı bir iştir. Çünkü sunucuya CGI programının yapacağı her istek geldiğinde, program çalıştırılır. Dış program derlemeli bir dil ile yazılmış veya yorumlanabilir bir dil ile yazılmış olabilir. Yazılımın... Django Framework Nedir? yazısı ilk önce Python Türkiye üzerinde ortaya çıktı. -
Testing django template tags
In this blog post I will give you simple example how to test your template tags in django using django testing tools. Let's get started! How to test templatetags? Let say that you got this template tag under /templatetags/create_header.py in django: from django import template register = template.Library() @register.inclusion_tag('example.html', takes_context=True) def make_header(context): return { 'header_title': context['title'] } This make_header tag will take the title from context and pass it template tag. Right after that example.html will be rendered. If you want to test if this template tag works you can use this: from django.test import SimpleTestCase from django.template import Context, Template class CreateHeaderTemplateTagTest(SimpleTestCase): def test_rendered(self): context = Context({'title': 'my_title'}) template_to_render = Template( '{% load create_header %}' '{% make_header %}' ) rendered_template = template_to_render.render(context) self.assertInHTML('<h1>my_title</h1>', rendered_template) What is happening here? I setup Context instance with a proper variable that will be taken by templatetag. After that I create Template. I used the same syntax to include templatetags inside your html files - they are templates for Django. Below I render a template with context and check if my templatetag renders correctly. And that's all! I have my templatetag tested. Feel free to comment! Cover image from Unsplash under CC0. -
Testing django template tags
In this blog post I will give you simple example how to test your template tags in django using django testing tools. Let's get started! How to test templatetags? Let say that you got this template tag under /templatetags/create_header.py in django: from django import template register = template.Library …