Django community: Community blog posts RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
Euler Totient Function
In this post I will explain yet another simple number theoretic function, called the Euler Totient Function. This is a simple function which is used indirectly in many problems in programming contests. The function is denoted as phi(n). phi(n) basically gives the number of integers greater than 1 and lesser than n which are co-prime to the integer n. In other words, it is the cardinality of the set S = { a: 1<a<n , gcd(a,n) = 1 }. To calculate this function, we will use a straight forward brute force approach. With a simple analysis, we can show that the running time of this algorithm is O(sqrt(n)). - Start with an estimate of the totient function as n - Iterate for i from 2 to sqrt(n) - If i | n , we know that all factors of i less than equal to n will have gcd with n as atleast i. Hence, they can never be in the set S. Hence remove them from the result. - While i|n, divide n by i. - After the iteration of i from 2 to sqrt(n), if n is a number greater than 1, remove all its multiples less than n … -
How to Export Your Data as CSV, XLS, or XLSX
There are times, when you need to export the data from your database to different formats. For example, you want to create some diagrams in Office program for a presentation. In this post I will show you how to create admin actions which export selected items as files for a spreadsheet application (like MS Excel, OpenOffice Calc, LibreOffice Calc, Gnumeric, or Numbers). I will cover the mostly used formats: Comma Separated Values (CSV), Binary Excel (XLS), and Office Open XML (XLSX). First of all, have a look at the model we will be dealing with. It's a simple model with title, description, and - of course - the id. # models.pyfrom django.db import modelsclass MyModel(models.Model): title = models.CharField(max_length=100) description = models.TextField(blank=True) In the admininstration options, we'll define three admin actions: export_csv, export_xls, and export_xlsx. # admin.pyfrom django.contrib import adminfrom models import MyModel# ... export functions will go here ...class MyModelAdmin(admin.Admin): actions = [export_csv, export_xls, export_xlsx]admin.site.register(MyModel, MyModelAdmin) Now let's create functions for each of those actions! Comma-Separated Values Format CSV is the most common import and export format for spreadsheets and databases. It's a textual format which one could easily create or parse himself, but there is also a python built-in … -
Imaginary Realities is back
The Imaginariy Realities webzine was the place to go to for MUD game design articles in the late 90's. Last released in 2001, its articles are still worth the read for any game designers today. But guess what - this venerable ezine has now returned! You can find the new issue here.I think this is a good community initiative worthy of support. I contibuted two articles myself (one of which is about Evennia) and would like to thank the staff/editors who took their work very seriously and did sterling work on getting everything in shape.Thanks also to the other authors who penned some very interesting articles. Great job guys!My impressions: KaVir elaborates in A modern interface for a modern MUD on the advantages of not sticking with an outdated UI just for the sake of it. Adding a more accessible presentation is not that hard and won't ruin your game but rather help it. Whereas I have read his argument about this before, this is a good summary to take to heart. Evennia's javascript web client is currently mainly a telnet clone; there's a lot of things we could offer to make it easier for users to offer a more graphical … -
Start Your API - django-rest-framework part 1
In this video we start creating our API by learning what it takes to put data in our database, and retrieve it back doing all of our CRUD operations. django-rest-framework makes it very simple to accomplish with a minimal amount of effort.Watch Now... -
A note on Django 1.5.2 rotating CSRF token and frontend javascript applications
Django 1.5.2 introduced a change named The CSRF token now rotates when a user logs in. This isn't anything problematic for typical web applications where user submits a form and the page reloads. For applications that are based on JavaScript frameworks like ember (or angular) this becomes a problem, as the user is logged in the background with an AJAX request (for example with Facebook JS SDK) and the page in general never reloads. When he logs in the token changes and API (POST) requests (to django-rest-framework for example) will get forbidden as answers from the server. A solution would be to return a new token with server response to the login request and insert it in the place of the old one, which was inserter when Django template was rendered: xhr.setRequestHeader("X-CSRFToken", "{{csrf_token}}"); I wonder if there are any solutions for this already?.hll { background-color: #ffffcc } .c { color: #408080; font-style: italic } /* Comment */ .err { border: 1px solid #FF0000 } /* Error */ .k { color: #008000; font-weight: bold } /* Keyword */ .o { color: #666666 } /* Operator */ .cm { color: #408080; font-style: italic } /* Comment.Multiline */ .cp { color: #BC7A00 } … -
Working Together
Workflows, teams committing conflicting VCS branches, foreign keys being annoying and more feature in this week's Django Diaries. Time for your semi-regular update on how Django's migrations are going! I've implemented two major things since the last blog post: workflow improvements, and ForeignKey change detection. Workflow One of the problems that only started emerging with South some years after it was released was the issue of working with distributed VCS systems - like Git and Mercurial - and how their tendency towards branching impacted migration writing. When South was initially released, the world was mostly a Subversion place, and so the chance of two developers committing conflicting migrations was small. These days, the chance that someone is going to go off in a branch to add new model features while the master branch also gets a model bugfix is quite high. South didn't really deal with the resultant merge very well; it implicitly ordered migrations by filename, and so whoever had the migration which appeared first alphabetically "won"; the other developer would have to apply migrations out-of-order or back out theirs and then apply both. In short, it was a mess. My fix to this problem inside django.db.migrations is simple … -
Adding stacktraces to log messages
I always had this issue with libraries that logs useful stuff but make figuring out how you called said library very hard. For example, Django logs SQL queries but it's hard to figure out what code made the ORM query. This is a huge pain for Ajax requests - django-debug-toolbar can't help you much with those ... well, not yet. Turns out Python's logging module allows custom formatters. How about we write one ? The filter is just a class with a filter(record) method. It appears that filter can be called multiple times so we need to make sure we don't patch the record more than 1 time: class WithStacktrace(object): def filter(self, record): if not hasattr(record, 'stack_patched'): record.msg += " -- Stack: \n" + ''.join(format_stack(f=sys._getframe(1))) record.stack_patched = True return True You probably notice now the stacktrace can get rather long, and it includes useless frames from the logging module. We can fix that by blacklisting frames from logging or whatever we chose to ignore (like the django.db package): import sys from traceback import format_stack class WithStacktrace(object): def __init__(self, skip=(), limit=5): self.skip = [__name__, 'logging'] self.skip.extend(skip) self.limit = limit def filter(self, record): if not hasattr(record, 'stack_patched'): frame = sys._getframe(1) if self.skip: … -
Adding stacktraces to log messages
I always had this issue with libraries that logs useful stuff but make figuring out how you called said library very hard. For example, Django logs SQL queries but it's hard to figure out what code made the ORM query. This is a huge pain for Ajax requests - django-debug-toolbar can't help you much with those ... well, not yet. Turns out Python's logging module allows custom formatters. How about we write one? The filter is just a class with a filter(record) method. It appears that filter can be called multiple times so we need to make sure we don't patch the record more than 1 time: class WithStacktrace(object): def filter(self, record): if not hasattr(record, 'stack_patched'): record.msg += " -- Stack: \n" + ''.join(format_stack(f=sys._getframe(1))) record.stack_patched = True return True You probably notice now the stacktrace can get rather long, and it includes useless frames from the logging module. We can fix that by blacklisting frames from logging or whatever we chose to ignore (like the django.db package): import sys from traceback import format_stack class WithStacktrace(object): def __init__(self, skip=(), limit=5): self.skip = [__name__, 'logging'] self.skip.extend(skip) self.limit = limit def filter(self, record): if not hasattr(record, 'stack_patched'): frame = sys._getframe(1) if self.skip: while … -
How to Store Your Media Files in Amazon S3 Bucket
In this article, I will show you how to use Amazon Simple Storage Service (S3) to store your media files in the cloud. S3 is known and widely used for its scalability, reliability, and relatively cheap price. It is free to join and you only pay the hosting and bandwidth costs as you use it. The service is provided by Amazon.com. S3 tends to be attractive for start-up companies looking to minimize costs.S3 uses a concept of buckets which is like a storage database. Each bucket has its own url. Inside the buckets you have folders and under that you have files. In fact, directories don't actually exist within S3 buckets. The entire file structure is actually just one flat single-level container of files. The illusion of directories is actually created based on having the file names like dirA/dirB/file.If you want to browse the files in a folder-like structure, you can use Transmit FTP client on Mac OS X. It supports S3 services. Amazon browser-based console also has interface for browsing or uploading files.OK. Now let's have a look how to set up a Django project which will use S3 for media files. 1. Create a bucketAt first you will … -
Getting Started Contributing to Django
Recently I had the opportunity to attend the DjangoCon Sprint here in Chicago (see related post). One of my big take-aways from the event was that contributing to Django isn’t as daunting as I previously thought. The process is mostly straightforward and the Django team has gone a long ... -
Django Facebook 5.3
The 5.3 release makes it a lot easier to get started with Django Facebook. Documentation has had a major overhaul, many small bugs have been fixed and setting validation will complain if you make mistakes during the install. Have a look at the new docs. Especially the bit documenting how to use OpenFacebook will be interesting to many people: Getting an OpenFacebook object Making calls The full docs are linked on github. Share and Enjoy: -
Djangosites Open Sourced
I've been promising it for years, but never gotten around to it. Finally, I've pushed the source code for djangosites.org up to Github. -
Djangosites Open Sourced
I've been promising it for years, but never gotten around to it. Finally, I've pushed the source code for djangosites.org up to Github. -
Djangosites Open Sourced
I've been promising it for years, but never gotten around to it. Finally, I've pushed the source code for djangosites.org up to Github. -
Djangosites Open Sourced
Back in 2008 I started djangosites.org as a listing of websites powered by Django. Prior to that, we relied on a wiki page to see who was using Django, so an image-based website felt like a big improvement. Since day one I've promised to release the source code that I use for the site. It's relatively simple, so I never stressed much about making it a high priority - but I continue to be asked and politely berated for not getting it published. Today that's changed. I think it's too late for me to say I've come good on my promise, but the Djangosites source code is now available on GitHub. The README has more details, but in short this is a dump of the code currently running the site. I'll continue to use this repository as changes are made to the live site, however I'm not actively working on djangosites at this point in time (other than reviewing & approving submissions) There's a few pieces of this that might be useful for people new to Django, but otherwise this is really a collection of generic views. The useful bits might be: Full-text searching (and helper SQL) with PostgreSQL Generating … -
Djangosites Open Sourced
Back in 2008 I started djangosites.org as a listing of websites powered by Django. Prior to that, we relied on a wiki page to see who was using Django, so an image-based website felt like a big improvement. Since day one I've promised to release the source code that … -
Python Indie Bundle Cyber Monday Sale
I've joined forces with Matt Harrison and Audrey Roy to put together the first ever Python Indie Bundle sale! This bundle includes the following e-book bundles (PDF, Kindle, ePub) for just $24.95: Treading on Python Volume 1: Foundations Treading on Python Volume 2: Intermediate Two Scoops of Django: Best Practices for Django 1.5 Bought individually, these three books would cost you $36.95, but in honor of Cyber Monday we've chopped off about 33%, bringing you the awesome price of just $24.95! We're just indie authors who work without a publisher. We pay all the bills, do all the marketing, and make it or break it on our own. It's a lot of work, but we think the total control we have over our work speaks volumes for the quality of result. If you've been to my blog before, you probably already know about Two Scoops of Django that I co-wrote with Audrey Roy. However, you may not know about Matt Harrison's books, so I'm going to share some thoughts: Matt has a talent for explaining things related to Python. If you are still new to Python, his books will amplify your skills and get you to the next level. If … -
Python Indie Bundle Cyber Monday Sale
I've joined forces with Matt Harrison and Audrey Roy to put together the first ever Python Indie Bundle sale! This bundle includes the following e-book bundles (PDF, Kindle, ePub) for just $24.95: Treading on Python Volume 1: Foundations Treading on Python Volume 2: Intermediate Two Scoops of Django: Best Practices for Django 1.5 Bought individually, these three books would cost you $36.95, but in honor of Cyber Monday we've chopped off about 33%, bringing you the awesome price of just $24.95! We're just indie authors who work without a publisher. We pay all the bills, do all the marketing, and make it or break it on our own. It's a lot of work, but we think the total control we have over our work speaks volumes for the quality of result. If you've been to my blog before, you probably already know about Two Scoops of Django that I co-wrote with Audrey Roy. However, you may not know about Matt Harrison's books, so I'm going to share some thoughts: Matt has a talent for explaining things related to Python. If you are still new to Python, his books will amplify your skills and get you to the next level. If … -
Bagels
It’s impossible to get good bagels further west than about New Jersey, so I make my own. My recipe’s adapted from Baking Illustrated, Alton Brown’s pretzel recipe (yes pretzels, see below), and some techniques I learned at Wheatfields. There’s a few keys to making everything turn out right: Use the highest protein flour you can find. King Arthur Bread Flour is the best commonly available one you can find (at about 12-13% protein). -
To my friends in the Node community
Dear friends in the Node community, I’m excited and inspired by the work that you’re doing. Being new is exciting: you get to re-invent the world, and shape it to suit your purposes. You’re not bound by the mistakes other environments have accrued over the years. This has paid huge dividends. You’ve come up with a way to make asynchronous programming accessible to a much wider pool of programmers. You’re creating a set of tools that put developer experience first, proving that developer tools don’t have to have terrible user interfaces. -
Out-of-band mergings
As of today the development repository of Evennia, which has been brewing for a few months now, merged into the main repository. This update grew from one experimental feature to a relatively big update in the end. Together with the "many-character-per-player" feature released earlier, this update covers all the stuff I talked about in my Behind the Scenes blog post. Evennia's webserver was moved from Portal to Server. This moves all database-modifying operations into the same process and neatly avoids race conditions when modifying a game world from various interfaces. The OOB (Out Of Band) handler was implemented. This goes together with a protocol for telnet sub-negotiations according to the MSDP specification. The handler allows on-demand reporting whenever database fields update. It also offers regular polling of properties if needed. A user can customize which oob commands are available to the client and write whatever handlers are needed for their particular game. In the future we'll also add support for GMCP, but the lack of a central, official specification is off-putting (if there is a central document besides accounts of how individual games chose to implement GMCP, please let me know). For our own included web client, we'll likely just use … -
Be More Productive with django_extensions
Django provides a lot of really useful things by default, but there are a few things we do over and over again. Learn how django extensions help solve some of these, and enhances your ability to do others with this intro to django-extensions. We will show a few of the many many useful things it can do.Watch Now... -
Welcome to the world, Wish List Granted
I built something. It's called Wish List Granted. It's a mash-up using Amazon.com's Wish List functionality. What you do is you hook up your Amazon wish list onto wishlistgranted.com and pick one item. Then you share that page with friends and familiy and they can then contribute a small amount each. When the full amount is reached, Wish List Granted will purchase the item and send it to you. The Rules page has more details if you're interested. The problem it tries to solve is that you have friends would want something and even if it's a good friend you might be hesitant to spend $50 on a gift to them. I'm sure you can afford it but if you have many friends it gets unpractical. However, spending $5 is another matter. Hopefully Wish List Granted solves that problem. Wish List Granted started as one of those insomnia late-night project. I first wrote a scraper using pyQuery then a couple of Django models and views and then tied it up by integrating Balanced Payments. It was actually working on the first night. Flawed but working start to finish. When it all started, I used Persona to require people to authenticate … -
New Apps & Migrations
This week, it's the exciting story of one man's battle against his idea of automatically writing settings files. One of the early questions I ran into when designing migrations was how to make the onboarding experience for users as easy as possible. All new apps that you made in 1.7 should come with migrations from the start, of course, but we didn't want to automatically convert apps that didn't have migrations - those could be third-party apps or, as I found to my amusement during initial testing, django.contrib apps. Several potential solutions were thought of and discussed between some of the core team and some South users I talk to regularly. The first thought was a new setting - perhaps MIGRATED_APPS - that defined which apps had migrations, but this was a bad idea and required people to opt-in - a definite no-no. The second idea was to automatically exclude django.contrib apps and then have a blacklist setting - UNMIGRATED_APPS. As we're trying not to introduce more settings in Django, this wasn't looking great from the start, and this still needed makemigrations to prompt you for every new app and ask if you wanted migrations - asking every time if … -
Hiring a Python web application developer
My client is looking to hire a new Python developer, initially for an 8 month contract. It's a home working position, we communicate mostly via Skype / email / gtalk etc. Although we do meet up in meatspace from time to time, so ideally a candidate would be in the London / Oxford area. You will be working with your truly. The projects I've been working on are in the server side of web-enabled devices. The web interface is written in Django, so you'll need the usual battery of front-end technologies; HTML, CSS, Javascript etc. We have a Twistd server which communicates with devices in the field, that my client produces. In the middle we have dynamic user interface generation from XML. So there is some genuinely interesting technology there, and more such projects planned. We need someone who is a good problem solver with a general interest in web technologies. There's also the occasionally need work with data at the bits and bytes level, so a working knowledge of C that would be a plus. See the Careers page on wildfoundry.com for the full details.