Django community: RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
Resurection
Well, as some of you may have seen this blog was on hold for quite a long time. There were multiple reasons mainly my Ph.D. and changing my job but it is back online. So, what is new? As a start this blog is no longer running on wordpress. The reason is that I had some issues with wordpress - the blog was hacked twice due to security holes in wordpress/plugins, it was terribly slow and the code looked like shit. Lots of inline styles and javascript etc. So I made a simple Django based blog that generates static content. Alse we have new design and new domain, the last one much easier to remember ))) Also the comments are now handled by Disquss and the search functinality is provided by Google. The code of the blog, needs some minor cleaning and then it will be released publicly in the next few weeks. Meanwhile you can check my latest post Working with intervals in Python. P.S. I have finally finished my Ph.D. so no more university/reasearch job and hopefully more time for blogging. -
Working with intervals in Python
Brief: Working with intervals in Python is really easy, fast and simple. If you want to learn more just keep reading. Task description: Lets say that the case if the following, you have multiple users and each one of them has achieved different number of points on your website. So you want, to know how many users haven't got any point, how many made between 1 and 50 points, how many between 51 and 100 etc. In addition at 1000 the intervals start increasing by 100 instead of 50. Preparing the intervals: Working with lists in Python is so awesome, so creating the intervals is quite a simple task. intervals = [0] + \ # The zero intervals [x * 50 for x in range(1, 20)] + \ # The 50 intervals [x * 100 for x in range(10, 100)] + \ # The 100 intervals [x * 1000 for x in range(10, 102)] # the 1000 intervals So after running the code above we will have a list with the maximum number of points for each interval. Now it is time to prepare the different buckets that will store the users count. To ease this we are going to … -
Django, pytz, NonExistentTimeError and AmbiguousTimeError
Brief: In one of the project I work on we had to convert some old naive datetime objects to timezone aware ones. Converting naive datetime to timezone aware one is usually a straightforward job. In django you even have a nice utility function for this. For example: import pytz from django.utils import timezone timezone.make_aware(datetime.datetime(2012, 3, 25, 3, 52), timezone=pytz.timezone('Europe/Stockholm')) # returns datetime.datetime(2012, 3, 25, 3, 52, tzinfo=<DstTzInfo 'Europe/Stockholm' CEST+2:00:00 DST>) Problem: You can use this for quite a long time until one day you end up with something like this: timezone.make_aware(datetime.datetime(2012, 3, 25, 2, 52), timezone=pytz.timezone('Europe/Stockholm')) # which leads to Traceback (most recent call last): File "", line 1, in File "/home/ilian/venvs/test/lib/python3.4/site-packages/django/utils/timezone.py", line 358, in make_aware return timezone.localize(value, is_dst=None) File "/home/ilian/venvs/test/lib/python3.4/site-packages/pytz/tzinfo.py", line 327, in localize raise NonExistentTimeError(dt) pytz.exceptions.NonExistentTimeError: 2012-03-25 02:52:00 Or this: timezone.make_aware(datetime.datetime(2012, 10, 28, 2, 52), timezone=pytz.timezone('Europe/Stockholm')) #throws Traceback (most recent call last): File "", line 1, in File "/home/ilian/venvs/test/lib/python3.4/site-packages/django/utils/timezone.py", line 358, in make_aware return timezone.localize(value, is_dst=None) File "/home/ilian/venvs/test/lib/python3.4/site-packages/pytz/tzinfo.py", line 349, in localize raise AmbiguousTimeError(dt) pytz.exceptions.AmbiguousTimeError: 2012-10-28 02:52:00 Explanation: The reason for the first error is that in the real world this datetime does not exists. Due to the DST change on this date the clock jumps from 01:59 … -
PyCon Sweden 2015
In a few words PyCon Sweden 2015 was awesome. Honestly, this was my first Python conference ever but I really hope it won't be the last. Outside the awesome talks and great organisation it was really nice to spend some time with similar minded people and talk about technology, the universe and everything else. I have met some old friends and made some new ones but lets get back to the talk. Unfortunately I was not able to see all of them but here is a brief about those I saw and found really interesting: It all started with Ian Ozsvald and his awesome talk about "Data Science Deployed" (slides / video). The most important point here were: log everything think about data quality, don't use everything just what you need think about turning data into business values start using your data Then Rebecca Meritz talked about "From Explicitness to Convention: A Journey from Django to Rails" (slides / video). Whether the title sounds a bit contradictive this was not the usual Django vs Rails talk. At least to me it was more like a comparison between the two frameworks, showing their differences, weak and strong sides. Whether I am … -
Django interview questions ...
... and some answers Well I haven't conducted any interviews recently but this one has been laying in my drafts for a quite while so it is time to take it out of the dust and finish it. As I have said in Python Interview Question and Answers these are basic questions to establish the basic level of the candidates. Django's request response cycle You should be aware of the way Django handles the incoming requests - the execution of the middlewares, the work of the URL dispatcher and what should the views return. It is not necessary to know everything in the tiniest detail but you should be generally aware of the whole picture. For reference you can check "the live of the request" slide from my Introduction to Django presentation. Middlewares - what they are and how they work Middlewares are one of the most important parts in Django. Not only because they are quite powerfull and useful but also because the lack of knowledge about their work can lead to hours of debugging. From my experience the process_request and process_response hooks are the most frequently used and those are the one I always ask for. You should … -
Service Discovery with Smartstack
A week of innovation First of all I want to thank Lifesum for having another "Innovation Week", it is a great opportunity and I hope that more companies will start following it. In a few words the idea is to allow everyone from the company to freely pick a project or idea that they want to develop and and work on it for one week. The benefits range from just making people happy because of the break of the routines and the opportunity to work on something a bit different, to seeing some pretty amazing prototypes that can be easily implemented in the company product. What is Service Discovery? In summary service discovery is the possibility of the separate services in scalable infrastructure to communicate with each other and to the outside world. In other words - how to route the requests to the corresponding service while providing balanced load on the instances in the pool and monitoring their health. Sounds simple, right? Well, unfortunately service discovery in the real world is not that simple. In my presentation from the Stockholm Python MeetUp I talked a bit more about the complexity of service discovery, the suboptimal solutions and Smartstack - … -
How to Create a Custom Django Middleware
In a nutshell, a Middleware is a regular Python class that hooks into Django’s request/response life cycle. Those classes holds pieces of code that are processed upon every request/response your Django application handles. The Middleware classes doesn’t have to subclass anything and it can live anywhere in your Python path. The only thing Django cares about is the path you register in the project settings MIDDLEWARE_CLASSES. Your Middleware class should define at least one of the following methods: Called during request: process_request(request) process_view(request, view_func, view_args, view_kwargs) Called during response: process_exception(request, exception) (only if the view raised an exception) process_template_response(request, response) (only for template responses) process_response(request, response) How it works? The Middlware classes are called twice during the request/response life cycle. For that reason, the order you define the Middlwares in the MIDDLEWARE_CLASSES configuration is important. Let’s have a look on the built-in Middleware classes the django-admin startproject command sets up: MIDDLEWARE_CLASSES = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ] During the request cycle, the Middleware classes are executed top-down, meaning it will first execute SecurityMiddleware, then SessionMiddleware all the way until XFrameOptionsMiddleware. For each of the Middlewares it will execute the process_request() and process_view() methods. At this … -
Django + Elastic Search + haystack = Powering your website with search functionality – part 1
Search is one of the most important and powerful functionality in web applications these days. It helps users to easily find content, products etc on your website. Without search option, users will have to find their way out to reach the desired content which no one likes to do. Just imagine Amazon without search bar, you will have to navigate through various categories to find out the product, and it may take you forever to find it. Having said that, implementing search into your web app is not difficult thanks to lot of libraries built for this purpose. In this article, we will discuss how to power up your django website with search functionality using haystack and elastic search. Assuming you already have a fair knowledge of django web framework, lets jump on to haystack and elastic search. Elastic Search Elastic search is a highly scalable lucene based search engine. It provides distributed, multitenant-capable full text search with support of schemaless JSON documents. ElasticSearch is able to achieve fast search responses because, instead of searching the text directly, it searches an index instead. It also provides RESTful API and almost any action can be performed using a simple RESTful API … -
How to Create a Custom Django Middleware
In a nutshell, a Middleware is a regular Python class that hooks into Django’s request/response life cycle. Those classes holds pieces of code that are processed upon every request/response your Django application handles. The Middleware classes doesn’t have to subclass anything and it can live anywhere in your Python path. The only thing Django cares about is the path you register in the project settings MIDDLEWARE_CLASSES. Your Middleware class should define at least one of the following methods: Called during request: process_request(request) process_view(request, view_func, view_args, view_kwargs) Called during response: process_exception(request, exception) (only if the view raised an exception) process_template_response(request, response) (only for template responses) process_response(request, response) How it works? The Middlware classes are called twice during the request/response life cycle. For that reason, the order you define the Middlwares in the MIDDLEWARE_CLASSES configuration is important. Let’s have a look on the built-in Middleware classes the django-admin startproject command sets up: MIDDLEWARE_CLASSES = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ] During the request cycle, the Middleware classes are executed top-down, meaning it will first execute SecurityMiddleware, then SessionMiddleware all the way until XFrameOptionsMiddleware. For each of the Middlewares it will execute the process_request() and process_view() methods. At this … -
Faster Django Tests
Speed is a feature. When it comes to automated tests, speed is critical. A faster test suite means that your team is more likely to run the tests often, to enjoy the fast feedback and to therefore write more tests and write them first. The easiest way to implement tests in Django is to write functional tests - i.e. tests that uses files, a database, fixtures, etc. These tests can become "slow". Here are my tricks to speed your test suite. -
Caktus at DjangoCon 2016 - Talks that Have Us Talking
Caktus is headed to Philadelphia for our seventh DjangoCon. We’re Gold Sponsors this year, so definitely come by our booth to chat with our team and grab t-shirts, stickers, and a 40% off coupon for Lightweight Django, co-written by our technical director, Mark Lavin. Also be sure to enter our raffle for an Amazon Echo. -
Django Tips #6 get_or_create
This is a convenience method for looking up an object, giving a set of parameters, creating one if necessary. The trick with the get_or_create method is that it actually returns a tuple of (object, created). The first element is an instance of the model you are trying to retrieve and the second is a boolean flag to tell if the instance was created or not. True means the instance was created by the get_or_create method and False means it was retrieved from the database. Consider a Django model named AppSettings, where you store configurations parameters of your website. obj, created = AppSettings.objects.get_or_create(name='DEFAULT_LANG') obj.value = request.POST.get('DEFAULT_LANG') obj.save() So, what happened here: if this was the first time I was saving a setting named DEFAULT_LANG, the get_or_create would create an instance and persist in the database. If this was the second or third time I was saving this setting it would simply update the existing instance. -
Django: how to create PDF, MS Word and Excel documents from templates
If you’re building a Django system that implies serving users some kind of documents like reports or bills, chances are you want to provide docs in various formats, from PDF to MS Word to HTML. But how to implement this without resorting to numerous templates and packages for each document type? -
Django: how to create PDF, MS Word and Excel documents from templates
If you’re building a Django system that implies serving some kind of documents like reports or bills, chances are you want to provide docs in various formats from PDF to MS Word to HTML. But how to implement this without resorting to numerous templates and packages for each document type? -
collective.recipe.sphinxbuilder buildout recipe works on python 3 now
I wanted to do a few small tweaks on collective.recipe.sphinxbuilder because it failed to install on python 3. I ended up as maintainer :-) It is now at https://github.com/reinout/collective.recipe.sphinxbuilder . The only change needed was to tweak the way the readme was read in the setup.py and do a new release. Since then Thomas Khyn added windows support. The documentation is now on readthedocs: http://collectiverecipesphinxbuilder.readthedocs.io/ . And the package is also tested on travis-ci.org. -
Django application with puppet- part two
I end first post at the moment of pulling code from git. This text is how to setup additional stuff for geodjango application. It's a good practice in python word to have isolated environments per application. In python 3 there is a tool for that in standard library called venv. How to create such virutal enviroment? By invoking similar command in shell: python3 -m venv /opt/geodjango/env As it is the command that is run in the shell, puppet has the special resource to handling these cases: exec. How to use it? It's simple: exec { 'create venv': command => 'python3 -m venv /opt/geodjango/env', path => '/usr/local/bin:/usr/bin:/bin', require => Vcsrepo['/opt/geodjango/geodjango'], } I'm telling puppet to execute command that is in path. I decided that this command will be run only when there are changes in the repo. That's why require argument. Right now I created virutal enviroment. It's time to install python packages that are needed for proper operation of the whole application. I've used so-called requirements.txt. To install packages from that file via puppet I need: exec { 'install requirements': command => '/opt/geodjango/env/bin/pip install --requirement /opt/geodjango/geodjango/requirements.txt', path => '/usr/local/bin:/usr/bin:/bin', require => Exec['create venv'] } I specify here full paths for … -
Django application with puppet- part two
I end first post at the moment of pulling code from git. This text is how to setup additional stuff for geodjango application. It's a good practice in python word to have isolated environments per application. In python 3 there is a tool for that in standard library called venv. How to create such virutal enviroment? By invoking similar command in shell: python3 -m venv /opt/geodjango/env As it is the command that is run in the shell, puppet has the special resource to handling these cases: exec. How to use it? It's simple: exec { 'create venv': command => 'python3 -m venv /opt/geodjango/env', path => '/usr/local/bin:/usr/bin:/bin', require => Vcsrepo['/opt/geodjango/geodjango'], } I'm telling puppet to execute command that is in path. I decided that this command will be run only when there are changes in the repo. That's why require argument. Right now I created virutal enviroment. It's time to install python packages that are needed for proper operation of the whole application. I've used so-called requirements.txt. To install packages from that file via puppet I need: exec { 'install requirements': command => '/opt/geodjango/env/bin/pip install --requirement /opt/geodjango/geodjango/requirements.txt', path => '/usr/local/bin:/usr/bin:/bin', require => Exec['create venv'] } I specify here full paths for … -
Django application with puppet- part two
Second post in series how to provision vagrant box using puppet for geodjango application. -
Django application with puppet- part one
This post is a quick tutorial how to provision geodjango application using puppet. While writing this tutorial I have taken the approach that I start with running code and then refactor this to something better. Firstly what is puppet? From their website : Puppet provides a standard way of delivering and operating software, no matter where it runs. With the Puppet approach, you define what you want your apps and infrastructure to look like using a common easy-to-read language. So it's a tool for automatic deployment. Other choices are: fabric or ansible. I've chosen this tool first because I use it in my work as a tool for automation as well as I was keen to look more how this all works. Puppet is different from other mentioned tools in a way it does deployment: there are two entities: puppet master and a puppet agent. Master is responsible for keeping the configuration how puppet agent should look like. When puppet is run it pulls out information from puppet master and apply to puppet agent. In other words, puppet agent doesn't have information about its configuration directly- it pulls this from puppet master. Other tools have a different approach: to push … -
Django application with puppet- part one
This post is a quick tutorial how to provision geodjango application using puppet. While writing this tutorial I have taken the approach that I start with running code and then refactor this to something better. Firstly what is puppet? From their website : Puppet provides a standard way of delivering and operating software, no matter where it runs. With the Puppet approach, you define what you want your apps and infrastructure to look like using a common easy-to-read language. So it's a tool for automatic deployment. Other choices are: fabric or ansible. I've chosen this tool first because I use it in my work as a tool for automation as well as I was keen to look more how this all works. Puppet is different from other mentioned tools in a way it does deployment: there are two entities: puppet master and a puppet agent. Master is responsible for keeping the configuration how puppet agent should look like. When puppet is run it pulls out information from puppet master and apply to puppet agent. In other words, puppet agent doesn't have information about its configuration directly- it pulls this from puppet master. Other tools have a different approach: to push … -
Django application with puppet- part one
First post in series how to provision vagrant box using puppet for geodjango application. -
The art of sharing nicks and descriptions
In the month or so since the merger of Evennia's development branch and all its web-client updates, we have been in bug-fixing mode as more people use and stress the code. There have been some new features as well though - I thought it could be interesting to those of you not slavishly following the mailing list. Shared web loginWhen you are logged into the website you will now also auto-login to your account in the web client - no need to re-enter the login information! The inverse is also true. You still need to connect to game at least once to create the account, but after that you will stay connected while the browser session lasts.Behind the scenes the shared login uses cookies linked to server-side Django sessions which is a robust and safe way to manage access tokens. Obviously browser sessions are irrelevant to telnet- or ssh connections.Extended Nicks Evennia's nick(name) system is a way to create a personal alias for things in game - both to on-the-fly replacing text you input and for referring to in-game objects. In the old implementation this replacement was simply matched from the beginning of the input - if the string matched, it was … -
Djangorecipe: easy test coverage reports
Code coverage reports help you see which parts of your code are still untested. Yes, it doesn't say anything about the quality of your tests, but at the least it tells you which parts of your code have absolute the worst kind of tests: those that are absent :-) Ned Batchelder's coverage.py is the number one tool in python. Note: I use buildout instead of pip to set up my projects. Reason: I can integrate extra automation that way. One of those extra automation steps is "djangorecipe": a recipe is a buildout plugin. It installs django, amongst others. My previous setup I use nose as a test runner. Easier test discovery was the reason at the time. Python's unittest2 is better at that, so it might be time to re-visit my choice. Especially as I just read that nose isn't really being maintained anymore. A nice thing about nose is that you can have plugins. coverage.py is a standard plugin. And with django-nose you can easily use nose as a test runner instead of the standard django one. So once I put a few nose-related settings in my setup.cfg, coverage was run automatically every time I ran my tests. Including … -
How to Use Django's Built-in Login System
Django comes with a lot of built-in resources for the most common use cases of a Web application. The registration app is a very good example and a good thing about it is that the features can be used out-of-the-box. With the Django registration app you can take advantages of the following features: Login Logout Sign up Password reset In this tutorial we will focus in the Login and Logout features. Getting started Before we start, make sure you have django.contrib.auth in your INSTALLED_APPS and the authentication middleware properly configured in the MIDDLEWARE_CLASSES settings. Both come already configured when you start a new Django project using the command startproject. So if you did not remove the initial configurations you should be all set up. In case you are starting a new project just to follow this tutorial, create a user using the command line just so we can test the login and logout pages. $ python manage.py createsuperuser In the end of this article I will provide the source code of the example with the minimal configuration. Configure the URL routes First import the django.contrib.auth.views module and add a URL route for the login and logout views: from django.conf.urls import … -
Two forms one view in django
This post is a reference for myself how to do a simple thing like rendering two forms in one view using django framework. How will it be working? The idea is very simple. There will be only one view to render both forms. Moreover, only GET method will be implemented to this view so there won't be a possibility to send POST request. Underneath the first main view will be 2 more views responsible only for handling POST request for both of forms. The simple picture presenting this can be seen below: Let's jump into the code. At first, there is main view responsible for rendering forms: class MainView(TemplateView): template_name = 'sample_forms/index.html' def get(self, request, *args, **kwargs): question_form = QuestionForm(self.request.GET or None) answer_form = AnswerForm(self.request.GET or None) context = self.get_context_data(**kwargs) context['answer_form'] = answer_form context['question_form'] = question_form return self.render_to_response(context) This is simple TemplateView which is responsible only for GET request. At first, my setup question and answer form from the request. Right after that I add these forms to context dictionary and render them on sample_forms/index.html. My sample_forms/index.html looks as follows: <h1>Question Form</h1> <form action="{% url 'question' %}" method="post">{% csrf_token %} {{ question_form }} <input type="submit" value="Send Question"> </form> <h1>Answer Form</h1> …