Archive for the ‘Learning Python’ Category

Jun21

Learning Python – Day 2

I’m excited about my new admin interface. Page 2 on the tutorial goes into how to customize the admin. I don’t care about that at the moment so I go to page 3.

Page 3 is about URLs. Django uses a sequence of tuples populated with regular expression -> url maps. Great, this is how I did things in PHP. I add a couple of URLs and move on.

Next it’s time to create my views. I write my first view which simple prints “Hi!”. It works, great. I see that the basics of the template language used in Django are very similar to how I do it in PHP. I port my view from my PHP template to the template in Django.

{% foreach company in category.company.all() %}

Unfortunately this doesn’t work. It’s not looping over my categories/companies/videos. I think it’s because it’s having trouble going backwards with my foreign key relationships.

I find that in order to go backwards you have to do this

{% for company in category.company_set.all() %}

You have to use _set to access foreign key items. This doesn’t work either! I give up on this route and I try the method I use in PHP… creating a giant multi-dimensional array and then looping over that.

This is where I hit a wall. I quickly find out that Python’s version of arrays, called dictionaries, can’t be created like they can in PHP. I don’t like that one bit.

Python doesn’t have a version of

$video['company_id']['category_id']['video_id']['name'] = 'Video Name';

I find some custom classes via google, but Django won’t iterate over them. A couple hours later I figure I need help. I ask a couple of places and everyone suggests that it is possible with Django’s ORM, I just need to remove the ()

I find that you have to do this!

{% for company in category.company_set.all %}

You have to remove the () for functions that have no arguments. Works like a champ. Two minutes later I have all categories/companies/videos displayed correctly. I’m still using Django’s development server though so I have no styles/images, just black test on a white background.

I read somewhere that I should be using mod_wsgi instead of mod_python (I can’t find the source). Using this I get it working with Django pretty quickly.


Good:Django’s ORM

Bad: Python’s dictionary handling not my favorite. I enjoy the way PHP has implemented arrays and how they can be used as lists, dictionaries, stacks, etc. Django’s documentation is thorough, but I get lost in the maze of versions it has.

Notes: I don’t know why I didn’t think of this, but it’s been pointed out to me that you can do this:

a = dict()
a['foo'] = dict()
a['foo']['bar'] = {'this works': 'yes it does'}

That probably would have solved my problem.

Jun17

Learning Python – Day 1

Step one: Does my server have python? Yes.

Great, I have python. Now what? I don’t know. I google “python web development”. I click the first link. Python webservers! I have to embed python into apache! Duh!

sudo apt-get install libapache2-mod-python

Now I have mod python running. Two hours later I have a hello world on my development site. That took way too long. I’m annoyed. I start asking people for recommendations. I just want to code! I’m sick of setting up my server. With PHP it’s cake. One call to apt-get install and off you go.

98% of the people I ask recommend Django. So I install django via apt-get install, head over to djangoproject.com, and start reading the tutorials.

Page 1 is pretty straight forward. I do some creation/configuring and create/activate my models. Then I get to play with my models. Django’s ORM is pretty nice. I usually prefer to write all my own SQL, but I could get used to this.

I get to page 2 of the tutorial where they start talking about the automatic admin. I’m shocked. This is amazing. Are frameworks in PHP this awesome? I’ve always used my own PHP framework. I’ll have to check that out. Back to Django.

This admin app is amazing I have to try this with SVO. I go back to page 1 on the tutorial and start following it but with SVO details. I get to page 2 and then import all my data into my newly created tables. I have a fully functioning admin in 10 minutes!

That’s all for day one.


Good: Django, Django’s automatic admin

Bad: Server setup not so easy

Notes:

1. To view the Django development server on a host other than localhost bind it to your server’s IP address

sudo python manage.py runserver 0.0.0.0:8000
Jun15

I’ve decided to learn Python

I decided to learn Python by rewriting skate videos online in it.

I’m going to chronicle my experience right here… for noone to read =)