May 4th, 2011
I have lost hours and hours on this one. My subscription confirmations just didn’t work, I have scanned the network traffic with wireshark and my app just didn’t send a response to confirmation request.
To solve this problem all you need to do is open application.conf ande set play.pool value to 2 or higher. Default is one and server can’t responde to a request until the first one is finished so confirmation fails.
March 23rd, 2011
Split string to an array
One common task that you probably want to learn how to do is spliting a string into an array (like explode function in PHP). You can do this by calling “split” method. By default it will split a string on whitespace, but you can pass it a delimiter. Delimiter can be a string or regular expression.
1
2
3
4
5
6
7
| animals = 'dog wolf cat lynx tiger'
animals.split # => ["dog", "wolf", "cat", "lynx", "tiger"]
"6534-234-23-45".split "-" # => ["6534", "234", "23", "45"]
calc = "45 + 36 - 25 * 14"
calc.split /\s*[-+*]\s*/ # => ["45", "36", "25", "14"] |
Read the rest of this entry »
March 23rd, 2011
Has and belongs to many
If you just need to set a simple many-to-many relationship the easiest way to do this is with has_and_belongs_to_many. Here is how your models should look like.
1
2
3
4
5
6
7
| class Post < ActiveRecord::Base
has_and_belongs_to_many :tags
end
class Tag < ActiveRecord::Base
has_and_belongs_to_many :posts
end |
This association is supported by an additional database table that will store post_id and tag_id for each relationship. This table should be called posts_tags and it will not be created for you, so you will have to create your own migration.

Read the rest of this entry »
February 10th, 2011
I’m starting to work on my project that is supposed to use PubSubHubbub. I had some difficulty understanding it as I found documentation inadequate. I have managed to get this simple app to work with a public Hub (http://pubsubhubbub.appspot.com/).
All it does is confirm all subscription requests, and when it recieves data it prints it on screen. It uses Sinatra for handling http requests. It’s not much but maybe someone will find it useful.
Read the rest of this entry »
April 20th, 2010
A few months ago I found a really useful
Stack Overflow Question. Here are my favorites from the answers.
Use render_to decorator instead of render_to_response
This decorator is found in the app
django annoying, and is a very nice shortcut for declaring what template a view should render.
Instead of returning the response of
render_to_response, you just return a python dict which will be used as the template context for the template specified as argument to the @render_to decorator. If anything else than a dict is returned, normal view processing will occur, so this won't break redirects or any other cases where you might return a HttpResponse (for example normal
render_to_response code).
Read the rest of this entry »
April 16th, 2010
This stupid install process gets me every time! How do you run Google App Engine which currently
requires Python2.5 on a machine that has Python2.6 installed? This also goes for anybody trying to use Django with App-Engine.
Read the rest of this entry »
April 15th, 2010
If you want to put your views in a subfolder and you are getting an import error, all you have to do is create a blank “__init__.py” file within that subfolder and everything should work fine.
March 7th, 2010
Deploying Django is a process that can drive one bananas. There are
a lot of things to setup to go from your development environment to the production. Aside from the regular hassles - there come special little buggers that can really make you mad. If you ever had problems with 500.html pages, url configurations or import errors - you know what I mean.
However it doesn't all have to be that bad. There are quite a few steps you can take early on to minimize the pain. If you do get into trouble - there are some things you can do to debug out of it.
Read the rest of this entry »
March 2nd, 2010
In this post I will try to explain some tricks I use to make python code more readable. These tips won’t make your code shorter and it won’t be better optimized, it will actually use more memory than before, but it will be much easier to understand and edit for someone else.
Read the rest of this entry »
February 26th, 2010
If you have non-english names for your models, they will look bad when django admin tries to make plural out of them by adding “s” in the end. Fortunately it is easy to override by defining “verbose_name_plural” in your model. Here is an example:
1
2
3
4
5
6
| class Tag(models.Model):
class Meta:
verbose_name_plural = "Tagovi"
ime = models.CharField(max_length="300")
opis = models.TextField(blank = True) |