Xanax onlineAdderall onlineLevitraviagra without prescriptionadderall onlineadderall without prescriptionPhentermine onlinetramadol onlinevalium online

Archive for the ‘Python’ Category

Jun20

Redirect non-www to www in django

In your settings file add

PREPEND_WWW = True

Then run syncdb

python manage.py syncdb
Jun02

Accessing dictionaries inside lists with Django templates

Click here to skip the story and straight to the code

Today while working on the new SVO i ran into an issue with the coding of breadcrumbs. I wanted to pull the breadcrumb code out of each page template and put it into the header template.

This is what i had originally in each template:

<p id="breadcrumbs">
    <a href="/">Home</a><span class="bc_sep">></span>
    <a href="{% url videos %}">Videos</a><span class="bc_sep">></span>
    <a href="{% url company video.company.id video.company.name|slugify %}">{{ video.company.name }}</a><span class="bc_sep">></span>
    <strong>{{ video.name }}</strong>
</p>

This quickly became annoying, repeating this code in every pag’es template. I decided to create a list of dictionaries inside each view to represent the breadcrumbs:

breadcrumbs = []
breadcrumbs.append( { 'home':reverse( 'home' ) } )
breadcrumbs.append( { 'skaters':reverse( 'skaters' ) } )
breadcrumbs.append( { index:'' } )
return render_to_response('index.html', {...'breadcrumbs':breadcrumbs})

Then in my template i loop the list and extract the dictionary:

<p id="breadcrumbs">
{% for breadcrumb in breadcrumbs %} #this loops the list
	{% for text, link in breadcrumb.items %} #this extracts the dictionary inside each list
		{% if link %}
			<a href="{{ link }}">{{ text|capfirst }}</a><span class="bc_sep">></span>
		{% else %}
			<strong>{{ text|capfirst }}</strong>
		{% endif %}
	{% endfor %}
{% endfor %}
</p>

The for loop seems like an “ugly” way to access the inner dictionary but i was unable to find a better method.

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 =)