Django community: RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
Need your help
This for all you developers out there who want to make a game with Evennia but are not sure about what game to make or where to start off.We need an example gameOne of the main critiques Evennia get from newbies is the lack of an (optional) full game implementation to use as an example and base to build from. So, Evennia needs a full, BSD-licensed example game. I'm talking "diku-like", something you could in principle hook up and allow players into within minutes of installing Evennia. The Tutorial world we already have is a start but it is more of a solo quest, it's not designed to be a full multiplayer game. Whereas Evennia supports other forms of MU* too, the idea is that the systems from a more "code-heavy" MUD could easily be extracted and adopted to a more freeform-style game whereas the reverse is not generally true. The exact structure of such a game would be up to the person or team taking this on, but it should be making use of Evennia's api and come distributed as a custom game folder (the folder you get with evennia --init). We will set this up as a separate … -
Stanford Social Innovation Review Highlights Caktus' Work in Libya
The Stanford Social Innovation Review recently featured Caktus in “Text the Vote” in Suzie Boss’ “What’s Next: New Approaches to Social Change” column. It describes how our team of developers built the world’s first SMS voter registration system in Libya using RapidSMS. -
Django Model managers and properties
Model: A model is the single, definitive source of data about your data. It contains the essential fields and behaviors of the data you’re storing. Generally, each model maps to a single database table. And an instance of that class represents a particular record in the database table. 1. Each model(database table) is a Python class that subclasses django.db.models.Model. 2. Each attribute of the model represents a database field(column). Example: from django.db import models class User(models.Model): name = models.CharField(max_length=100) email = models.EmailField() def __str__(self): return self.name To create an object, instantiate it using keyword arguments to the model class, then call save() to save it to the database. Assume models are in a file project1/app1/models.py Example: from app1.models import User user = User(name="xxx", email="xxx@xxx.xxx") user.save() To create and save an object in a single step, use the create() method. Example: from app1.models import User user = User.objects.create(name="xxx", email="xxx@xxx.xxx") Managers: A Manager is the interface through which database query operations are provided to Django models.By default, Django adds a Manager with the name "objects" to every Django model class. Example: from django.db.models import Model, Manager class User(Model): … -
Django Model managers and properties
Model: A model is the single, definitive source of data about your data. It contains the essential fields and behaviors of the data you’re storing. Generally, each model maps to a single database table. And an instance of that class represents a particular record in the database table. 1. Each model(database table) is a Python class that subclasses django.db.models.Model. 2. Each attribute of the model represents a database field(column). Example: from django.db import models class User(models.Model): name = models.CharField(max_length=100) email = models.EmailField() def __str__(self): return self.name To create an object, instantiate it using keyword arguments to the model class, then call save() to save it to the database. Assume models are in a file project1/app1/models.py Example: from app1.models import User user = User(name="xxx", email="xxx@xxx.xxx") user.save() To create and save an object in a single step, use the create() method. Example: from app1.models import User user = User.objects.create(name="xxx", email="xxx@xxx.xxx") Managers: A Manager is the interface through which database query operations are provided to Django models. By default, Django adds a Manager with the name "objects" to every Django model class. Example: from django.db.models import Model, Manager class User(Model): … -
Robots Robots Ra Ra Ra!!! (PyCon 2015 Must-See Talk: 6/6)
Part six of six in our PyCon 2015 Must-See Series, a weekly highlight of talks our staff enjoyed at PyCon. I’ve had an interest in robotics since high school, but always thought it would be expensive and time consuming to actually do. Over the past few years, though, I’ve observed the rise of open hardware such as the Arduino and the Raspberry Pi, and modules and kits built on top of them, that make this type of project more affordable and accessible to the casual hobbyist. I was excited by Katherine’s talk because Robot Operating System (ROS) seems to do for the software side what Arduino and such do for the hardware side. -
Why Doesn't Python Have Switch/Case?
Unlike every other programming language I've used before, Python does not have a switch or case statement. To get around this fact, we use dictionary mapping: def numbers_to_strings(argument): switcher = { 0: "zero", 1: "one", 2: "two", } return switcher.get(argument, "nothing") This code is analogous to: function(argument){ switch(argument) { case 0: return "zero"; case 1: return "one"; case 2: return "two"; default: return "nothing"; }; }; While the Python code is often more terse than the standard method of handling cases, I could argue it is more arcane. When I first started Python it felt weird and distracting. Over time it grew on me, the use of a dictionary key being the identifier in a switch becoming more and more habitual. Dictionary Mapping for Functions In Python we can also include functions or lambdas in our dictionary mapping: def zero(): return "zero" def one(): return "one" def numbers_to_functions_to_strings(argument): switcher = { 0: zero, 1: one, 2: lambda: "two", } # Get the function from switcher dictionary func = switcher.get(argument, lambda: "nothing") # Execute the function return func() While the code inside zero() and one are simple, many Python programs use dictionary mappings like this to dispatch complex procedures. Dispatch Methods … -
Why Doesn't Python Have Switch/Case?
Unlike every other programming language I've used before, Python does not have a switch or case statement. To get around this fact, we use dictionary mapping: def numbers_to_strings(argument): switcher = { 0: "zero", 1: "one", 2: "two", } return switcher.get(argument, "nothing") This code is analogous to: function(argument){ switch(argument) { case 0: return "zero"; case 1: return "one"; case 2: return "two"; default: return "nothing"; }; }; While the Python code is often more terse than the standard method of handling cases, I could argue it is more arcane. When I first started Python it felt weird and distracting. Over time it grew on me, the use of a dictionary key being the identifier in a switch becoming more and more habitual. Dictionary Mapping for Functions In Python we can also include functions or lambdas in our dictionary mapping: def zero(): return "zero" def one(): return "one" def numbers_to_functions_to_strings(argument): switcher = { 0: zero, 1: one, 2: lambda: "two", } # Get the function from switcher dictionary func = switcher.get(argument, lambda: "nothing") # Execute the function return func() While the code inside zero() and one are simple, many Python programs use dictionary mappings like this to dispatch complex procedures. Dispatch Methods … -
Why Doesn't Python Have Switch/Case?
Unlike every other programming language I've used before, Python does not have a switch or case statement. To get around this fact, we use dictionary mapping: def numbers_to_strings(argument): switcher = { 0: "zero", 1: "one", 2: "two", } return switcher.get(argument, "nothing") This code is analogous to: function(argument){ switch(argument) { case 0: return "zero"; case 1: return "one"; case 2: return "two"; default: return "nothing"; }; }; While the Python code is often more terse than the standard method of handling cases, I could argue it is more arcane. When I first started Python it felt weird and distracting. Over time it grew on me, the use of a dictionary key being the identifier in a switch becoming more and more habitual. Dictionary Mapping for Functions In Python we can also include functions or lambdas in our dictionary mapping: def zero(): return "zero" def one(): return "one" def numbers_to_functions_to_strings(argument): switcher = { 0: zero, 1: one, 2: lambda: "two", } # Get the function from switcher dictionary func = switcher.get(argument, lambda: "nothing") # Execute the function return func() While the code inside zero() and one are simple, many Python programs use dictionary mappings like this to dispatch complex procedures. Dispatch Methods … -
Deploying a Django application - Part I
Well, first and foremost, this blog just crossed 1000 page views. Sincere thanks to all readers. Despite what the title indicates, this is not the last post. The world of Django is ever changing and there's always new stuff to learn. But this post (or series of posts, haven't decided yet) will show you how to deploy the project you've created to a server and make it battle ready.I was recently drafted by my college to create and deploy an online mentorship platform. We have a mentorship process that involves scheduling meetings with mentors and discussing attendance, marks and also stuff like grievances. The college wants to make it paper free. So here I am. It's a point of pride, because I'm creating and deploying all by myself.Anyway, first off, let's see what all we need to pull this off. Here's the list:Working Django code (duh.)A dedicated computer that will act as a server. Specifications:At least 4 GB RAM (to handle multiple concurrent requests)At least 500 GB Hard Disk space (to store the database and static files)Django installation: same as or one compatible with the one you developed your project inA static IP address that is unique within the network … -
Testing Client-Side Applications with Django Post Mortem
I had the opportunity to give a webcast for O’Reilly Media during which I encountered a presenter’s nightmare: a broken demo. Worse than that it was a test failure in a presentation about testing. Is there any way to salvage such an epic failure? -
All Pre-Orders Have Been Shipped
We shipped all pre-ordered copies of Two Scoops of Django 1.8 on either Thursday, June 4 or Friday, June 5. When Will Mine Arrive? For details on when your book will arrive, check your tracking number, which you should have received via email. Packing the Orders On Wednesday, we received a huge delivery of books to our home. It was supposed to come on Friday, giving us the weekend to package our shipments. But it came two days early. We ended up staying up very late Wednesday and Thursday night to sign and package them. After signing each book, we shrinkwrapped and then packed it very carefully, to minimize the risk of damage in transit. On Saturday, June 7, we saw the first tweet about a book shipped directly from us! Look what the mailman just dropped off. Woohoo! Thanks guys! @pydanny @audreyr @twoscoopspress #python #django pic.twitter.com/9Octqj6x5X — Brett Thompson (@HKstrongside) June 6, 2015 -
All Pre-Orders Have Been Shipped
We shipped all pre-ordered copies of Two Scoops of Django 1.8 on either Thursday, June 4 or Friday, June 5. When Will Mine Arrive? For details on when your book will arrive, check your tracking number, which you should have received via email. Packing the Orders On Wednesday, we received a huge delivery of books to our home. It was supposed to come on Friday, giving us the weekend to package our shipments. But it came two days early. We ended up staying up very late Wednesday and Thursday night to sign and package them. After signing each book, we shrinkwrapped and then packed it very carefully, to minimize the risk of damage in transit. On Saturday, June 7, we saw the first tweet about a book shipped directly from us! Look what the mailman just dropped off. Woohoo! Thanks guys! @pydanny @audreyr @twoscoopspress #python #django pic.twitter.com/9Octqj6x5X — Brett Thompson (@HKstrongside) June 6, 2015 -
All Pre-Orders Have Been Shipped
We shipped all pre-ordered copies of Two Scoops of Django 1.8 on either Thursday, June 4 or Friday, June 5. When Will Mine Arrive? For details on when your book will arrive, check your tracking number, which you should have received via email. Packing the Orders On Wednesday, we received a huge delivery of books to our home. It was supposed to come on Friday, giving us the weekend to package our shipments. But it came two days early. We ended up staying up very late Wednesday and Thursday night to sign and package them. After signing each book, we shrinkwrapped and then packed it very carefully, to minimize the risk of damage in transit. On Saturday, June 7, we saw the first tweet about a book shipped directly from us! Look what the mailman just dropped off. Woohoo! Thanks guys! @pydanny @audreyr @twoscoopspress #python #django pic.twitter.com/9Octqj6x5X — Brett Thompson (@HKstrongside) June 6, 2015 -
PyLadies RDU and Astro Code School Team Up for an Intro to Django Workshop
This past Saturday, Caktus developer Rebecca Conley taught a 4-hour introductory level workshop in Django hosted by PyLadies RDU. PyLadies RDU is the local chapter of an international mentorship group for women who love coding in Python. Their main focus is to empower women to become more active participants and leaders in the Python open-source community. -
Djangocon sprint: zest.releaser 5.0 runs on python 3
Good news! zest.releaser supports python 3.3+ now! Now... what is zest.releaser? zest.releaser takes care of the boring release tasks for you. So: make easy, quick and neat releases of your Python packages. The boring bit? Change the version number (1.2.dev0 -> 1.2 before releasing and 1.2 -> 1.3.dev0 after releasing). Add a new heading in your changelog. Preferrably record the release date in the changelog, too. svn/git/bzr/hg tag your project. Perhaps upload it to pypi. Zest.releaser takes care of this for you! Look at the docs on readthedocs.org for details. The short page on our assumptions about a releasable python project might be a good starting point. Now on to the python 3.3 support. It was on our wish list for quite some time and now Richard Mitchell added it, kindly sponsored by isotoma! We got a big pull request last week and I thought it would be a good idea to try and merge it at the djangocon sprint. Reason? There are people there who can help me with python 3. Pull request 101 was the important one. So if you want to get an idea of what needs to be done on a python package that was first … -
Formatting python log messages
I see three conflicting styles of log formatting in most of the code I come across. Basically: import logging logging = logging.getLogger(__name__) logger.info("%s went %s wrong", 42, 'very') logger.info("{} went {} wrong".format(42, 'very')) logger.info("%s went %s wrong" % (42, 'very')) I looked at the official PEP 282 and at the official docs. With the help of someone's stackoverflow question I think I understand it now. Use the first version of the three examples. So: The actual log message with the old, well-known %s (and %d, %f, etc) string formatting indicators. As many extra arguments as you have %s-thingies in your string. Don't use the second and third example, as both of them format the string before it gets passed to the logger. So even if the log message doesn't need to be actually logged somewhere, the full string gets created. The first example only gets passed a string and some extra arguments and only turns it into a real full string for in your logfile if it actually gets logged. So if you only display WARN level and higher, your DEBUG messages don't need to be calculated. There is no easy way to use the first example with {} instead … -
Zweite Hamburger Python Unconference
Vom 04. bis 06. September 2015 findet die 2. Python Unconference Hamburg im Institut für organische Chemie der Universität Hamburg statt. Erwartet werden weit über 200 Python-User bzw. Developer aus Hamburg und dem Rest der Welt. Von "Greenhorns" bis zu "proven Experts" aus den Bereichen Mathematik, Data Science, System-Administration und DevOps bis hin zu Web-Development und Python-Core-Entwicklung werden alle nur erdenklichen Facetten der Python-Welt vertreten sein. Wie im letzten Jahr findet die Unconference am Samstag und Sonntag im Barcamp-Stil statt, mit Vorträgen und Diskussionen aus allen Bereichen der Python Welt. Neu ist der Freitag: am Freitag gibt es Raum für Sprints, Tutorials und Workshops. Aktuell gibt es noch Early Bird Tickets für 42,- Euro. Mehr Informationen gibt es unter www.pyunconf.de. -
How do you run distributed standups?
I run a distributed team, across nine hours of time zone offset, so staying in sync takes some work. We’ve been fooling around with different methods and cadances for our sync-ups, so as a way of gathering ideas, I did a quick “twitter poll” yesterday: https://twitter.com/jacobian/status/606118584515993600 The results were super interesting! Do distributed teams do daily standups? Yes, overwhelmingly: 42 people said their teams do daily standups. 8 did standups, but less frequently (ranging from every other day, to weekly). -
Djangocon: React.js workshop - Maik Hoepfel
He compares react.js to angular. Angular basically want to take over your whole page. Multiple apps on one page, mixed with some jquery, isn't really possible. You also have to learn a lot of terminology. It is a full stack framework. And angular 2.0 won't have a migration path from angular 1.x... React.js only does one thing and does it well: the view. It doesn't do data fetching, url routing and so. React.js is quite pythonic. You can start adding little bits at a time to your code. You cannot really do anything wrong with it. A core idea is that the html is in one corner. That's the output. And the state is in the other corner. They're strictly seperated, which makes it easier to reason about. It is build by facebook. They take page load time seriously. You can even run react.js partially on the server side ("react native", inside node.js), taking down rendering time, especially on mobile. Facebook is pretty deeply invested in it, so the chances of it disappearing are low. (As opposed to angular: google is barely using it itself). When you start using react.js on a page, you'll have to start thinking about components. … -
Loop Letters | Tracy Osborn
The following is the second installment of our Loop Letters series, an interview with Tracy Osborn. Tracy is the author of Hello Web App, a book to help designers and non-programmers get up and running with Python and Django. Tracy comes from a designer background herself and offers a unique and refreshing take on programming in her writing. We asked Tracy all about getting involved with Django, recent talks she's given, any advice she has, and what she enjoys doing away from a screen. Let's get started! Your startup, Wedding Lovely, has turned into your full time job. What inspired you to get this going? What’s your favorite part about what you do each day? I have an Art degree and I've always loved wedding invitations — they're such beautiful pieces of art. When I first decided to do a startup, I wanted to build a website to help people do the typography for their own invitations. Unfortunately, that turned into a harder problem to solve than I thought; outputting a PDF with perfect typography is no easy task! Instead, I decided to teach myself programming and start with an easier idea: building a simple directory of freelance and small … -
Djangocon: The net is dark and full of terrors - James Bennett
(One of the summaries of a talk at the 2015 Djangocon EU conference). James Bennett worked already with Django for 9 years. And especially: he's been involved with Django's security releases for 8 years now. So this talk is about a history of django and security and how django tries to protect you. And where django screwed up and what you can learn from it. Bottom line: security is hard. There are always things that can be improved, always things that could be more secure. Some history He showed the first ever django security bug. And the last one (number 49). The last one had a proper full release, the first one told you to overwrite one specific file. The first issue played down the problem a bit, the last one didn't. See the full list of all security issues. In 2007, there was still an informal security process. In 2008, django 1.0, django started autoescaping in the templates. Quite controversial with a long discussion, but it was decided it was so important that it was enabled by default. In 2010, django 1.2, modern csrf protection got added. Security by default. Better to be safe out of the box. Next … -
Djangocon: How to write a view - David Winterbottom
(One of the summaries of a talk at the 2015 Djangocon EU conference). David Winterbottom says that on the one hand, views are simple: a request comes in and a response goes out. On the other hand, you have lots of freedom in views, which might lead to problems. A central principle in software architecture is separation of concerns. For instance by encapsulation: providing a clean interface and hiding information from other parts of the system. A well-known design pattern is MVC, model-view-controller. Originally used for desktop application, but also used in most web frameworks. Note that every web framework interpretes it differently. Model: business logic. Domain logic. The data, the rules. View: what the user sees. Controller: steers the whole process. The user interacts with the controller. Django talks about MTV: model, template, view. He'd say that what's the view in MVC is mostly the templates and static files. The Controller in django is then views, middleware, urls.py. What David often notices in django projects is that the views.py is too big. There's data validation in there. Some of the model layer creeps into the view. And so on. You're not encapsulating everything like you could be. Django is … -
Djangocon: Hypothesis, randomised testing for Django - Rae Knowler
(One of the summaries of a talk at the 2015 Djangocon EU conference). Rae Knowler talks about hypothesis. It is a property based testing library inspired by Haskell's Quickcheck. So: it took out a a bunch of good ideas and you don't need to know a lot of scary Haskell :-) The core idea is that it uses randomized data. You tell it to test your login page, for instance, with a name, an age and an email address. It then creates 1000 random names/ages/emails and feeds them to your form. It simplifies examples of failures. What that means: if it finds an error, it tries iterating on it, finding out in the end that perhaps your form doesn't handle a name with a $ in it. It remembers interesting inputs. It works with pytest, unittests, everything. It is just a library. There's django integration: https://pypi.python.org/pypi/hypothesis-django/ . Custom field types are allowed. It can generate child models. And since last sunday it supports fixtures. Rae showed a demo. With random numbers, hypothesis reliably found a DivideByZero bug. Nice! Hypothesis makes discovering this kind of bugs easy. It makes it simple to write tests without having to rigorously figure out the … -
Djangocon: An ageing coder tackles his ageing code - Owen Campbell
(One of the summaries of a talk at the 2015 Djangocon EU conference). Owen Campbell himself is the ageing coder he talks about. He picked the title because he qualified for early bird tickets because of his age :-) His experience started at age 11 with a 1kB synclair ZX81 that you could only program in z80 assembly language. Storage? Cassette tapes. In university he did assembly programming on the 8086 and 86000. And for an extra course he did Fortran, a "high level language" which meant submitting code to the mainframe and getting a print back that you missed a comma. Please re-submit. He showed an impressive list of programming languages he used in all those years. For his own company he needed an accounting system. He re-used an older Lotus project and re-did it in microsoft access. Later he moved it to MSSQL server and a dotnet frontend. All in all it ran well for 15 years. But he got enough of the server license costs and the microsoft development environment. That accounting system is the "old code" from the title. He wanted to convert it to a web app. He started out with Ruby & Rails. He … -
Djangocon: lookups, transforms and expressions - Anssi Kääriäinen
(One of the summaries of a talk at the 2015 Djangocon EU conference). (Anssi Kääriäinen gave a great talk last year at "django under the hood" about the django ORM. Look at that one, too.) Lookups, transforms and expressions are new features in the ORM (part in 1.7, part in 1.8). They are ways to inject custom SQL in a well-structured way. Custom lookups are things like __glob that you can write yourself in addition to the build-in ones like __gte. Custom transforms? __lower, as an example. How you could do this previously was by using .extra(), you could tweak the sql this way. The new approach is more reusable. It can also be chained (somefield__lower__glob)! Another drawback of .extra() is that it quite a hacky part of the django code. The new functionality Lookups. Those are basically "a condition in the query's WHERE clause". Examples in django are lt, gt, gte, contains and so. To create a custom lookup, you subclass models.Lookup. You set the lookup_name and you implement the .as_sql() method. Afterwards you register it for the various Field types for which it is valid. An example (very quickly and incompletely copied over from the presentation): @models.CharField.register_lookup class …