The Django Plugins I Use

  • Thu 01 November 2012

  • For anyone reading this, a note. I'm not a developer. I've been using Django for less than 6 months. See this post and this post for history.

    The Django Plugins I Use & Why

    • Django-crispy-forms From Django Package's description "The best way to have DRY Django forms. The app provides a tag and filter that lets you quickly render forms in a div format while providing an enormous amount of capability to configure and control the rendered HTML."

      My $.02 cents. It makes forms simple.

    An example:

    ## forms.py
    
    from django.forms import ModelForm
    from crispy_forms.helper import FormHelper
    from crispy_forms.layout import *
    from crispy_forms.bootstrap import *
    from web_site.Community.models import CommunityEvent
    $
    $
    class CommunityEventForm(ModelForm):
        """A form to allow the creation of a new article"""
        class Meta:
            model = CommunityEvent
            fields = ['title', 'description']
    
        def __init__(self, *args, **kwargs):
            self.helper = FormHelper()
            self.helper.form_method = 'POST'
            self.helper.form_id = 'add-article'
            self.helper.form_class = 'well form-inline'$
            self.helper.form_action = ''$
            self.helper.layout = Layout($
                Field('title',
                css_class="input-large",
                placeholder="Event
                Title"),
                Field('description',
                type="textarea", id="redactor_content", placeholder="Description"),
            FormActions(
                Submit('submit', "Submit", css_class='btn'),
                Submit('cancel', "Cancel", css_class='btn'),
            )
        )
            super(CommunityEventForm, self).__init__(*args, **kwargs)
    

    And my template:

    {% extends "account/base.html" %}
    {% load upload_tags %}
    {% load account_tags %}
    {% load crispy_forms_tags %}
    {% crispy form form.helper %}
    
    
    
    
    {% block head_title %}Account{% endblock %}
    
    {% block body %}
        {% if user.is_superuser %}
        <div class="container">
    
        <legend>Manage Community Event</legend>
    
           {% crispy form %}
    
        {% else %}
    
                <h1>You May have reached this page in error or you don't have access to this page</h1>
    
        {% endif %}
    
    </div>
    
    {% endblock %}
    

    You can see it's a simple {% crispy form %} in the template, and I get a form rendered.

    • Django-floppyforms The way it was described to me was all the benefits of HTML5 forms, and no downside. My advice, install it, see for yourself. Seeing is believing, and only after someone showed me, was I convinced to use it going forward.

    • pillow > PIL. From PyPi's description: "Pillow is the "friendly" PIL fork. PIL is the Python Imaging Library. Pillow was started for and is currently maintained by the Plone community. But it is used by many other folks in the Python web community, and probably elsewhere too."

      If you have ever tried to install PIL in a virtualenv, it's not the most friendly of plugins. Use Pillow, make your life easier!

    • pip install ipython (in your virtualenv) If you've ever used Idle or Django-shell from manage.py ipython extends that. If you've never used iPython before, again, do yourself a favor and use it!

    • django-extensions From their description "Django Extensions is a collection of custom extensions for the Django Framework." I use django-extensions local/dev only and the two things I take advantage of is manage.py shell_plus (+ iPython FTW!) and manage.py runserver_plus. Per their documentation, it is VERY important you do not run runserver_plus in production.

      shell_plus + iPython is nothing short of awesome. You know how you open django-shell, and you do a from 'app.views' import view? This does it automatically!! It's all there! Also, tab completion! Which exposes hits via tab. Not sure what you can/can't do...press 'tab'.

    • South learn it! (it is very likely you will bleed -- backup OFTEN!)

      Only advice I can give you, read the docs. Read them again. Understand what is happening and why it's happening. Then find #django-south and/or #django on Freenode and get help! My advice, start using it from the beginning. You will lose too much time doing a manage.py reset app. Also, using it early and often will give you exposure. My advice also, if you're hosting on Postgresql in production, use Postgresql in your dev environment. South is a bit quirky sometimes.

    • pinax-theme-bootstrap I'm torn on. It's VERY COMPLEX. The first project I just bootstrapped "manually". I LOVED what Pinax is/was, so I decided to give it a go. Not sure if I'd use it agian.

    • django-user-accounts Use it! AWESOME! First pjoject I built my own Django login/logout. NEVER AGAIN! Word of Caution -- like pinax-theme-bootstrap, django-user-accounts is also VERY COMPLEX and there is a learning curve.

    • django-thumbs With PIL you can do the dirty and generate your own thumbnails, or you can use something like django-thumbs and let it do it for you! Also, when you delete an image in a view, it deletes the file!

      An example:

    class PressArticle(models.Model):
        title = models.CharField(_("Title"), max_length=255, null=True, blank=True)
        publication = models.CharField(_("Publication"), max_length=255, null=True,blank=True, unique=True)
        year = models.PositiveSmallIntegerField(_("Year"), max_length=4, null=False, blank=False)
        thumbnail = ImageWithThumbsField(_("thumbnail"), upload_to="images/",
            blank=True, null=True, sizes=((125, 125), (250, 250), (640, 427),))
    

    Something else to note. If you're using South, you need something like this, so South knows what to do with the field-type 'ImageWithThumbsField':

    from south.modelsinspector import add_introspection_rules
    
    add_introspection_rules(
        [
            (
                (ImageWithThumbsField, ),
                [],
                {
                    "verbose_name": ["verbose_name",{"default":None}],
                    "name": ["name", {"default": None}],
                    "width_field": ["width_field", {"default": None}],
                    "height_field":["height_field", {"default": None}],
                    "sizes": ["sizes", {"default": None}],
                 },
             ),
         ],
    ["^django_thumbs.db.models",])
    
    • django-debug-toolbar Use it! Django Deug Toolbar exposes some of the "magic" that is Django. Ever wonder what SQL statement/syntax it is using exactly, Django Debug Toolbar shows you this. If you have complex templating, with pages inheriting from pages, and includes, it often becomes difficult to determine what template/page is loading what data. Django Debug Toolbar shows you your templates, and *args and **kwargs that are passed in the request. All of this is helpful in troubleshooting.

    Comments !

    social