Django community: Community blog posts RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
Reactify Django
# Reactify Django is coming. ... -
Django development with Docker — Testing, Continuous Integration and Docker Hub
At the end of my last post about Django development with Docker, we saw how to improve our development environment by implementing these features:Accessing our database from outside the containerInstalling different requirements for each environmentUsing different settings for each environmentEnabling server reloading on code changesBut we did not execute or write any test, even more we are not using any kind of Continuous Integration. Let’s see how to implement these features.IntroductionDuring this post, we will address the following points:How can we configure and run our tests inside a Docker container?How can we integrate our application with a Continuous Integration (C.I.) server and implement Continuous Delivery?Is it possible to Upload our images to Docker Hub from our C.I. server?While we configure travis ci as our Continuous Integration server, we will run our tests and upload a new image to Docker Hub.Note: This post will not go deep in travis and docker hub explications, the main focus is in using them for our intentions.Writing & Running testsFirst, enumerate what we need to accomplish in this section:Install a test framework and configure it.Configure an in-memory database for themWrite some tests for our applicationRun our tests in a containerWe’re going to use pytest, a … -
Ansible provision/deploy setup
I never got around to write down the ansible setup I figured out (together with others, of course, at my previous job) for deploying/provisioning django websites. The whole setup (as a cookiecutter template) can be found in https://github.com/reinout/cookiecutter-djangosite-template . The relevant code is in the ansible/ directory. Note: this is a "cookiecutter template" from which you can generate a project so you'll see some {{ }} and {% %}: when you create the actual project these items will be filled in. My goal was to keep the setup simple and safe. With "safe", I mean that you normally cannot accidentally do something wrong. And it was intended for getting one website project onto the server(s). It is not a huge ansible setup to set up the whole infra in one fell swoop. First the inventory files: Yes, multiple: there are two of them. production_inventory and staging_inventory. The safe aspect is that we used to have a single inventory file with [production] and [staging] headings in it. If you wanted to update staging, you had to add --limit staging on the command line. If you forgot that... With two separate files, you never have this problem. Accidents are much less likely … -
GeoDjango and PostgreSQL
I recently had to set up GeoDjango on a site that used PostgreSQL as a database. As the online instructions for doing this were less than totally clear, I decided to write down how I did it. -
Best React Books 2018
List of current React/JavaScript books. -
Django Local 404 Page
Learn how to display and customize a 404 page locally. -
Angular 6|5 Tutorial: Integrating Angular with Django
In the previous Angular 6 tutorial we've seen how to build a CRUD web application with a Django REST framework API back-end. In this tutorial we'll see how we can integrate the Angular 6 front-end with the Django back-end. After creating both the back-end and front-end apps we need to integrate them i.e instead of taking the approach where both applications are completely separated we'll serve the front-end application using a Django view. In development we'll have both Django development server and Angular/Webpack dev server running but for production we'll only need a Django server. To use this approach you need to tweak the Webpack settings of your front-end project, use the webpack-bundle-tracker (from npm) and use the django-webpack-loader package (from PyPI) The webpack-bundle-tracker is a Webpack plugin that generates a stats file containing meta data information about the assets of your front-end application generated by Webpack. We'll start by installing the webpack-bundle-tracker module then update the Webpack configuration file to make use of this plugin. npm install webpack-bundle-tracker --save Next you need to eject the Webpack configuration from the Angular 6 CLI using ng eject If the ejection is successful you'll find a webpack.config.js in the root of your … -
QuerySet Filters on Many-to-many Relations
Django ORM (Object-relational mapping) makes querying the database so intuitive, that at some point you might forget that SQL is being used in the background. This year at the DjangoCon Europe Katie McLaughlin was giving a talk and mentioned one thing that affects the SQL query generated by Django ORM, depending on how you call the QuerySet or manager methods. This particularity is especially relevant when you are creating your QuerySets dynamically. Here it is. When you have a many-to-many relationship, and you try to filter objects by the fields of the related model, every new filter() method of a QuerySet creates a new INNER JOIN clause. I won't discuss whether that's a Django bug or a feature, but these are my observations about it. The Books and Authors Example Let's create an app with books and authors, where each book can be written by multiple authors. # -*- coding: UTF-8 -*-from __future__ import unicode_literalsfrom django.db import modelsfrom django.utils.translation import ugettext_lazy as _from django.utils.encoding import python_2_unicode_compatible@python_2_unicode_compatibleclass Author(models.Model): first_name = models.CharField(_("First name"), max_length=200) last_name = models.CharField(_("Last name"), max_length=200) author_name = models.CharField(_("Author name"), max_length=200) class Meta: verbose_name = _("Author") verbose_name_plural = _("Authors") ordering = ("author_name",) def __str__(self): return self.author_name@python_2_unicode_compatibleclass Book(models.Model): title = … -
Djangocon keynote: the naïve programmer - Daniele Procida
(One of my summaries of a talk at the 2018 European djangocon.) The naïve programmer is not "the bad programmer" or so. He is just not so sophisticated. Naïve programmers are everywhere. Almost all programmers wish they could be better. Whether you're Programming is a craft/art/skill. That's our starting point. Art can be measured against human valuation. In the practical arts/crafts, you can be measured against the world (if your bridge collapses, for instance). Is your craft something you do all the time, like landing a plane? Or are you, as programmer, more in the creative arts: you face the blank canvas all the time (an empty models.py). In this talk, we won't rate along the single axis "worse - better". There are more axes. "Technique - inept", "creative - dull", "judgment - uncritical" and sophistication - naïve. It is the last one that we deal with. What does it mean to be a sophisticated programmer? To be a real master of your craft? They are versatile and powerful. They draw connections. They work with concepts and ideas (sometimes coming from other fields) to think about and to explain the problems they have to solve. The naïve programmer will write, … -
Djangocon: an intro to docker for djangonauts - Lacey Williams Henschel
(One of my summaries of a talk at the 2018 European djangocon.) Docker: Nice: it separates dependencies. It shares your OS (so less weight than a VM). It puts all memmbers on the same page. Everything is defined to the last detail. But: there is a pretty steep learning curve. Docker is like the polyjuice potion from Harry Potter. You mix the potion, add a hair of the other person, and you suddenly look exactly like that other person. The (docker) image is the person you want to turn into. The (docker) container, that is you. The Dockerfile, that is the hair. The DNA that tells exactly what you want it to look like. (She showed how the format looked). docker build actually brews the potion. It builds the image according to the instructions in the Dockerfile. Ok. Which images do I have? Image Revelio!: docker images. Same with continens revelio: docker container ls. From that command, you can grap the ID of your running container. If you want to poke around in that running container, you can do docker exec -it THE_ID bash Stop it? Stupefy! docker stop THE_ID. But that's just pause. If it is Avada kedavra! you … -
Djangocon: survival tricks and tools for remote developers - Alessio Bragadini
(One of my summaries of a talk at the 2018 European djangocon.) He works in a company that has many remote workers. He is one of them. The core question for him: "how do I manage to work remotely in an effective way without much stress". There is a difference between a remote-friendly company and a remote-first company. Remote-friendly is a company that still has an office. But you're allowed to work from home and you don't have strict work hours. Remote-first changes the entire structure/culture. Can agile help? Test driven development. First you make the tests. That's handy for remote teams. It sets strict boundaries where you can take over the work in a way that you do not have when sitting behind the same keyboard. No code ownership. Anybody can work on everything all the time. Shared "visual backlog" (boards and so). But... "agile" also says that teams that work face-to-face are more efficient in conveying information. But note that the agile manifesto is many years old now. Face-to-fase means proximity, but also truthfulness. So: no documents that can mean anything, but truthful conversation. Eh: we are now used to slack, skype, whatsapp. This is 99% of what … -
Djangocon: Graphql in python and django - Patrick Arminio
(One of my summaries of a talk at the 2018 European djangocon.) For APIs, REST is the normal way. But REST is not perfect. You can, for instance, have too many requests. If you request a user (/users/1) and the user has a list of friends, you have to grab the user page of all those friends also. You could make a special endpoint where you get the names of the friends, but can end up with many endpoints (/users-with-friends/1, /users-with-friends-and-images/1). Or with very big responses that contain everything you might need. Graphql was created to solve some of these issues. You have a single /graphql endpoint, which you POST to. You post the data structure that you want to get back. There's the option of adding types. So you're not bound to pre-defined REST responses, but you can tell exactly how much or how few you need and in what form. Almost every graphql instance has introspection enabled. You can discover the API that way, including which data types to expect. In python, you can use the graphene library. From the same authors, there's graphene-django. There is also integration for django REST framework in graphene-django. Quite useful when you … -
Djangocon: banking with django, how not to lose your customers' money - Anssi Kääriänen
(One of my summaries of a talk at the 2018 European djangocon.) He works for a company (holvi.com) that offers business banking services to microentrepreneurs in a couple of countries. Payment accounts (online payments, prepaid business mastercard, etc). Business tools (invoices, online shop, bookkeeping, etc). Technically it is nothing really special. Django, django rest framework, postgres, celery+redis, angular, native mobile apps. It runs on Amazon. Django was a good choice. The ecosystem is big: for anything that you want to do, there seems to be a library. Important for them: django is very reliable. Now: how not to lose your customers' money. Option 1: reliable payments. Option 2: take the losses (and thus reimburse your customer). If you're just starting up, you might have a reliability of 99.9%. With, say, 100 payments per day and 2 messages per payment, that's 1 error case per day. You can handle that by hand just fine. If you grow to 10.000 messages/day and 99.99% reliability, you have 5 cases per day. You now need one or two persons just for handling the error cases. That's not effective. Their system is mostly build around messaging. How do you make messages reliable? The original system … -
Djangocon: want more woman in tech? Start with Django Girls - Sara Heins
(One of my summaries of a talk at the 2018 European djangocon.) She lives in Kansas, USA. Kansas is great for women in tech, there are many initiatives. The one she is most enthousiastic about is django girls. "We are raising our girls to be perfect and we're raising our boys to be brave". If a woman applies for a job, she fulfills 100% of the demands, if a man applies, he fullfills on average 60%... Why does programming make girls brave? You are going to mistakes and you're going to fix them. How do you get more girls to try it? One of the best ways is to organize a django girls workshop. There are some qualifications you don't need. You don't need to be perfect. You only have to be brave :-) You don't need to be a savvy event planner. There's lots of info on github. There are pre-made websites. Lots of resources. You don't need to have a large budget. Qualifications you do need: You need to be able to solve problems. You need to be open to learning new things. You need to want to constantly improve. You'll be busy with it for half a … -
Djangocon: don't look back in anger, the failure of 1858 - Lilly Ryan
(One of my summaries of a talk at the 2018 European djangocon.) Full title of the talk: "don't look back in anger: Wildman Whitehouse and the great failure of 1858". Lilly is either hacking on something or working on something history-related. "Life can only be understood backwards; but it must be lived forwards -- Søren Kierkegaard" So if you make mistakes, you must learn from it. And if something goes right, you can party. The telegraph! Old technology. Often used with morse code. Much faster than via the post. Brittain and the USA had nation-wide telegraph systems by 1858. So when the first trans-atlantic cable was completed, it was a good reason for a huge celebration. Cannon, fireworks. They celebrated for three weeks. Until the cable went completely dead... Normally you have a "soft lauch". You try out if everything really works for a while. But they didn't do this in case. They could only imagine succes... Failures aren't necessarily bad. You can learn a lot from it. But at that time you didn't have agile retrospectives. An agile retrospective at that time was probably looking over your shoulder while you sprinted along the highway, chased by highway robbers... Now … -
Djangocon: an ode to OAuth - Akos Hochrein
(One of my summaries of a talk at the 2018 European djangocon.) A show of hands. Who use a password manager? 90% of the hands went up. Who uses at least two? Some 40%. Passwords are irritating. There have been initiatives to "outsource" passwords. Openid, oauth, oauth2.0. On to OAuth2.0. It started at twitter. They started with OpenID, but that only handled login, not access to resources. In the end, oauth2.0 came out. (Note: he said "openid connect", but that's build on oauth2.0, so he must have meant plain "openid" if I'm correct. But it might mean that I'm not totally correct in this summary, or I heard it incorrectly). There are multiple ways to work with oauth2.0. He showed the "authorisation code grant". I can't visualize his diagram here, look at the video for that. There are some terms: Resource owner. Client: you, behind your browser. Authentication server: this is where you will log in ("log in with facebook/google/etc") and where the Resource server: this is where the data is. Akos works at prezi. The backend is actually a django site. But there were many customizations to auth, sessions and user objects. At one point, they wanted to make … -
Djangocon: it's about time - Russell Keith-Magee
(One of my summaries of a talk at the 2018 European djangocon.) Handling time and timezones is complex and painful. It starts with leap years. Every four years, there is a leap year. Except every 100 years. Except except every 400 years. The latter two rules are from the gregorian calendar, which replaced the julian calander in 1582 (at least, in parts of the world...) When did the "october revolution" happen? Well, either 25 oct or 7 november 1918. Russia still had the Julian calender at that time :-) Year? Well, some countries use years based on the lunar cycle... Or they count from a different starting point. In IT we had the Y2k problem. In 20 years time there'll be the 32 bit epoch overflow. It already crashed the AOL mail servers (!) years ago. In python, there's the time module. It represents how your computer thinks about time. It isn't terribly useful if you actually want to do something with it that you're going to show to the user. The datetime module is the one you'll probably want to use. But do you use a date or a datetime? A date doesn't have a timezone. The day Hawkings … -
Djangocon: friday lightning talks
(One of my summaries of a talk at the 2018 European djangocon.) The stenographers - Sheryll and Andrew The stenographers are the ones that provide LIVE speech to text subtitles for the talks. Wow. They use "shorthand machines" for steno. It is like a piano where you press multiple keys to form words. On wednesday the speakers talked 46000 words... How fast do people talk? Anything between 180 and 250, though 300 also occurs. The handiest are speakers that speak in a regular tempo. And not too fast. They ask presenters for notes and texts beforehand. That helps the software pick the right words. Pytest-picked - Ana Paula Gomes Say you have a codebase that has tests that take a long time. You've just changed a few files and don't want to run the full test before the commit. You could run git status and figure out the files to test. But you can do it automatically with pytest-picked. It is a small plugin, but she wants to improve it with better guessing and also with support for testing what changed on a branch. How to build a treehouse - Harry Biddle A talk about building actual treehouses! One of … -
Djangocon keynote: 23 years without a 'proper job' - Rachel Willmer
(One of my summaries of a talk at the 2018 european djangocon.) Rachel has used django since it was created, but this is her very first djangocon. She hasn't had a "normal" salaried job for 23 years. She's been freelancing, self employed, some contracting to the Scottish government, had her own company, etc. So here are some tips for those that think about such a life. We as programmers are very lucky. We're very flexible. We can work anywhere we like at anytime we like. As a bus driver, you have to show up when the bus timetable says.... We have lots of freedom, we 'only' have to arrange it so that who we work for agrees to it. She has a side project, https://luzme.com, for searching for low ebook prices. Side project? Not really. She uses it to try out new techniques. Firebase, django channels, etc. Fun! And it helps her learn a lot more than when she would have just followed tutorials. Ok, back to you. You want to do something for yourself. Perhaps your own company? If that is what you want, you have to learn how to run a company. You have to learn how to … -
Djangocon: organizing conferences for learners, how we did it in Namibia - Jessica Upani
(One of my summaries of a talk at the 2018 european djangocon.) For Jessica, it all began at PythonNamibia2015. She went there, not because she wanted to learn python, but because she was bored. And the conference was free. It had all changed by the end of the conference! Thanks to the organizers that inspired a lot of people there to become active with python. In 2017, she helped organize a 'computer day'. Talks and panel discussions, poster presentations, software project presentations and workshops. It was aimed at kids! Especially the panel discussions were aimed at the newcomers: trying to transfer experience. In 2018, there were separate workshop days, amongst other an introductory python course. There are some differences from organizing a conference for adults: You need to write letters to parents! Convincing them to send their kids to the conference. Behaviour. You need to follow the behaviour of the kids. They're less well-behaved than a room of adults. Adults sit still, kids move around and look for attention. The one giving the presentation needs to be a good teacher, otherwise they won't be able to keep the kids' attention. Different expectations. Kids expect fun and games, they don't expect … -
Djangocon: ORM, the sequel - Katie McLaughlin
(One of my summaries of a talk at the 2018 european djangocon.) SQL. The "language to talk to databases in a structured way". The ORM. Object Relational Mapper. The magic that makes it all work in django (even though there's no magic in there). The talk is about the experience of experienced programmers that, for the first time, have to dive into a django project. She used http://glasnt-orm.us.aldryn.io/ ("unicodex") as an example. So. There's a missing icon on the sample page. You have to debug/fix that as an experienced-programmer-without-django-experience. You're used to SQL, but not to the django ORM. You get a tip "use the shell". Which shell? "The c shell? bash?". No, they mean manage.py shell. With a bit of _meta and some copy/paste you can get a list of the available models. You could do the same with manage.py dbshell, which would dump you in the sql shell. List the databases and you get the same answer. Pick a promising table and do a select * from unicodex_codepoint. You can do the same in the django shell with: from unicodex.models import Codepoint Codepoint.objects.all() The rest of the presentation was a nice combination of showing what happens in SQL … -
Djangocon: taking channels async - Andrew Godwin
(One of my summaries of a talk at the 2018 european djangocon.) Channels, started in 2015 as "django-onair", had its 1.0 release in 2017. It used twisted, ran on python 2.7, and django runs synchronously. Python 2.7 undermined it. Python 3 has asyncio support, 2.7 has not. Because of that, channels had to be too complex. The design was wrong. Now, there's channels 2.0. It requires python 3.5+. Native asyncio! Much simpler to deploy, also. It was quite a big rewrite: 75% was changed. A big challenge was that django had to become partially asynchronous. The regular django ORM, views, middleware, url routing is still synchronous. Parallel to that, there's channels (ASGI) middleware and so. Two separate worlds. But still, there are a few contact points. Towards the ORM, for instance. So he needed two functions, sync_to_async and async_to_sync, to move between the two worlds. They took two months to write! Synchronous code has to run in threads. The ThreadPoolExecutor does most of the hard work. Both async and sync code are useful. Channels lets you write your code as both. Async is hard, so you don't want to have to be forced to use it. Channels' two functions make … -
Djangocon: growing old gracefully, on being a career programmer - Carlton Gibson
(One of my summaries of a talk at the 2018 european djangocon.) He's a longtime django user. "The web framework for perfectionists": yes, that's him. He has also build lots of backends for IOS apps. For that, he used django REST framework. He got involved on the mailinglist and on stackoverflow. Now he's a core team member of django rest framework. He also started maintaining django-crispyforms and so. And now he's the "django fellow". So far, so good. He has a job. Nothing million-euro-making, but fine. The problem: he's getting older. There was a post on hacker news: "before you're 40, make sure you have a plan B". You might get into problem when searching for django jobs: "you're older, so you're more expensive, so we'll take a younger person". You also might have a family, so moving (especially working abroad) is harder. There's a common path: become a manager. But he won't do that. He's a good programmer, but not a good manager. He wants to stay productive and creative. So what if you just want to keep programming? He has some generic strategies: Look outside the tech bubble. The software tech bubble. "Software is eating the world", so … -
Djangocon: protecting personal data with django (because it's the law) - Will Hardy
(One of my summaries of a talk at the 2018 european djangocon.) Will is a software developer with a law degree. Now that we have the GDPR, his law degree is suddenly very relevant. GDPR takes effect on 25 May 2018. What is the GDPR? It is a law that regulates the use of personal data. You'll probably have had lots of emails from companies telling you that they'll be good with your data and asking whether they're still allowed to use it. He encourages you to read the actual regulation. The first part is quite readable. The actual articles are quite detailed, but only the first 34 are relevant for us. He thinks we have a professional duty to be on top of this. We have to know about it. As programmers, we're in the front line. We might be the ones that can best advise the company on how to comply. We ought to know the details. If you help your company, you're valuable to your company, so... He has three categories in his talk: terms, rights, tasks. Terms Terms in the legal world aren't defined as rigorously as in software standards. "Personal data is any information relating … -
Djangocon: on the look-out for your data - Markus Holtermann
(One of my summaries of a talk at the 2018 european djangocon.) We have piles of data in our databases. We want to search in there. What is "search", actually? "Try to find something by looking or otherwise seeking carefully and thoroughly". On the one hand, there's "try". On the other hand "careful and thorough". So: search is hard. What about searching in django? In a very basic sense, you already search if you do get_object_or_404(Article, pk=12)! And if you don't find it, you return a "404 Not found". Searching text is more complicated. Text is unstructured data. So you'd have something like get_object_or_404(Article, text__icontains=search_parameter). This is not efficient for your database: WHERE text ILIKE `%your search parameter%'. There is an index in postgres that's much more efficient: trigrams. These chop up your text into various pieces of string that are 1, 2 or 3 characters long. See https://www.postgresql.org/docs/current/static/pgtrgm.html It is not included in django, but you can write a custom index for it. The code fitted (readable!) on one slide. When we talk about searching text, we often mean full text search. Word order doesn't matter, for instance. And the exact verb form also doesn't matter. In linguistics there …