'django' Tag

  • django registration form custom field

    June 1, 2010

    In most cases when a user registers in your django application you need to know more than just user name, password and e-mail. To add custom fields to the registration form you should pass form_class parameter to the register view function – it’s easy, but saving additional data could be rather complex. There are solutions […]

  • check login decorator for json responses

    March 21, 2010

    Django has a login_required decorator that redirects to the login page if user is not authenticated, here I’ll show how to write a custom decorator for json responses. If the uses is authenticated then decorated function returns the desired data otherwise some error in json format. NB – this decorator is for view functions with […]

  • dynamic forms in dajngo

    March 18, 2010

    Django has a very convenient forms framework and I want to show how to implement a form with dynamic set of fields. First – declare a form class: from django import forms class SomeAddressForm(forms.Form): pass Then the tricky part – declare a function that returns type of dynamic form: def getAddressForm(addMoreField): fields = {“city” : […]

  • Have ascii coding error in django admin?

    March 10, 2010

    If you have some unicode data in tables e.g. some Russian names and want them to be displayed  by default you need to do some trick: class SomeTable(models.Model): name = models.CharField(max_length=def_max_length) def __unicode__(self): return “%s” % self.name See that – instead of returning self.name directly I returned “%s” % self.name and admin panel is shining!

  • Django is slow?

    March 2, 2010

    Check that Debug=False and that Google Analytics is off! In my case analytics slows speed dramatically.

  • django: Comments from authenticated users

    January 25, 2010

    Djano has a beautiful application – django.contrib.comments. And what is even more beautiful that it is customizable! So I’ll describe how to customize it so that only authenticated users are able to leave comments and comments must contain some rating – I will call such comments reviews. First, create an overridden comments application in a […]

  • get_comment_list reversed

    January 23, 2010

    Django has comments application and it works fine for me, except that I need comments in the reversed order. I could have used reversed order in the {% for %} tag, but this doesn’t work with pagination as pagination wants already prepared list. So I wrote a separate filter: from django.template import Library register = […]

  • urllib and cookies

    September 10, 2009

    urllib doesn’t have cookie support and it’s difficult to test a django application that uses sessions as session id is stored in the cookies. However, urllib2 is able to handle cookies: import urllib2, cookielib jar = cookielib.CookieJar() handler = urllib2.HTTPCookieProcessor(jar) opener = urllib2.build_opener(handler) urllib2.install_opener(opener) data = urllib2.urlopen(someurl).read() Source: http://coding.derkeiler.com/Archive/Python/comp.lang.python/2006-04/msg04229.html

  • {% with %}

    July 8, 2009

    There is a very usufull tag in django templates which lets to remove duplication very smoothly. For instance, you  have 30 places in some template where you’d like to put a state of something, e.g.  ’Loding data…’ or ‘Ready’, and you have 30 instances of some object that return those states through a method called […]

  • Odd-even table rows with Django

    July 2, 2009

    Using the following code you can color table rows in django templates:: <table> {% for item in data %} <tr {% if not forloop.counter|divisibleby:”2″ %} class=”odd” {% endif %} > <td> {{item.name}} </td> </tr> {% endfor %} </table> P.S. You can also use {% cycle %}

Powered by Wordpress and MySQL. Theme by Shlomi Noach, openark.org