Django community: Community blog posts RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
Functions in Python Homework
After some delay(please excuse me for it) here are sample solutions of the problems defined in the presentation Functions in Python. Solutions are also available at GitHub. -
Introduction to Django - presentation
This presentation shows the basics of Django - what is inside the framework and explains the Model-View-Template system. One of the most important parts is a diagram how the request is processed and the response is generated. Shows the project and the application structure and the basic elements - Models, URLs dispatcher, Views and Templates. Introduction to django from Ilian Iliev -
Django for Web Prototyping
Or how to use the benefits of Django template system during the PSD to HTML phase There are two main approaches to start designing a new project - Photoshop mock-up or an HTML prototype. The first one is more traditional and well established in the web industry. The second one is more alternative and (maybe)modern. I remember a video of Jason Fried from 37 Signals where he talks about design and creativity. You can see it at http://davegray.nextslide.com/jason-fried-on-design. There he explains how he stays away from the Photoshop in the initial phase to concetrate on the things that you can interact with instead of focusing on design details. I am not planning to argue which is the better method, the important thing here is that sooner or later you get to the point where you have to start the HTML coding. Unfortunately frequently this happens in a pure HTML/CSS environment outside of the Django project and then we waste some extra amount of time to convert it to Django templates. Wouldn't be awesome if you can give the front-end developers something that they can install/run with a simple command and still to allow them to work in the Django environment … -
Python Interview Question and Answers
Update 2: You can also check the follow up post Django interview questions. For the last few weeks I have been interviewing several people for Python/Django developers so I thought that it might be helpful to show the questions I am asking together with the answers. The reason is ... OK, let me tell you a story first. I remember when one of my university professors introduced to us his professor - the one who thought him. It was a really short visit but I still remember one if the things he said. "Ignorance is not bad, the bad thing is when you do no want to learn." So back to the reason - if you have at least taken care to prepare for the interview, look for a standard questions and their answers and learn them this is a good start. Answering these question may not get you the job you are applying for but learning them will give you some valuable knowledge about Python. This post will include the questions that are Python specific and I'll post the Django question separately. How are arguments passed - by reference of by value? The short answer is "neither", actually it … -
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. In addition to enjoying cheese steaks and saying hi to everyone, we can hardly wait to check out the lineup of talks. I asked our staff which talks they’ve earmarked and the verdict is in— there’s too many good talks to cover by a single person. Here’s the talks our team are especially looking forward to: Monday, July 18th 11:00 AM: Building Dynamic Dashboards with Django and D3 1:30 PM: Websockets: Intro to Messaging 2:50 PM: Solving Problems with Django Forms Tuesday, July 19th 2:20 PM: Making the Most out of Code Reviews 2:20 PM: High Availability Django 2:50 PM: Geospatial Analysis with GeoDjango 4:40 PM: Angular 2 and You 5:10 PM: I Didn’t Know QuerySets Could Do That Wednesday, July 20th 11:30 AM: It is Darkest Before Dawn: Alcoholism and Addiction in Tech 3:50 PM: Django and React: Perfect Together Sign up to chat … -
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.