Django community: RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
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 … -
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.) Mark Lavin, Rebecca Conley, Dmitriy Chukhin, and Ben Phillips built a Taylor Swift song generator that uses her lyrics to create new songs. Not only can you create new songs out of … -
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 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 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 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! We were incredibly lucky to have great partners from the local Django and Python community supporting our work that day, including PyLadies RDU, Durham Women in Tech, Code for Durham, and Code for Raleigh. Coaches donated their time and their weekends and attendees came from all walks of life (even as far as Atlanta, Georgia!) to spend their Saturdays teaching and learning to code. Working in groups of 3 with one coach per group, attendees worked their way through a tutorial covering the development of a blog application and deploying it to Python Anywhere. “It was such a warm and open environment,” remarked Viola, a workshop attendee. “It was very … -
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. Colin, as volunteer co-founder and co-captain of Code for Durham, will be discussing how he and fellow developers transformed over 20 million data points and twelve years of NC police data into a website that can help identify race-based policing practices. His co-presenter will be civil rights lawyer Ian Mance of Southern Coalition for Social Justice, who originated the idea and guided the site’s development. Last year, Ian's work with a local coalition of Durham activists received front-page attention in the New York Times for using a data-driven approach to combat racial profiling. For more about Code for America’s role in the open data and civic technology movement, visit codeforamerica.org. -
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
MathJax.Hub.Config({ TeX: { extensions: ["color.js"] }}); 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. The subject of game AI generally begins with so-called perfect information games. These are turn-based games where the players have no information hidden from each other and there is no element of chance in the game mechanics (such as by rolling dice or drawing cards from a shuffled deck). Tic Tac Toe, Connect 4, Checkers, Reversi, Chess, and Go are all games of this type. Because everything in this type of game is fully determined, a tree can, in theory, be constructed that contains all possible outcomes, and a value assigned corresponding to a win or a loss for one of the players. Finding the best possible play, then, is a matter of doing a search on the tree, with the method of choice at each level alternating between picking the maximum value and picking the minimum value, matching the different players' conflicting goals, as the search proceeds down the tree. This algorithm is called Minimax. The problem with Minimax, … -
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. PyCon 2016 will be held in Portland, OR and PSF knew they wanted to convey the distinctive mood and look of that city with the 2016 website. Working collaboritively with PSF, Designer Trevor Ray drew on everything from the unique architecture of the city’s craftsman-style bungalow houses and the surrounding mountainous landscape to the cool color scheme of the Pacific-Northwest region. The team developed a site that leads the user on a journey. As he or she scrolls, the user is brought further into the city, from the low, rolling, misty, forest-topped mountains on the outskirts, into the very heart of its neighborhoods. Trevor also redesigned the PyCon logo for 2016, giving it a peaked shape, hand-lettered typography (a reference to the thriving craft community of Portland), and a classic, balanced layout. Ultimately, our team and PSF worked together to achieve a site that we hope is welcoming and functional. We’re excited for this year’s PyCon, and we hope the site excites attendees as well. Only 249 … -
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 … -
Utrecht javascript meetup summaries
Normally I attend python/django meetups, but tonight I attended the Utrecht (=middle of the Netherlands) javascript meetup for the second time. I don't have notes of the first one (that was much too visual to make writing something down worthwile), but this time I have. WebRTC datachannel - Roland van Laar Roland van Laar talks about web applications for education and webrtc data channels. Webrtc is p2p (peer to peer) for browsers. You can use it for audio, video. Datachannel is a bit like p2p websockets. Why did he wanted to use it? Well, he writes educational software. In elementary schools in the Netherlands, that often means a teacher and a digiboard. He showed a demo with a website that showed a six-figure code that the teacher then had to type in on his accompagnying phone app. After thus connecting the webbrowser to the phone, he could then steer the webbrowser from the phone. Advancing sheets, and so. The advantage about webrtc and p2p: it is a direct connection between the phone and the browser, without being dependent on the ofttimes-flaky school's internet connection. How do you build up a connection? The one that starts creates an RTCPeerConnection and creates … -
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 remote system, … -
django-semanticui-form
I'm working on a (side)project in Django that uses the awesome Semantic UI CSS framework. This project has some Django forms that are rendered on the server and so I can't let Django render the form HTML or else the CSS framework can't do its magic. The project is called django-semanticui-form and it's a fork from django-bootstrap-form. It doesn't come with the Semantic UI CSS files at all. That's up to you. Semantic UI is available as a big fat bundle (i.e. one big .css file) but generally you just pick the components you want/need. To use it in your Django templates simply, create a django.forms.Form instance and render it like this: {% load semanticui %} <form> {{ myform | semanticui }} </form> The project is very quickly put together. The elements I intend to render seem to work but you might find that certain input elements don't work as nicely. However, if you want to help on the project, it's really easy to write tests and run tests. And Travis and automatic PyPI deployment is all set up so pull requests should be easy. -
django-semanticui-form
I'm working on a (side)project in Django that uses the awesome Semantic UI CSS framework. This project has some Django forms that are rendered on the server and so I can't let Django render the form HTML or else the CSS framework can't do its magic. The project is called django-semanticui-form and it's a fork from django-bootstrap-form. It doesn't come with the Semantic UI CSS files at all. That's up to you. Semantic UI is available as a big fat bundle (i.e. one big .css file) but generally you just pick the components you want/need. To use it in your Django templates simply, create a django.forms.Form instance and render it like this: {% load semanticui %} <form> {{ myform | semanticui }} </form> The project is very quickly put together. The elements I intend to render seem to work but you might find that certain input elements don't work as nicely. However, if you want to help on the project, it's really easy to write tests and run tests. And Travis and automatic PyPI deployment is all set up so pull requests should be easy.