Django community: Community blog posts RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
Django under the hood: how the Django admin works - Ola Sitarska
(One of my summaries of a talk at the 2015 django under the hood conference). Ola Sitarska gave a higher-level talk about the django admin at the recent Cardiff djangocon. She loves the django admin. The django admin hasn't seen any big updates for the last 8 years. It just works and it works well and it is stable. Until django 1.9: there's a new visual skin for the admin. Check it out! (She showed a screenshot later on in the presentation: yes, it looks great!) There are three main parts to the django admin: AdminSite, ModelAdmin, ChangeList. The AdminSite encapsulates an instance of the django admin application. You don't need to do admin.autodiscover() anymore as it is called automatically by Django now that django has a signal for when an app is loaded. For the admin app, it fires off autodiscover(). Autodiscover finds all your models and extracts information about them. Including the admin.py customization/configuration. This ends up as a list of admin URLs. Note: you can have multiple admins with different configurations in case you have a need for two completely differently configured admin sites. Some of the things you can customize on an adminsite are the site_title … -
Django under the hood: Django CMS and the ORM - Iacopo Spalletti
(One of my summaries of a talk at the 2015 django under the hood conference). Iacopo Spalletti is one of the core developers of django CMS. Django CMS was invited to give a talk because it is a big Django application. So it can tell us what's the good, the bad and the ugly about Django from their perspective. Django CMS is a good django citizen. It integrates with other django apps. It provides features to other applications. It glues applications together. It handles unstructured content using "pages". Django is upgradable. It takes some work, but it is doable and not very painful. Similarly django CMS. The basis of django CMS is the Page and a tree hierarchy in which those pages are placed. And of course a template to render it. Django CMS is multilingual at the core, so everything is translatable. A title is a separate Title object. Similarly the actual content. And so on. Django CMS uses a lot of django features. Of the 14k lines, 3k are in the admin and 2k in the models, for instance. And 1k lines in the template tags: that is a lot. It is a central part of django CMS … -
Intensive Django Training with the US Air Force
Last week Audrey Roy Greenfeld and I had the privilege of teaching the US Air Force's 91st Cyberspace Operations Squadron the finer details of how to build, test, and deploy Django web applications. It was a challenging, exciting experience for us as instructors. We've taught numerous Python and Django trainings over the years under Cartwheel Web , but this was the first time we taught as Two Scoops Academy using our new system of instruction. The environment that the 91st Network Warfare Squadron works in is heavily isolated from the internet. What this means is that until technology or information is approved, it is unavailable to the software developers. While they can access Google, Stack Overflow, and Read the Docs, reading the source on GitHub is something they do at home, not on the job. This means they really need to understand their material. Our experience with their team (already competent in other programming tools) was that they were bright, focused, and willing to learn. The class included material on: Python, PEP8, PEP20, object oriented programming Django and related best practices Class-based views, ModelForms, and Forms Using third-party Python/Django components Bootstrapping projects with Cookiecutter and Cookiecutter-Django PostgreSQL, pgAdmin III, and … -
Intensive Django Training with the US Air Force
Last week Audrey Roy Greenfeld and I had the privilege of teaching the US Air Force's 91st Cyberspace Operations Squadron the finer details of how to build, test, and deploy Django web applications. It was a challenging, exciting experience for us as instructors. We've taught numerous Python and Django trainings over the years under Cartwheel Web , but this was the first time we taught as Two Scoops Academy using our new system of instruction. The environment that the 91st Cyberspace Operations Squadron works in is heavily isolated from the internet. What this means is that until technology or information is approved, it is unavailable to the software developers. While they can access Google, Stack Overflow, and Read the Docs, reading the source on GitHub is something they do at home, not on the job. This means they really need to understand their material. Our experience with their team (already competent in other programming tools) was that they were bright, focused, and willing to learn. The class included material on: Python, PEP8, PEP20, object oriented programming Django and related best practices Class-based views, ModelForms, and Forms Using third-party Python/Django components Bootstrapping projects with Cookiecutter and Cookiecutter-Django PostgreSQL, pgAdmin III, and … -
Intensive Django Training with the US Air Force
Last week Audrey Roy Greenfeld and I had the privilege of teaching the US Air Force's 91st Cyberspace Operations Squadron the finer details of how to build, test, and deploy Django web applications. It was a challenging, exciting experience for us as instructors. We've taught numerous Python and Django trainings over the years under Cartwheel Web , but this was the first time we taught as Two Scoops Academy using our new system of instruction. The environment that the 91st Cyberspace Operations Squadron works in is heavily isolated from the internet. What this means is that until technology or information is approved, it is unavailable to the software developers. While they can access Google, Stack Overflow, and Read the Docs, reading the source on GitHub is something they do at home, not on the job. This means they really need to understand their material. Our experience with their team (already competent in other programming tools) was that they were bright, focused, and willing to learn. The class included material on: Python, PEP8, PEP20, object oriented programming Django and related best practices Class-based views, ModelForms, and Forms Using third-party Python/Django components Bootstrapping projects with Cookiecutter and Cookiecutter-Django PostgreSQL, pgAdmin III, and … -
Understanding *args and **kwargs
args and kwargs are hard. They are confusing, and tricky when you are learning python. Even experienced developers can be tripped up by them. Learn how to expand and collapse lists and dictionaries for args and kwargs.Watch Now... -
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. With an established partner network, others interested in adopting or adapting this code can easily find experienced partners who've worked on this project before. The network consists of those of us that worked on the original code base and can provide commercial support. How SmartElect got its start Initially, the building of SmartElect was entirely driven by the Libyan High National Elections Commission’s need for a voter registration alternative. The interim Libyan government and HNEC faced extraordinary challenges in access to elections: challenging terrain, nomadic populations, … -
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. 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. -
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.