Django community: RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
Testing django admin search_fields
In the django admin you can add search fields. Very handy! You get a search field at the top of your admin page to quickly filter the results. Here's an example: class InvitationAdmin(admin.ModelAdmin): model = models.Invitation list_display = ['email', 'name', 'is_activated', 'activated_on'] search_fields = ['name', 'email'] See the django documentation. There is a mistake I often make: adding a foreignkey field in the search_fields list! Let's take the standard django book/author example. You have a Book and an Author model. Book has a foreignkey to Author. On the admin page that lists the books you want to have a search box. Not only for book titles, but you also want to search on authors' names and get their books. Easy: class BookAdmin(admin.ModelAdmin): model = models.Book list_display = ['title', 'isbn', 'num_pages', 'author'] search_fields = ['title, 'author'] Let's try it out in the admin. Oops: Traceback (most recent call last): ... TypeError: Related Field got invalid lookup: icontains You have to point at a proper texually-searchable field instead of directly to a model (via a ForeignKey field). It is easy to fix: class BookAdmin(admin.ModelAdmin): ... search_fields = ['title, 'author__name'] I've written a very simple test that tests all my ModelAdmin's search_fields: from … -
Open Sourcing SmartElect: Libya's SMS Voter Registration System
We are proud to say that, with the Libyan High National Elections Commission (HNEC) and consultative support from the United Nations Support Mission to Libya, we have open sourced their elections management platform today under a permissive Apache 2.0 license. Open sourcing means other governments and organizations can freely adopt and adapt the elections tools which cover nine functional areas. The tools range from SMS voter registration, the first of its kind, to bulk alerts to voters and call center support software. You can learn more at our brand new SmartElect homepage. This is the cumulation of two years of work, so we’re incredibly excited to share SmartElect with the rest of the world. -
Introducing Flask Editable Site
I'd like to humbly present Flask Editable Site, a template for building a small marketing web site in Flask where all content is live editable. Here's a demo of the app in action. Text and image block editing with Flask Editable Site. The aim of this app is to demonstrate that, with the help of modern JS libraries, and with some well-thought-out server-side snippets, it's now perfectly possible to "bake in" live in-place editing for virtually every content element in a typical brochureware site. This app is not a CMS. On the contrary, think of it as a proof-of-concept alternative to a CMS. An alternative where there's no "admin area", there's no "editing mode", and there's no "preview button". There's only direct manipulation. "Template" means that this is a sample app. It comes with a bunch of models that work out-of-the-box (e.g. text content block, image content block, gallery item, event). However, these are just a starting point: you can and should define your own models when building a real site. Same with the front-end templates: the home page layout and the CSS styles are just examples. -
A simple script to update a DB migration in Django 1.7+
During the development of a Django model on your local machine is it often necessary to refine the most recent migration to cope with updates to the model, without polluting the migrations of the app with a new migration for each local update. South had the update flag for the schemamigration command, but I didn’t find a similar functionality in the Django builtin makemigrations command introduced in Django 1.7. So I put togheter a simple bash script to automate the process. #!/bin/bash if [ -z $1 ]; then echo "Usage: $0 app_label" exit 1 fi app=$1 # this is the name of the app where we want to update the last migration # get the list of known migrations for the app migrations=(`./manage.py showmigrations $app | awk '{print $2}' | tail -2`) if [ ${#migrations[@]} == 1 ]; then # there is just one migration in the list # here we are updating the initial migration previous_migration=zero current_migration=${migrations[0]} # should be 0001_initial else # there is more than one migration in the list # get the previous one to go back to # and the current one to update previous_migration=${migrations[0]} current_migration=${migrations[1]} fi # go back to the previous migration ./manage.py … -
A simple script to update a DB migration in Django 1.7+
During the development of a Django model on your local machine is it often necessary to refine the most recent migration to cope with updates to the model, without polluting the migrations of the app with a new migration for each local update. South had the update flag for the schemamigration command, but I didn’t find a similar functionality in the Django builtin makemigrations command introduced in Django 1.7. So I put togheter a simple bash script to automate the process. https://gist.github.com/baxeico/4b5ceb2eee14fe776c34 Basically the script unapplies the last migration for a given app, update the migration and reapplies it. I find it a little time saver and it feels good to know that the script is doing the right things in the right order. The post A simple script to update a DB migration in Django 1.7+ appeared first on Augusto Destrero - Freelance developer and sysadmin. -
Testing & Packaging
How to ensure that your tests run code that you think they are running, and how to measure your coverage over multiple tox runs (in parallel!). src I used to scoff when I saw Python projects that put their packages into a separate src directory. It seems unnecessary and reminded me of Java. But it also seemed to be dying out. Imagine my surprise when I saw cryptography – one of Python’s most modern projects – adopt a src directory subsequently! That got me thinking. Though I didn’t take action until I got a bug report that my tox and coverage setup works only by accident: my tests didn’t run against the version of my app that got installed into tox’s virtualenvs. They ran against the actual directory1. In general, that’s not a big deal. In the end, it’s the same code. The only likely problems you could miss are packaging issues (although especially those tend to be frustrating to track down). It demonstrated to me though, that there’s likely more that can go wrong than I thought and that isolating the code into a separate – un-importable – directory might be a good idea. To achieve that, you add … -
Identifying Racial Bias in Policing with a Data-driven App
Recently, Caktus co-founder Colin Copeland spoke about the creation of a web app that analyzes North Carolina traffic stop data to identify racial bias during the Code for America 2015 Summit. The website allows both police departments and community members to visualize a dataset of more than 18 million stops statewide. Colin spoke with Ian Mance, the originator of the app idea and staff attorney with the Southern Coalition for Social Justice. Together with fellow community members, Andy Shapiro and Dylan Young, they used Django, an open source web framework, to make policing data more accessible. -
ShipIt Day ReCap: Q4 2015
Members of one team joined forces with local meetup Code for Durham to help with the alpha launch of the School Navigator App. Using publicly available data, the School Navigator, allows users to geolocate nearby Durham schools and view information like performance ratings. The team included Code for Durham co-founder Colin Copeland who upgraded the Django template for the latest version of SALT. Erin Mullaney helped expand a feature denoting different school zones on the map, using Angular for the first time to do so. She also worked on a query change to more closely match the rules of districting on the map’s display. [Victor Rocha] developed various bug fixes, and merged pull requests. In the meantime, David Ray put his new Ionic skills to the test by building a mobile app version of the School Navigator, now available from the Google App store. (David’s starting Ship It Day project was working through an Ionic tutorial to create a Reddit viewing app with pull refresh and infinite scrolling.) -
Illustrations and soaps
I recently made an article presenting the infrastructure and main coding features of Evennia using a series of nifty diagrams.It can hopefully help newcomers get a feeling for the grand structure of an Evennia server/game a little easier. It's published as a feature on Optional Realities and you can find the article here: Evennia Illustrated.I also recently joined MU Soapbox, a forum predominantly discussing MUSH games, to answer some technical questions on Evennia. Unsurprisingly, some (although so far surprisingly few) MUSHers express concern about Evennia's explicit lack of softcode (that is, the ability for players to use a safe in-game language to code their game rather than to use external Python modules). Their argument is sound: They are used to in-game coding as a way to easily express creatitivy and easily garner help from players.Evennia's stance here clash a bit with those opinions: Our philosophy is that our command system is powerful enough to offer players any complexity of build commands they want. The design/coding of the game itself should be done using proper coding IDEs and modern version control tools.There is no denying that compared to a softcode-coded game, a player-level contributor to an Evennia game needs some extra … -
Easy Django Deployments with Ansible
There are multiple ways to get a python web application running in production, including Docker, push-to-deploy PaaS services like Heroku, using one of the many configuration management tools now available (e.g. Puppet, Chef, Salt, and Ansible), or old fashioned manual configuration of a server. The last is a road that too many newcomers to Django and python web applications are led down, as when you are new to a whole language and suite of tools it is difficult and frustrating to learn a whole other category of tools on top of Django just to get your project running somewhere that other people can see it! One thing I like to do when learning a new technology is to build, from scratch, the simplest possible thing that will work using that tech. That might be the simplest possible Django project or the minimum configuration needed to use SAML authentication with Django. So I decided to create the simplest Ansible playbook I could that would take you from a freshly created VM to a running Django site, with as little configuration as possible. This playbook will do the following: Install git, nginx, gunicorn, postgres, and a few other libraries Configure and run … -
Easy Django Deployments with Ansible
There are multiple ways to get a python web application running in production, including Docker, push-to-deploy PaaS services like Heroku, using one of the many configuration management tools now available (e.g. Puppet, Chef, Salt, and Ansible), or old fashioned manual configuration of a server. The last is a road that too many newcomers to Django and python web applications are led down, as when you are new to a whole language and suite of tools it is difficult and frustrating to learn a whole other category of tools on top of Django just to get your project running somewhere that other people can see it! One thing I like to do when learning a new technology is to build, from scratch, the simplest possible thing that will work using that tech. That might be the simplest possible Django project or the minimum configuration needed to use SAML authentication with Django. So I decided to create the simplest Ansible playbook I could that would take you from a freshly created VM to a running Django site, with as little configuration as possible. This playbook will do the following: Install git, nginx, gunicorn, postgres, and a few other libraries Configure and run … -
Django Subdomains to do advanced things
We always struggle to give users customizations even before they log in to the system like abc.micropyramid.com and Django don't know how to handle that out of the box. We can do it by writing simple middleware. Django Middleware has access to request and responses, so, we can get hold on to request and pass it on to Django views for further process. Here we will add a new property to request and can render pages by seeing at the subdomain property. First, write a middleware class and include it in settings.py middlewares section. class SimpleSubdomainMiddleware: def process_request(self, request): host = request.META.get('HTTP_HOST', '') host = host.replace('www.', '').split('.') if len(host) > 2: request.subdomain = ''.join(host[:-2]) else: request.subdomain = None Now you need to add it to settings.py MIDDLEWARE_CLASSES += ( 'path_to_your_file.SimpleSubdomainMiddleware', ) Now edit your host's file to check this working. Add the following to /etc/hosts 127.0.0.1 abc.com You can verify it by running your Django project and open xyz.abc.com in a browser to see it in action. Any view will have access to the subdomain it's serving to and you can get it by request.subdomain. Here is sample view def login_page(request): if get_merchant(request.subdomain): render page of the get_merchant else: render … -
Django Subdomains to do advanced things
We always struggle to give users customizations even before they login to the system like abc.micropyramid.com and django don't know how to handle that out of the box. We can do it by writing simple middleware. Django Middleware have access to request and responses, so, we can get hold on to request and pass it on to django views for further process. Here we will add new property to request and can render pages by seeing at the subdomain property. First write a middleware class and include it in settings.py middlewares section. class SimpleSubdomainMiddleware: def process_request(self, request): host = request.META.get('HTTP_HOST', '') host = host.replace('www.', '').split('.') if len(host) > 2: request.subdomain = ''.join(host[:-2]) else: request.subdomain = None Now you need to add it to settings.py MIDDLEWARE_CLASSES += ( 'path_to_your_file.SimpleSubdomainMiddleware', ) Now edit your hosts file to check this working. Add the following to /etc/hosts 127.0.0.1 abc.com You can verify it by running your djang project and open xyz.abc.com in browser to see it in action. Any view will have access to the subdomain its serving to and you can get it by request.subdomain. Here is sample view def login_page(request): if get_merchant(request.subdomain): render page of the get_merchant else: render generic page or … -
Django Girls Workshop Recap
This past Saturday we had the pleasure of holding a Django Girls RDU workshop at Caktus in downtown Durham, NC. We hosted 22 students and 10 coaches for a free, all-day coding workshop. Aimed at increasing diversity in tech and encouraging women to gain the skills they need to fall in love with coding, Django Girls is a global nonprofit that provides the framework for these workshops. In regular Django Girls style, the day was part party, part coding marathon and every student left having deployed their very own website! -
django-pipeline + django-jinja
Do you have django-jinja in your Django 1.8 project to help you with your Jinja2 integration, and you use django-pipeline for your static assets? If so, you need to tie them together by passing pipeline.templatetags.ext.PipelineExtension "to your Jinja2 environment". But how? Here's how: # in your settings.py from django_jinja.builtins import DEFAULT_EXTENSIONS TEMPLATES = [ { 'BACKEND': 'django_jinja.backend.Jinja2', 'APP_DIRS': True, 'OPTIONS': { 'match_extension': '.jinja', 'context_processors': [ ... ], 'extensions': DEFAULT_EXTENSIONS + [ 'pipeline.templatetags.ext.PipelineExtension', ], } }, ... Now, in your template you simply use the {% stylesheet '...' %} or {% javascript '...' %} tags in your .jinja templates without the {% load pipeline %} stuff. It took me a little while to figure that out so I hope it helps someone else googling around for a solution alike. -
django-pipeline + django-jinja
Do you have django-jinja in your Django 1.8 project to help you with your Jinja2 integration, and you use django-pipeline for your static assets? If so, you need to tie them together by passing pipeline.templatetags.ext.PipelineExtension "to your Jinja2 environment". But how? Here's how: # in your settings.py from django_jinja.builtins import DEFAULT_EXTENSIONS TEMPLATES = [ { 'BACKEND': 'django_jinja.backend.Jinja2', 'APP_DIRS': True, 'OPTIONS': { 'match_extension': '.jinja', 'context_processors': [ ... ], 'extensions': DEFAULT_EXTENSIONS + [ 'pipeline.templatetags.ext.PipelineExtension', ], } }, ... Now, in your template you simply use the {% stylesheet '...' %} or {% javascript '...' %} tags in your .jinja templates without the {% load pipeline %} stuff. It took me a little while to figure that out so I hope it helps someone else googling around for a solution alike. -
Emoting system, or how to chat up a girl
A few days ago I pushed an emoting contribution to Evennia. A "contrib" is an optional plugin system that is not part of core Evennia but is meant to be easily picked up and used in people's own designs.If you are not familiar with what an emoting system does, it is a way to decribe the actions of a character in the game. The simplest form of emote is a single command (like the command dance leading to some canned response, or in the case of a graphical game, a dance animation). This contribution offers a more sophisticated system though, allowing input like the following: emote /me smiles at /cheerful as he sits at her table. "Hello!" he says.Now, this includes /keywords that relate to the objects in the room. So assuming there is a very cheerful girl in the room, this string will come out as Griatch smiles at a very cheerful girl as he sits at her table. "Hello!" he says. But she will actually see only my outward appearance (the short description) since she doesn't know me. So the cheerful girl (let's say her name is Sandra) would for example seeA man in flowing robes smiles at Sandra … -
Colin Copeland to Speak on Police Data and Racial Bias at Code for America Summit
This Thursday, Colin Copeland, CTO and Caktus Group Co-founder, will be co-presenting “Case Study from North Carolina: Identifying Racial Bias in Policing Practices” during the prestigious 2015 Code for America Summit in Oakland, CA. This invite-only event joins technologists, activists, and officials ranging from mayors to White House officials to discuss technology’s role in civic participation. -
Evennia on Podcast.__init__
So a few weeks back I was contacted by the general Python podcast Podcast.__init__. They had stumbled on Evennia and were intrigued about MUD development still going on - and in Python even! As it turned out at least one of the two hosts were an old-school MU* gamer and between the two of them they had plenty of good questions both on multiplayer text games in general and about Evennia technology in particular. You can listen to my accent via one of the links below: Google+: https://plus.google.com/+Podcastinit-the-python-podcast/posts/Vu9dPVjsR9h Show Notes: http://pythonpodcast.com/griatch_evennia.html Patreon Post: https://www.patreon.com/posts/3448526 Twitter Announcement: https://twitter.com/Podcast__init__/status/648703365019529216Podcast.__init__ logo borrowed from their homepage. -
Filter Your List of Objects in the Admin
Getting the data you need in the admin quickly is important. Learn how to filter not only based on fields, but add your own custom filter as well.Watch Now... -
Pushing through a straw
Recently, a user reported a noticeable delay between sending a command in-game to multiple users and the result of that command appearing to everyone. You didn't notice this when testing alone but I could confirm there was almost a second delay sometimes between entering the command and some users seeing the result. A second is very long for stuff like this. Processing time for a single command is usually in the milliseconds. What was going on? Some background Evennia has two components, the Portal and the Server, running as two separate processes. The basic principle is that players connecting to an Evennia instance connects to the Portal side - this is the outward facing part of Evennia. The connection data and any other input and output will be piped from the Portal to the Server and back again.The main reason for this setup is that it allows us to completely reset the Server component (reloading module data in memory is otherwise error-prone or at least very tricky to make generically work in Python) without anyone getting disconnected from the Portal. On the whole it works very well.DebuggingTracing of timings throughout the entire Server<->Portal pipeline quickly led me to rule out … -
Introduction to Monte Carlo Tree Search
For DjangoCon 2015, Jeff Bradberry created an A.I. for our booth game, Ultimate Tic Tac Toe. Reprinted here from jeffbradberry.com is his explanation of the Monte Carlo Tree Search used to build the A.I. -
PyCon 2016: Behind the Design
Having helped to design an award-winning event site for last year’s PyCon in Montreal, we are thrilled to collaborate again with the Python Software Foundation (PSF) on this year’s site for PyCon 2016. -
Automate Django Deployments with fabfile
Fabric is a Python library and command-line tool with the ability to execute commands on a remote server. It is designed to use SSH to execute system administration and deployment tasks on one or more remote machines. Usage To automate administration tasks and deployments. Typical usage involves creating a python module containing one or more functions, then executing them via the fab command-line tool. Installation Fabric requires Python version 2.5 or 2.6. The easiest ways of installing Fabric is via, pip, easy_install: $ pip install fabric (OR) $ sudo easy_install fabric (OR) $ sudo apt-get install fabric Fabric Functions Now, we learn about some of the functions in Fabric. Fabric provides a set of commands in fabric.api module: $ local # To execute a local command. $ run # To execute a remote command on all specific hosts, user-level # permissions. $ sudo # To execute a command on the remote server with sudo (i.e. superuser) # privileges. $ put # To copy the local file to a remote destination. $ get # To download a file from the remote server. $ prompt # Prompt user with text and return the input (like raw_input). $ reboot # To reboot the … -
Django Migrations Doesn't Play Nice with Third-Party Stuffs
I ran into something annoying while working on my Tweetpile project the other day and it just happened to me today on Atlas. Sometimes, removing code can cause explosions with migrations -- even when they've already been run. Example: You've created a new class called MyClass. It subclasses models.Model It makes use of a handy mixin you wrote called MyMixin: class MyClass(MyMixin, models.Model): # stuff here You create a migration for it, run it, commit your code and congratulate yourself on code well done. Months later you come back and realise that the use of MyMixin was a terrible mistake, so you remove it. Now migrations don't work anymore. Here's what happened: Creating a migration that's dependent on non-Django-core stuff to assemble the model (think mixins that add fields, or the use of custom fields etc.) means that migrations has to import those modules to run. This is a problem because every time you run manage.py migrate it loads all migration files into memory, and if those files are importing now-non-existent modules, everything breaks. Solution: It's an ugly one, but so far it's the only option I can figure: manually collapsing the migration stack. Basically you make sure you've run …