Django community: RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
Doing things with Django models - aka - Django models tutorial
Django abstracts most of the actions you would be doing with the Database. What it doesn't abstracts, and doesn't try to abstract is the Database modelling part. This is a quick tutorial describing to how model your data in Django models.py, and how to access and modify them. Consider a hypothetical HR department, which wants you to build an application to track and manage their processes. They have employees who work for a department, contractors who work for multiple department. Let's see how you you would do that in Django. from django.db import models class Employee(models.Model): name = models.CharField(max_length = 100) department = models.ForeignKey("Department") class Department(models.Model): name = models.CharField(max_length = 100) class EmployeeHistory(models.Model): employee = models.OneToOneField(Employee) date_joined = models.DateField() marital_status = models.BooleanField() class Contactor(models.Model): name = models.CharField(max_length = 100) departments = models.ManyToManyField(Department) Let's see the type of relationship we created here. An Employee has a Many-to-one relationship with Department, (i.e. One department will have many Employee, but one employee will have a single departments.) So Employee has a field department = models.ForeignKey("Department"). See that the ForeignKey field was added on the class which has in many. In database, this creates a FK from in Employee table which refernces Departments. A … -
Rails and Django commands : comparison and conversion
The most commonly used Rails commands and their Django equivalents Rails | Django rails console | manage.py shell rails server | manage.py runserver rake | None rails generate | None rails dbconsole | manage.py dbshell rails app_name | django-admin.py startproject/manage.py startapp rake db:create | manage.py syncdb The salient points to note are, Django has all commands via manage.py, Rails has it broken into rails and rake. Overall there are more Rails+Rake commands available than Django commands There is no one to one mapping between Rails and Django commands. Eg. There are no equivalent to rake doc:* or rake notes in Django. Similarly there is no equivalent to manage.py createsuperuser in rails. References http://docs.djangoproject.com/en/dev/ref/django-admin/ http://guides.rails.info/command_line.html http://github.com/uswaretech/Acts-as-Django/blob/master/commands.markdown -
Django is not flexible
Django is not flexible at all because you can not do simple things like. Using various authentication mechanisms. You can authenticate via Username, emails, Facebook, Twitter or any combination of these. Using different database backends. Use MySQL, PostgreSQL, Non-relational databases, more or a combination. Use different mail sending strategies. Send mail via smtp, queue to database, log to files and more. Different file storage method. Store locally, FTP, Amazon S3 or write your own. Store messages in Sessions, cookies or others. Cache things in Files, DB, Memcache or just fake it. Save sessions in files, DB, Memcache or cookies. Use Jinja, Mako or Gensi as templating library. One of the most lingering criticisms of Django has been that it is not flexible. People have criticised its design as too-tightly coupled, hard to extend, and inflexible. As a result, people have recommended to roll their own using SQLchemy, Jinja or web.py. I think this view is unfounded and wrong. You would be hard pressed to get the choices and flexibility which Django offers. Eg, while development I run with Sqlite, Dummy caching, and emails on console, while on production I switch to PostgreSQL, Memcache, and queued emails. Many apps use this … -
Getting trending Github projects via YQL
Github.com/explore allows you to see the trending Github topics. They use repopular.com for this, which use twitter retweets to find out the popular Github repos. Since neither Repopular, nor Github have a RSS of these trending repos, I wanted to get a list of these. Here is how easy it is with YQL. How we do it Go to YQL console. Give the SQL query to get the data from the webpage. where url="repopular.com" and css="div.pad a" is the magic which select the webpage, and also what DOM elemenst we are interested in. We get this data in JSON format which is munged to get the list of links. This list of links is passed via is_github_project which gets me just the Github projects. And we are done. PS. YQL is amazing. -
Importing wordpress
We now have a way to import wordpress to blogango, and all our old posts are on this blog. Thanks for reading. -
Seven reasons why you should switch to Vim
So you want a better IDE for developing django, huh? Why not give good old vim a try? Use pathogen to maintain your vim plugins (and sanity). Using this, you can clone the repositories listed here to .vim/bundle/ and start using them right away. Also, consider adding your .vimrc and .vim to a repository. Include .vimrc inside .vim and symlink .vim/.vimrc to ~/.vimrc to version control your .vimrc. My vim files can be found here. Also, here's an imgur album demonstrating these plugins in action. 1. Syntax highlighting for django templates Starting from vim 7.1, syntax highlight for django templates works out of the box. (Check your vim version using vim --version or :version within vim) If you are on an older version, use this plugin 2. Django snippets with snipmate SnipMate provides commonly used "snippets". This eliminates a lot of boiler plate code genreally required to start a django project from scratch. Django specific snippets can be found at robhudson's repository Its very easy to write custom snippets in case you need them. 3. On-the-fly error checking with pyflakes This one is not django specific, but it can save a lot of time spent debugging typos or silly mistakes. … -
Real time applications with Django, XMPP and StropheJS
TL;DR: django-pubsub allows you to create Twitter like real-time updates for your models with Admin like ease. Demo at http://chat.agiliq.com/pubsub/ Code at https://github.com/agiliq/django-pubsub Introduction: PubSub is a XMPP extension which allows publishing and subscribing to events. This is useful when you instantly want to notify many clients about something interesting happening on your server. Quoting the authors of PubSub specs: The protocol enables XMPP entities to create nodes (topics) at a pubsub service and publish information at those nodes; an event notification (with or without payload) is then broadcasted to all entities that have subscribed to the node. Pubsub therefore adheres to the classic Observer design pattern and can serve as the foundation for a wide variety of applications, including news feeds, content syndication, rich presence, geolocation, workflow systems, network management systems, and any other application that requires event notifications. To understand this better, think of newspapers as publishers and the people who subscribe to the newspapers as subscribers. Getting your daily newspaper is similar to checking your RSS feeds because you only get information at regular intervals. (Of course you could keep buying newspapers every 'x' hours, i.e. keep refreshing your RSS reader so often). This is as inefficient … -
Behind the Scenes: From HTML Form to Storage
In this post, we are going to see what happens behind the scenes when a file is uploaded to a django powered web application. An HTML form with a file input (atleast one) and encoding set to multipart/form-data is submitted. The MultiPartParser parses the POST request and returns a tuple of the POST and FILES data (request.POST, request.FILES). The MultiPartParser processes the uploaded data using the File Upload Handlers objects (through the new_file, receive_data_chunk and upload_complete methods). The request.FILES values are a sequence of instances of UploadedFile. In the django form, we pass the request.FILES MultiValueDict. These UploadedFile instances are validated by the full_clean method on the Form. The full_clean method in turn calls the _clean_fields method which calls the clean method on the forms.FileField and checks if the data is empty. After the form is successfully validated, we might assign and save the cleaned_data of the form to a model instance (or by saving the ModelForm). When the save method on the model instance is called, the save_base method calls a pre_save method on each field of the model. This pre_save method returns the value of the file instance bound to that FileField and calls it's save method. This … -
Haml for Django developers
Haml is taking the Ruby(and Rails) world by storm. Its not used as heavily by Python(and Django) developers as the Python solutions aren't as mature. I finally gave Haml a try and was pleasantly surprised how easy it was. The most mature Python implementation of Haml is hamlpy, which converts the hamlpy code to Django templates. Others are shpaml and GHRML Lets look at some templates from Django tutorial {% if latest_poll_list %} <ul> {% for poll in latest_poll_list %} <li><a href="/polls/{{ poll.id }}/">{{ poll.question }}</a></li> {% endfor %} </ul> {% endif %} Here is this template converted to Haml -if latest_poll_list %ul -for poll in latest_poll_list %li %a{'href':'/polls/{{ poll.id }}'}= poll.question You can see that this lost a lot of noise from the Django template. Being a Django programmer, significant whitespace is very pleasing. Here is another one from the same tutorial. <h1>{{ poll.question }}</h1> <ul> {% for choice in poll.choice_set.all %} <li>{{ choice.choice }}</li> {% endfor %} </ul> And converted to hamlpy %h1 = poll.question %ul -for choice in poll.choice_set.all %li = choice.choice Again, I lose a lot of noise from the templates, and the signifiant whitespace improve the chance that the out put would be valid html. … -
Behind the Scenes: Request to Response
In the previous installment of "Behind the Scenes", we saw how the control flows from Form to File Storage. Today, we are going to see how the application reacts from request to response. In this post, we are going to assume that we are using django's inbuilt runserver. The flow doesn't change much for other WSGI servers available. When you invoke the runserver management command, the command line options are validated and an instance of WSGIServer is created and passed the WSGIRequestHandler, which is used to create the request object (WSGIRequest). After the request object is created and the request started signal is fired, the response is fetched through the WSGIRequestHandler.get_response(request). In the get_response method of the request handler, first the urlconf location (by default the urls.py) is setup based on the settings.ROOT_URLCONF. Then a RegexURLResolver compiles the regular expressions in the urlconf file. Next, the request middlewares are called in the order specified in the settings.MIDDLEWARE_CLASSES followed by the view middlewares after matching the view (callback) function against the compiled regular expressions from the urlconf. Then the view (callback) is invoked and verified that it does not return None before calling the response middlewares. You can see the pictorial … -
Getting started with South for Django DB migrations
South is a migration tool used with Django.There will be times when you would be adding fields to a model or changing the type of field (eg: Field was an IntegerField and you want to change it to FloatField). In such cases syncdb doesn't help and South comes to your rescue. There were times when i tried "python manage.py migrate southapp", got some error and then tried "python manage.py migrate southapp 0001 --fake". In some cases, that worked. When it did not work, i tried something else and so on. There were confusions regarding what --fake does and why it does so. In this post, i intend to remove(lessen) that confusion. We will be seeing some scenarios which will help us understand south better. At various points, we will intentionally make some mistakes and will rectify them later. Also, we keep looking at our database schema as we go. Scenario 1: Using south on a brand new app. Let's create an app named "farzi_app". python manage.py startapp farzi_app In settings.py, add 'south' to INSTALLED_APPS and run syncdb. python manage.py syncdb Running syncdb creates a table named 'south_migrationhistory' in your database. This table 'south_migrationhistory' keeps a track of all the migrations … -
How and why to use pyflakes to write better Python
Here is another of the screencasts I created for "Getting started with Python" series. Liked them? Let me know what else would you like me to create screencasts about? -
Screencast: Django Tutorial Part 1
Django Screencast Tutorial 1 from Shabda Raaj on Vimeo. I am creating screencasts for Django tutorial and other Django videos. Here is part 1. Liked this? Leave me a comment or email me and tell me what would you like me to create screencasts for. (Watch the screencast in full screen mode.) -
How to use pep8.py to write better Django code
Here is another or screencast I created for my "Getting Started with Django" series. Like this? Email us on what would you like to see. (Watch in fullscreen.) -
Deploying Django apps on Heroku
Read this first: http://devcenter.heroku.com/articles/django. This is a great article by the Heroku. I am just filling in some more details and making this step-by-step. Get your Django project code. Create a virtualenv with a no-site-packages command virtualenv vent --no-site-packages Install Django, psycopg2 (postgres connector), gunicorn and any other required Django libraries. Confirm that you have all the required libraries and you can run your code locally using manage.py runserver. Create a requirement.txt by using pip freeze > requirements.txt Make sure you have a requirements.txt at the root of your repo. Heroku uses this to identify that the app is a Python app. Create a Procfile. Put this entry: web: python mysite/manage.py run_gunicorn -b "0.0.0.0:$PORT" -w 3shabda This is what your directory structure will look like [root-of-heroku-folder] requirements.txt Procfile [Django-project-folder] __init__.py manage.py settings.py [app1] [app2] (If you aren't tracking your files with git yet.) Track your files with git. git init git add . git commit -am "First commit" Make sure you have the heroku toolbelt installed. If not go to http://toolbelt.herokuapp.com/ and install. 11.You should have these commands available now: heroku foreman Authenticate to heroku with heroku auth:login Run this command. heroku create --stack cedar This will create a new … -
Dynamically attaching SITE_ID to Django Caching
It would be useful and convenient, if you have an automatic way to add the SITE_ID, especially, when you have multiple sites running on the same deployment. Django provides a cache prefix function KEY_FUNCTION in settings which can be used to achieve this. Just follow the following steps, and your cache, automatically prepends SITE_ID to the cache key, making it unique across multiple sites. Put the following into the settings file. CACHES = { 'default': { 'BACKEND': 'django.core.cache.backends.db.DatabaseCache', 'LOCATION': 'cache_table', KEY_FUNCTION = ‘projectname.appname.modulename.functionname’, } } Write a function to get current site id, say, get_current_site(), which returns current SITE_ID. Add a function like below, as functionname at the same path as specified in KEY_FUNCTION. from django.utils.encoding import smart_str def prefix_site_id(key, key_prefix, version): site_id = get_current_site() return ':'.join([str(site_id), str(version), smart_str(key)]) That’s it. You have successfully added an automatic, dynamic, function based cache prefix for django. -
Deploy Django App in 5 Easy Steps
So you just bought a new VPS, have installed Ubuntu and want to deploy your django app, GREAT!! We shall get your app, up and running in 5 easy steps, using best(arguably) of tools available. The post is targeted to audience who are new to deployment arena, but assumes you are comfortable with developing basic django apps. We shall be using gunicorn as our server and nginx nginx as our reverse proxy and static hanlder. Here we go: 1. Login and OS Updation: $ ssh root@{your_ip} # apt-get update # apt-get upgrade # apt-get dist-upgrade # dpkg-reconfigure tzdata #choose your time zone 2. Setup Users and Permissions: # useradd agiliq # mkdir /home/agiliq # chown agiliq:agiliq /home/agiliq # passwd agiliq #choose a password # chsh agiliq -s /bin/bash #choose a shell # visudo #lets give sudo access to the user agiliq root ALL=(ALL) ALL agiliq ALL=(ALL) ALL # su agiliq # switch from root to agiliq 3. Install necessary stuff and create a dev environment: $ sudo apt-get install python-pip git mysql-client mysql-server $ python-mysqldb nginx emacs(you might choose vim or stick to default vi/nano) $ pip install virtualenv #please refer to documentation of virtualenv $ virtualenv projhq # creates … -
Deploying Django apps on Heroku
Deploying Django apps on Heroku: Read this first: http://devcenter.heroku.com/articles/django. This is a great article by the Heroku. I am just filling in some more details and making this step-by-step. Get your Django project code. Create a virtualenv with a no-site-packages command :: virtualenv vent --no-site-packages Install Django, psycopg2 (postgres connector), gunicorn and any other required Django libraries. Confirm that you have all the required libraries and you can run your code locally using manage.py runserver. Create a requirement.txt by using :: pip freeze > requirements.txt Make sure you have a requirements.txt at the root of your repo. Heroku uses this to identify that the app is a Python app. Create a Procfile. Put this entry: :: web: python mysite/manage.py run_gunicorn -b "0.0.0.0:$PORT" -w 3shabda This is what your directory structure will look like :: [root-of-heroku-folder] requirements.txt Procfile [Django-project-folder] __init__.py manage.py settings.py [app1] [app2] (If you aren't tracking your files with git yet.) Track your files with git. :: git init git add . git commit -am "First commit" Make sure you have the heroku toolbelt installed. If not go to http://toolbelt.herokuapp.com/ and install. 11.You should have these commands available now: :: heroku foreman Authenticate to heroku with :: heroku auth:login … -
Progress Report March 11th, 2013
After the break Sorry for the lack of updates, but last week was spring break at the College of Charleston. I had initially planned to complete a few bug fixes for Django, but this did not occur. Instead of working on Django, I spent the break visiting old friends, and applying for internships. I already have another interview on Wednesday, which hopefully go better than my last one. On the Django front, I did have one more documentation change accepted, so not all is lost. As for Team Django a couple of other contributions have been accepted, and some are awaiting review from the core developers. In the wake of the release of Django 1.5 and multiple Django sprint events, many of our targeted issues have been or are being addressed. Having just recently combed back through the tickets that are not feature requests many tend to deal with database interactions or other implementations of Python (Jython, PyPy, and IronPython). Having little to no real world experience with databases and none at all with the other implementations of Python, I'm a little bit hesitant to claim any of these tickets. Nevertheless, the Team is scouring through the tickets, and I … -
Beginner's Guide to PyCon 2013 Part III
This is Part III in a series of blog posts about PyCon US 2013. The goal is to provide a handy reference guide for first time attendees of the world's largest Python conference. Part I was mostly about tutorials, Part II was mostly about the first day of talks, this third post in the series will be about the second day of talks. Early Saturday Morning The PyCon 5K Charity Fun Run begins at 7 AM. Registration for this event is seperate from PyCon, and all proceeds go to the John Hunter Memorial Fund and the American Cancer Society. Saturday Morning After breakfast ends at 8:30 am, don't miss 30 minutes of lightning talks! Then get ready as noted Python experts and proven speakers Jessica McKellar and Raymond Hettiger deliver keynotes to remember. They've changed communities and lives with the speeches given around the world. 10:50 AM talks Getting started with automated testing (Carl Meyer) - This talk will get you moving into good test automation practices, and is presented by one of the maintainers of Django, pip, and virtualenv. 5 powerful pyramid features (Carlos de la Guardia) - Pyramid is a minimalist, modular web framework that encourages excellent coding … -
Beginner's Guide to PyCon 2013 Part III
This is Part III in a series of blog posts about PyCon US 2013. The goal is to provide a handy reference guide for first time attendees of the world's largest Python conference. Part I was mostly about tutorials, Part II was mostly about the first day of talks, this third post in the series will be about the second day of talks. Early Saturday Morning The PyCon 5K Charity Fun Run begins at 7 AM. Registration for this event is seperate from PyCon, and all proceeds go to the John Hunter Memorial Fund and the American Cancer Society. Saturday Morning After breakfast ends at 8:30 am, don't miss 30 minutes of lightning talks! Then get ready as noted Python experts and proven speakers Jessica McKellar and Raymond Hettiger deliver keynotes to remember. They've changed communities and lives with the speeches given around the world. 10:50 AM talks Getting started with automated testing (Carl Meyer) - This talk will get you moving into good test automation practices, and is presented by one of the maintainers of Django, pip, and virtualenv. 5 powerful pyramid features (Carlos de la Guardia) - Pyramid is a minimalist, modular web framework that encourages excellent coding … -
django-fancy-cache with or without stats
If you use django-fancy-cache you can either run with stats or without. With stats, you can get a number of how many times a cache key "hits" and how many times it "misses". Keeping stats incurs a small performance slowdown. But how much? I created a simple page that either keeps stats or ignores it. I ran the benchmark over Nginx and Gunicorn with 4 workers. The cache server is a memcached running on the same host (my OSX 10.7 laptop). With stats: Average: 768.6 requests/second Median: 773.5 requests/second Standard deviation: 14.0 Without stats: Average: 808.4 requests/second Median: 816.4 requests/second Standard deviation: 30.0 That means, roughly that running with stats incurs a 6% slower performance. The stats is completely useless to your users. The stats tool is purely for your own curiousity and something you can switch on and off easily. Note: This benchmark assumes that the memcached server is running on the same host as the Nginx and the Gunicorn server. If there was more network in between, obviously all the .incr() commands would cause more performance slowdown. -
Django Extensions 1.1.1
Another bugfix release of Django-Extensions hits the deck. ChangeLog: FIX: pipchecker, fix typos FIX: pipchecker, improve message when installed version is newer then on pypi FIX: pipchecker, use urllib2 instead of requests FIX: sqldiff, fix for bigserial support -
ArangoDB Driver for Python: added support of Python 3.3 and ArangoDB 1.2
At least added support of Python 3.3 to the ArangoDB Python driver v 0.1.6 (github). Move forward with pip install arango. By default in Python 3.3 pycurl client will not work, so it's better to specify client by hands using: Also, added support of ArangoDB 1.2. -
PyCon Tutorial: Wiring Up Django Packages
You just finished the Django tutorial. What now? You learn how to Wire Up Django Packages! This is a 3+ hour PyCon tutorial on March 14th at 1:20pm, in Santa Clara California at PyCon US. In alphabetical order, the instructors: Audrey Roy Daniel Greenfeld (myself) Kenneth Love Tutorial Description Django is part of an ecosphere of over 20,000 packages, which can be leveraged to great effect. This tutorial will teach the evaluation, use, and extension of third party Python and Django applications in your projects. This tutorial will be a lecture with a lot of detailed and annotated code examples. This tutorial does much more than instruct on how to wire in third party packages, it also instructs on Django/Python best practices such as PEP-8, loose coupling, good database design, clarity of concept, and workable patterns. Material covered: Evaluating packages using Django Packages, PyPI, Read the Docs, and other sources. Must-include packages for every project Class-based Views Forms Models Caching Comments Templates Admin Format This will be a lecture with detailed and annotated code examples. Attendees will have a chance per section to ask questions and submit alternative packages of their own. Prerequisites Practical or professional experience with Django is …