Skip navigation

Tag Archives: django

Okay. First off, I’m aware that I’ve lagged behind my New Year’s Resolution. But I’ve only lagged behind the blogging part: I’m still exploring new stuff monthly. Guess I’ll talk about them in brief for now and, maybe, expound some other time. ^_^

Just to clear some smoke, last time, I told you that I may be in for a volunteer project. Well, so I’ve been and yes, I did use Django pretty extensively with it. The project is for the up-and-coming Python Philippines. I’m the one behind the website and it’s running on Django CMS. Go visit it. We don’t have content yet but, well, you can look at the pretty theme. Hehe.

And just last Saturday, I talked about Django CMS at Python Philippines’ monthly meet-up, to encourage contributors. I feel that my talk went well and I hope to put up a transcript of it in here, to make up for my lack of blogging. Soon!

 

I’ve also been trying to learn some things from the ground up in Python. I’ve done sockets last time and this time around, I tried doing threads. Straight from the box, Python does not really support threads. You’d need libraries like gevent to actually get some concurrency done in peace. But still, unlike PHP, if you just want to switch among jobs, Python has something for you.

I’ve experimented with Python’s threading library. It’s a curious thing, coming from Java’s threads. I got so curious with it I’m even trying to learn statistics to validate my experiments. Because, you know, I did all my samples in powers/multiples of ten.

 

Lastly, as a form of humbrag1, I got myself an Xperia Z, for around a month now2. I must say, it’s damn gorgeous! I never got people who complain that Samsung uses cheap plastic to house impressive hardware but with the Xperia Z’s all-glass build, I get it now. It’s all about the user experience, people. User. Experience.

The hardware is damn powerful; Iron Man 3 plays very smoothly, save for occasional lags. The camera isn’t shabby either; for quick shots, it can do in place of my bulky SLR. Being water proof, dust proof, and shock proof sealed the deal for me.

At last, at last, I can play Temple Run 2 even when it’s raining.

  1. Okay, really bragging mostly []
  2. Those who know the camera I am using can now scream “Fanboy!” []

For February I tinkered with Django, arguably the most popular web framework for Python. Prior to this, the only web framework I’ve had experience with is CodeIgniter and one proprietary JSP-based framework.

In an earlier post I covered installing Django. Nothing much has changed between then and now except that the most recent version of Django, 1.5, now supports Python 3! The first for Django ever. But I’m sticking with Python 2 for the time being.

Note: I tried learning Django last August, in an afterthought project with goals loftier than what my resources could have achieved. Needless to say, I never got too far with it. It was this which prompted me to specify a mini-project I’d use to learn web frameworks (any web framework). I’m not sharing the specifics of it now but you can gather what you can in my sandbox repository.

The Admin Site

One nifty feature of Django is how it creates an admin site automatically for you. On my CodeIgniter projects, I’d pattern the users table something like this, borrowing from Linux file systems:

CREATE TABLE IF NOT EXISTS users(
    -- Some project-specific fields here
    create_time TIMESTAMP NOT NULL,
    last_updater INTEGER NOT NULL,
    last_update TIMESTAMP NOT NULL,
    can_read BOOLEAN NOT NULL DEFAULT TRUE,
    can_write BOOLEAN NOT NULL DEFAULT TRUE,
    can_exec BOOLEAN NOT NULL DEFAULT FALSE,
    PRIMARY KEY (userid),
    FOREIGN KEY (last_updater) REFERENCES users (userid)
    -- Yes, apparently, MySQL allows you to foreign key recursively
);

If I’m really feeling up to it, or if the project requires (never had it tho), I might even implement user groups. But even with this, I can already define 23=8 types of users, which is already way more than what one project usually requires. What I’d normally do here is to designate can_exec privileges to admins. Way neater than maintaining different tables for different user types and then giving admins backdoor log-in pages—one of the naive approaches I used when I first started designing databases for apps.

Django automatically covers this for you, user permissions, groups, and all. The structure becomes a bit different though, and I find permission management way more complicated than my binary-scheme. Take a look at all possible user permissions in Django:

django_permissions

Look at how many that is, and realize that there is still a scroll bar!

What Django basically did is to allow you to specify add/change/delete permissions for every table in your database. Note that due to how Django enforces code modularity (see below), the number of tables in your database could easily blow-up.

Code Modularity

Perhaps one of the most confusing things I encountered as I tried to learn Django was how it distinguishes the terms “project” and “app”. Normally, I’d talk about an “app” being a “project” for some party. But in Django a project is a collection of apps. I had a hard time designing my project around these terminologies even after reading discussions at StackOverflow until I realized that perhaps a better term for “app” would’ve been “plug-in” and there’s really nothing stopping you from making your project as one big plug-in.

With the side-effect, of course, of your app being non modular and probably not well-factored. So, yes, the way Django handles things enforces the nice practice of modularity. Nice to take advantage of it, though it may be too time-taking to wrap your design around Django’s rules.

Django also enforces a form of Object-Relational Mapping (ORM). In fact, you don’t need to write a single line of actual SQL code to get your database up and running—all DB creation is done through the ORM and manage.py . This even allows you to easily switch DB back-ends. Then again, as with above, not all projects would fit well for an ORM approach.

Overall?

I find Django pretty neat given that you only need minimal set-up for this whole bunch of admin stuff. But then I haven’t had much time to explore it although I’ve volunteered in a (non-work, non-paid) project that might use Django pretty extensively. Guess I’ll get more Django this month. Let’s see.

(You’ll find lots of this online. I’m just putting this up here for my personal reference.)

Hi there. I’m writing this with a Linux/Unix/OS X environment in mind. Basically, any OS that has a pretty powerful terminal. Don’t know how this goes for Windows…Cygwin maybe?

First off, make a virtualenv to work on and install Django. You need virtualenv and virtualenvwrapper. If you do not have them installed yet, do not despair. You can use virtualenv-burrito (and I do recommend that you use virtualenv-burrito). The readme of the GitHub repo linked is pretty self-explanatory so I won’t dwell much on that. I’ll just say that…

…if, after following the instructions for virtualenv-burrito, you still can’t invoke virtualenv commands, you might have to do

$ source /home/username/.venvburrito/startup.sh

Or, better yet, just put that line in your .bashrc file (or equivalent).

And also, if you are a Python 3.x fan, Django does not work yet with Python 3.x .

So, make and work on a virtualenv by

$ mkvirtualenv env_name
$ workon env_name

While inside a virtualenv, install Django (note that, being in a virtualenv, you need to be in a virtualenv to use Django):

(env_name)$ pip install Django

Create new projects by invoking

(env_name)$ django_admin.py startproject project_name

This will create a directory named project_name . This directory has some Django-related files. Go to this directory. Start the server by

(env_name)$ python manage.py runserver

And your set to create your web apps!

I’ve heard of Django long ago but what really got me started with it is PyCon Philippines. Here are Daniel Greenfeld’s notes on the PyconPH Django talk by Marconi Moreto, which was my primary reference in setting up my Django installation. However, I had a few deviations and personal discoveries, hence this blog post.