Django community: Community blog posts RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
Django Raw Sql Queries
When your model query API don't go well or you want more performance, you can use raw SQL queries in Django. The Django Object Relational Mapper (ORM) helps bridge the gap between the database and our code Performing raw queries. app/model.py class employee(models.Model): name = models.CharField(max_length=20) position = models.CharField(max_length=20) sal = models.IntegerField() Select statment: >>> emp_list=employee.objects.raw('select * FROM blogapp_employee') SQL statement in .raw() return a set of rows from the database if not return rows, error will result. >>> for i in emp_list: ... i.name ... u'ravi' u'john' u'pheebi' Above work same as model_name.objects.all(). MyModel.objects.all() is a very simplyfied ORM. This code will return the entire contents of the database table, same as running: select * from MyModel. Only use .raw() method when you con't do task with which Django QuerySet. Update, Delete With sql(Using cursor) >>>from django.db import connection >>>cursor = connection.cursor() >>>cursor.execute("UPDATE tablename SET field=value, .. WHERE .'condition'. ") >>>cursor.execute("DELETE tablename SET field=value, .. WHERE .'condition'. ") To see What are the sql queries django running: >>> from django.db import connection >>> connection.queries [{'sql': 'SELECT * FROM blog_employee', … -
Django Forms basics explained
Django forms is powerful module to help Django application development in rendering html from model, validate input from httprequest to model specifications. And we can extend the module to suit our exact need. Lets see basics of django forms. Forms: A collection of fields that knows how to validate itself,Form classes are created as subclasses of django.forms.Form Example: Consider a form used to implement add menu item functionality create models.py from django.db import models class Menu(models.Model): title = models.CharField(max_length=100) content = models.CharField(max_length=100) order = models.IntegerField() create forms.py from django import forms class MenuForm(forms.Form): title = forms.CharField(max_length=100) content = forms.CharField(max_length=100) order = models.IntegerField() A form is composed of Field objects. Using a form in a view: django.http import HttpResponse from app.forms import MenuForm def add_menu(request): if request.method == "POST": form = MenuForm(request.POST) if form.is_valid(): title = request.POST.get("title") content = request.POST.get("content") order = Menu.objects.all().count() + 1 menu = Menu.objects.create(title=title,content=content, order=order) return HttpResponse("Menu Successfully created") … -
What We're Clicking - March Link Roundup
We’re starting a new, monthly series on the Caktus blog highlighting the articles and posts shared by Cakti that drew the most attention on Twitter. These roundups will include everything from Django how-tos to explorations of the tech industry, to innovations for social good. This month we’re featuring articles on front-end development, data visualization, open source, and diversity in tech. Python for Geospatial Data Processing An excellent how-to from Carlos de la Torre on satellite images classification and geospatial data processing in Python. Why Do Many Data Scientists Love Using Python Over Ruby? We obviously love Python. But if you’re not convinced, read Harri Srivastav’s post singing the praises of Python for data management, processing, and visualization. Python’s speed, availability of libraries, options for graphics, and its large, active community make it a standout for working with data. Rachel Andrews’ Talks on Modern CSS Layout Our lead front-end developer Calvin Spealman recommends all of Rachel Andrews’ talks on modern CSS layout. She addresses everything from Flexbox to Grid and Box Alignment as well as a handful of other front-end tools. Should Your NGO Go Open Source? Catherine Cheney’s article on the pros and cons of open source for NGOs sparked … -
New white paper: "Shipping faster: Django team improvements"
For the past couple months, we’ve been working on a new white paper, “Shipping Faster: Django Team Improvements”. We examined our existing processes, looked at best practices, and considered what has or hasn’t worked across our dozens of simultaneous projects. Development teams need to deliver projects quickly and on budget, but often run into challenges that are beyond technical prowess. To build apps faster and more sustainably, we take a holistic look at both technical and environmental influences. Here are the four factors we found that lead to sharper app development: Partnering with stakeholders Focusing on business impact Building apps that can grow with needs (clean code!) Keeping your team sharp We go into detail on each of these within the white paper, highlighting common challenges and tips to overcoming them. Download the white paper by clicking the button below. We look forward to hearing what you think! -
Lightweight Django now in Portuguese!
We're proud to report that Lightweight Django (O'Reilly Media) is now available in Portuguese as Django Essencial. The book was written by our technical director Mark Lavin and Caktus alumnus Julia Elman to great reviews. Django Essencial comes just in time for Mark's keynote talk during PyCon Nordeste. Purchase Django Essencial here. -
Lightweight Django now in Portuguese!
We're proud to report that Lightweight Django (O'Reilly Media) is now available in Portuguese as Django Essencial. The book was written by our technical director Mark Lavin and Caktus alumnus Julia Elman to great reviews. Django Essencial comes just in time for Mark's keynote talk during PyCon Nordeste. Purchase Django Essencial here. -
Braintree Integration with Django
Braintree Integration with Django -
Checking that it's all translatable
When building a translated application, it's important to test that all of the text is going to be translated, but difficult to tell until the translation has been done. Until then, even when you switch languages you still see English everywhere. It's not until all the text that's been set up to be translated actually is that you can see the site in the other language, at which point the English messages stick out like a sore thumb. But that's usually very late in the process. How can we catch those errors earlier? One trick that works surprisingly well is to do a "fake" translation. If you can programmatically modify your English text in a recognizable way and pretend that's your actual translation, when you run the site you can see the messages that have not been modified, and know they need to be marked for translation. (I didn't come up with this idea, but saw it used in a previous job to great effect.) Here's how I'm doing this for Django. Process the messages in the application to a .po file: python manage.py makemessages -l en Run the fake translation tool, taking the English .po file as input and … -
A side-by-side Comparison of Django and Moya
The Django code in this post comes from the official Django tutorial. I've tried not to be disingenuous with the comparison, and I'm only going to compare like with like, so I can show code from both frameworks and let you draw your own conclusions. I'll cover the areas where they differ in another post. Models Both Moya and Django use models to map databases on to familiar data structures. In the case of Moya, the mapping is done with SQLAlchemy. Django uses its own ORM. Here's the models.py from the Django tutorial and a Moya version: models.py import datetime from django.db import models from django.utils import timezone class Question(models.Model): question_text = models.CharField(max_length=200) pub_date = models.DateTimeField('date published') def __str__(self): return self.question_text def was_published_recently(self): return self.pub_date >= timezone.now() - datetime.timedelta(days=1) class Choice(models.Model): question = models.ForeignKey(Question, on_delete=models.CASCADE) choice_text = models.CharField(max_length=200) votes = models.IntegerField(default=0) def __str__(self): return self.choice_text models.xml <moya> <model libname="Question" repr="question_text" xmlns="http://moyaproject.com/db"> <string name="question_text" length="200"/> <datetime name="pub_date" label="date published"/> <property name="was_published_recently" expression="pub_date gte .now - 1d"/> </model> <model libname="Choice" repr="choice_text" xmlns="http://moyaproject.com/db"> <foreign-key name="question" model="#Question" backref="choices" owned="yes"/> <string name="choice_text" length="200"/> <integer name="votes" default="0"/> </model> </moya> You may have noticed that there is nothing comparable to Python's import statements in the Moya code. … -
Building a location aware web app with GeoDjango
PostgreSQL has excellent support for geographical data thanks to the PostGIS extension, and Django allows you to take full advantage of it thanks to GeoDjango. In this tutorial, I’ll show you how to use GeoDjango to build a web app that allows users to search for gigs and events near them. Requirements I’ve made the jump to Python 3, and if you haven’t done so yet, I highly recommend it - it’s not hard, and there’s very few modules left that haven’t been ported across. As such, this tutorial assumes you’re using Python 3. You’ll also need to have Git, PostgreSQL and PostGIS installed - I’ll leave the details of doing so up to you as it varies by platform, but you can generally do so easily with a package manager on most Linux distros. On Mac OS X I recommend using Homebrew. If you’re on Windows I think your best bet is probably to use a Vagrant VM. We’ll be using Django 1.9 - if by the time you read this a newer version of Django is out, it’s quite possible that some things may have changed and you’ll need to work around any problems caused. Generally search engines … -
Django search with haystack and whoosh
Whoosh Haystack is a Django plugin to allow text search, while Whoosh is a pure Python search backend and it provides a library of classes and functions for indexing text and then searching the index. It allows you to develop custom search engines for your content. First I installed Whoosh and Haystack using pip. sudo pip install Whoosh sudo pip install django-haystack then in settings.py, i added haystack application,project application HAYSTACK_SITECONF = 'drumcoder.search_sites' HAYSTACK_SEARCH_ENGINE = 'whoosh' HAYSTACK_WHOOSH_PATH = '/home/user/web/drumcoder/index.whoosh' INSTALLED_APPS = ( #... 'haystack', 'blog', #... ) The HAYSTACK_WHOOSH_PATH is the location at which the index files will be stored. search_sites.py Next, inside my Django site, at the same level as settings.py, I created a new search_sites.py: import haystack haystack.autodiscover() This causes haystack to look for search_indexes.py inside each app in the Django site. blog/search_indexes.py This fill maps the Django model object to the search engine. import datetime from haystack.indexes import * from haystack import site from drumcoder.blog.models import BlogEntry class BlogEntryIndex(SearchIndex): text = CharField(document=True, use_template=True) def get_queryset(self): """ This is used when the entire index for model is updated, and … -
Django permissions and Groups
Permissions: Actually permissions are of 2 types: 1.Model level permissions 2.object level permissions If you want to give permissions on all cars, then Model-level is appropriate, but if you want to give permissions on a per-car basis you want Object-level. You may need both, and this isn't a problem as we'll see. For Model permissions, Django will create permissions in the form 'appname.permissionname_modelname' for each model. If you have an app called 'drivers' with the Car model then one permission would be 'drivers.delete_car'. The permissions that Django automatically creates will be create, change, and delete.Read permission is not included in CRUD operation.Django decided to change CRUD's 'update' to 'change' for some reason. To add more permissions to a model, say read permissions, you use the Meta class: class Book( models.Model ): # model stuff here class Meta: permissions = ( ( "read_book", "Can read book" ), ) Permissions is a set of tuples, where the tuple items are the permission as described above and a description of that permission. Finally, to check permissions, you can use has_perm: obj.has_perm( … -
Using Gitlab API, integrating gitlab in django project for authentication and access
This is a simple way to integrate gitlab authentication in your django apps. We can get user verified email id, general information, git lab URL and token from Gitlab API. These Following steps are needed for Gitlab integration: 1. creating git lab app 2. Authenticating user and getting an access token. 3. Get user information, git lab URL using an access token. 1. Creating Gitlab App a. To create an app, Go to your profile and click on applications then create an application on top of a page. Here you can give your application name, redirect URLs then the application will be created. b. Now you can get the client id, secret of an application 2. Authenticating user and getting an access token. a. Here We have to create a GET request for asking user permission. GET "http://{{Your GIT_LAB_DOMAIN }}/oauth/authorize?client_id={{ Your GIT_LAB_APP_ID }} &redirect_uri={{ redirect_uri }}&response_type=code" GIT_LAB_DOMAIN: your git lab domain, GIT_LAB_APP_ID: your application client id, REDIRECT_URI: The URL which … -
Create Cache Bustable Static Files
Dealing with static files like css and javascript and the cache can be annoying, and hard. Fortunately Django provides a way to solve this fairly easily. Learn how to add a hash in your static file names so caching can be easier.Watch Now... -
Technical stuff happening
Hi folks, a bit more technical entry this time. These usually go onto the Evennia mailing list but I thought it would be interesting to put it in the dev-blog for once. So, I'm now halfway through the TODO list issue of the wclient development branch as alluded to in the last post. The wclient branch aims to rework and beef up the web client infrastructure of Evennia.The first steps, which has been done a while was converting the SSH/SSL and IRC input/output protocols to use the new protocol infrastructure (telnet and websockets was done since before). That's just under-the-hood stuff though. Today I finished the changes to the Monitor/TickerHandlers, which may be of more general interest. With the changes to the the way OOB (Out-Of-Band) messages are passing through Evennia (see this mailing list post for more details), the OOBHandler is no more. As discussed there, the handling of incoming data is becoming a lot freer and will be easily expandable to everyone wanting to make for a custom client experience. The idea is thus for Evennia to offer resources for various input commands to make use of, rather than prescribing such functionality in a monolothic way in the … -
Caktus Internship Fuels a Career Re-Launch
What is it like to be an intern at Caktus? I am finishing up as the spring Django intern (though I didn't actually use any Django), so I'll share my experience. What brought me to this point As a long-time stay-at-home mom re-entering the workforce, I was apprehensive about how my lack of recent professional work experience and references would affect my job search. To update my skillset, I completed the intensive 12-week Python course at The Iron Yard. I was hoping to find work in the tech industry I had left behind before having three children. I was fortunate to be offered a 12-week part-time internship at Caktus. On my application and in my interview, I had expressed an interest in exploring HTML and CSS in more depth than I had had a chance to do at The Iron Yard. And that is how I have spent the majority of my time here. My first task was to implement a style guide for the Caktus website. A generic style guide existed but needed to be customized to reflect the Caktus website. What is a style guide? Style guides do several things: Give direction to designers and developers about the … -
Django + Celery & Rabbit - part four
This is the fourth part of Celery and RabbitMQ in Django series. Today I will fix minor bugs and sum up this series. Audio File detail view This is the first bug that I wanted to tackle. Did you remember my last post ? At the end there was a bug show below: The problem was after successful upload django redirect to detail view of uploaded file. And in HTML template of this view, it expects that ac3_file will be there but FFmpeg still is transcoding it. So I came up with solution: 1.First, I added new field to AudioFile model called was_processed to indicate whenever file has been processed: class AudioFile(models.Model): # .... was_processed = models.BooleanField(default=False) By default, this field has value False. 2.Then in my task I added signal handler that ran after every task: from celery.signals import task_postrun @task_postrun.connect def task_executed_handler(sender=None, body=None, **kwargs): audio_file = AudioFile.objects.get(id=kwargs['args'][0]) audio_file.was_processed = True audio_file.save() In this handler id of AudioFile object is taken from kwargs. After retrieving certain file from the database, the flag was_processed is set. 3.Lastly, in my audiofile_detail.html I added this code: {% if object.was_processed %} <ul><a href="{{ object.ac3_file.url}}">Ac3 File</a></ul> <ul><a href="{{ object.ogg_file.url}}">Ogg File</a></ul> <ul><a href="{{ object.wav_file.url}}">Wav File</a><ul> … -
Django + Celery & Rabbit - part four
This is the fourth part of Celery and RabbitMQ in Django series. Today I will fix minor bugs and sum up this series. Audio File detail view This is the first bug that I wanted to tackle. Did you remember my last post ? At the end there was a bug show below: The problem was after successful upload django redirect to detail view of uploaded file. And in HTML template of this view, it expects that ac3_file will be there but FFmpeg still is transcoding it. So I came up with solution: 1.First, I added new field to AudioFile model called was_processed to indicate whenever file has been processed: class AudioFile(models.Model): # .... was_processed = models.BooleanField(default=False) By default, this field has value False. 2.Then in my task I added signal handler that ran after every task: from celery.signals import task_postrun @task_postrun.connect def task_executed_handler(sender=None, body=None, **kwargs): audio_file = AudioFile.objects.get(id=kwargs['args'][0]) audio_file.was_processed = True audio_file.save() In this handler id of AudioFile object is taken from kwargs. After retrieving certain file from the database, the flag was_processed is set. 3.Lastly, in my audiofile_detail.html I added this code: {% if object.was_processed %} <ul><a href="{{ object.ac3_file.url}}">Ac3 File</a></ul> <ul><a href="{{ object.ogg_file.url}}">Ogg File</a></ul> <ul><a href="{{ object.wav_file.url}}">Wav File</a><ul> … -
Django + Celery & Rabbit - part four
Fourth part of series about Django application with Celery and RabbitMQ. -
Django Life – Session 4 (Bangla Screencast)
The audio is in Bangla, you can switch to HD video by toggling the youtube settings. -
Best Python Libraries
Our love for Python is no secret. But with so many modules, tools, and libraries, it can be overwhelming for beginning developers to identify the most useful. Obviously, our favorite framework is Django. But we’re setting aside our undying love for that framework for a moment to offer a list of other helpful Python libraries. From those offering standard solutions to everyday programming problems, to ones that hold a special place in the heart of the dev who created them, these are some of our developer’s favorite tools for Python development. Calvin Spealman coverage.py measures code coverage for Python programs. Tests are important, but too often overlooked. Coverage gives you a way to create a benchmark for how much of a project’s code you’re testing and for improving from there. straight.plugin is the most widely used open source project Calvin has written, so he’s proud to see it pop up in the wild now and then. It provides a type of plugin a developer can create from almost any existing Python module as well as an easy way for outside developers to add functionality and customization to projects with their own plugins. Victor Rocha Requests is an HTTP library for … -
Buildout/setuptools speed improvement
Sometimes buildout can seem to hang almost forever, even though you've set a timeout. It happens when installing the project itself. Buildout effectively calls python setup.py develop on your project. Setuptools then first calls os.walk() and builds a complete list of files. Only then does it read your MANIFEST.in and some setup.py settings to determine which files to exclude and include. If you have lots of files inside your directory, this might take a long time. The key thing here is that it first builds a complete list. This includes that BUILDOUT_DIR/var/something directory with 245.934 files. And the BUILDOUT_DIR/node_modules/ with your full npm-downloaded stack of javascript with 10.000 files. And the BUILDOUT_DIR/var/project/ symlink to some slow windows share. Ouch. A buildout that normally takes half a minute can take two hours this way... The setuptools bug reports: https://bitbucket.org/pypa/setuptools/issues/249/have-a-way-to-ingore-specific-dirs-when https://bitbucket.org/pypa/setuptools/issues/450/egg_info-command-is-very-slow-if-there-are Intermediate solution: place this monkeypatch_setuptools.py next to your setup.py: import os TO_OMIT = ['var', '.git', 'parts', 'bower_components', 'node_modules', 'eggs', 'bin', 'develop-eggs'] orig_os_walk = os.walk def patched_os_walk(path, *args, **kwargs): for (dirpath, dirnames, filenames) in orig_os_walk(path, *args, **kwargs): if '.git' in dirnames: # We're probably in our own root directory. print("MONKEY PATCH: omitting a few directories like var/...") dirnames[:] = list(set(dirnames) - set(TO_OMIT)) … -
Test multiple user accounts without logging out
Have you ever needed to test multiple user accounts in your web app? If you need to check your permissions are working correctly, you might find you are constantly switching users by logging in and logging out. Or you could have an admin user signed in to Chrome, and another user signed in to Firefox. Either way, its a bit of a pain. Here's a simpler solution; define multiple aliases to 127.0.0.1 in your /etc/hosts file. That way you can have one tab open at http://adminuser:8000/ and another open at http://user1:8000/ with two entirely different user accounts. This assumes of course that you have a development server running locally, and that your app is configured to serve to these 'domains'. For example (append this to /etc/hosts): 127.0.0.1 adminuser 127.0.0.1 user1 127.0.0.1 user2 Not sure if this trick is common knowledge. But it literally just occurred to me. -
Django + Celery & Rabbit - part three
This is a third part of Celery and RabbitMQ in Django series. Today I will be building the Celery and RabbitMQ stack. In previous 2 posts: Django + Celery & RabbitMQ part one and Django + Celery & RabbitMQ part two I have made simple application and played with ffmpeg. First: why we need Celery? Imagine that user upload mp3 file to the application and then in form validation the file is transcoded to other formats. The problem is that user will have to wait for the end of a task. So user sends a request. Then he waits for ffmpeg to transcode uploaded file to different format and then sends the response back. At first glance, it may look correct. But imagine that there are big files to transcode or there is a lot of formats to transcode. The user will have to wait a lot of time. To avoid this I will use celery task with rabbitmq broker to provide transcoding in the background. So what is exactly celery? From the docs: Celery is a simple, flexible and reliable distributed system to process vast amounts of messages, while providing operations with the tools required to maintain such a … -
Django + Celery & Rabbit - part three
This is a third part of Celery and RabbitMQ in Django series. Today I will be building the Celery and RabbitMQ stack. In previous 2 posts: Django + Celery & RabbitMQ part one and Django + Celery & RabbitMQ part two I have made simple application and played with ffmpeg. First: why we need Celery? Imagine that user upload mp3 file to the application and then in form validation the file is transcoded to other formats. The problem is that user will have to wait for the end of a task. So user sends a request. Then he waits for ffmpeg to transcode uploaded file to different format and then sends the response back. At first glance, it may look correct. But imagine that there are big files to transcode or there is a lot of formats to transcode. The user will have to wait a lot of time. To avoid this I will use celery task with rabbitmq broker to provide transcoding in the background. So what is exactly celery? From the docs: Celery is a simple, flexible and reliable distributed system to process vast amounts of messages, while providing operations with the tools required to maintain such a …