SQLite DATABASE_ENGINE on Windows

Posted: January 28th, 2010 | Author: Davo | Filed under: Django | Tags: , , | No Comments »

I was having issues to use SQLite on Windows based machines.
The database was correctly creating with managy.py syncdb but I was not able to use it.
A quick fix was to edit the settings.py and specify a FULL path for the database.

DATABASE_ENGINE = 'sqlite3'
DATABASE_NAME = 'C:/path/to/thedb' # instead of 'thedb'

Hope this will solve your issue as well.


Strip/Remove HTML tags

Posted: January 26th, 2010 | Author: Davo | Filed under: Django | Tags: , , , | No Comments »

To strip/remove HTML tags from an existing string we can use the strip_tags function.

# import the strip_tags
from django.utils.html import strip_tags

# simple string with html inside.
html = '<p>paragraph</p>'
print html # will produce: <p>paragraph</p>

stripped = strip_tags(html)
print stripped # will produce: paragraph

This is also available as a template tag:

{{ somevalue|striptags }}

If you want to remove only specific tags you need to use the removetags

from django.template.defaultfilters import removetags
html = '<strong>Bold...</strong><p>paragraph....</p>'
stripped = removetags(html, 'strong') # removes the strong only.
stripped2 = removetags(html, 'strong p') # removes the strong AND p tags.

Also available in template:

{{ value|removetags:"a span"|safe }}

Validating email without EmailField

Posted: January 25th, 2010 | Author: Davo | Filed under: Django | Tags: , , | No Comments »

Sometimes it is handy to test if a given string is a valid email without having EmailField or Form.
The following function can be used to validate an email.

from django.core.validators import email_re

def is_valid_email(email):
    if email_re.match(email):
        return True
    return False

TEMPLATE_DIRS relative to the project folder

Posted: January 21st, 2010 | Author: Davo | Filed under: Django | Tags: , | 1 Comment »

When developing with Django we need to specify our templates directory inside the settings.py with the TEMPLATE_DIRS directive. When it comes to deploying or publising the application we often forget to change this setting to the new folder where the application will be deployed. In order to counter this, it is possible to make this setting dynamic with the following in setting.py

import os.path
PROJECT_DIR = os.path.dirname(__file__) # this is not Django setting.
TEMPLATE_DIRS = (
    os.path.join(PROJECT_DIR, "templates"),
    # here you can add another templates directory if you wish.
)

Validate an instance without POST values

Posted: January 14th, 2010 | Author: Davo | Filed under: Django | Tags: , , , | No Comments »

Sometimes it is useful to validate an instance of a model without having the POST values.
You will ask me why? Well, a form can be filled in different steps and be validated at the final step.
This is where it comes handy, validate the Object to see if all the steps are properly completed.

The following example demonstrates how to validate an instance without using the request.POST

from django.db import models
from django import forms

# a simple example of an Article model
class Article(models.Model):
     title = models.CharField(max_length=100)
     body = models.TextField(max_length=400)

# let's create the form from the Article model.
class ArticleForm(forms.ModelForm):
     class Meta:
          model = Article

# an article form with only the title on it.
# we can use this as a step 1, where the user is filling only the title.
class ArticleLessform(forms.ModelForm):
     class Meta:
          model = Article
          fields = ('title', ) # only show the title.
# inside views.py
from your_directory.models import Article, ArticleForm, ArticleLessForm 

def do_something(request):
     # change for the appropriate id.
     article_qs = Article.objects.filter(id=1)

     # get the actual Article object.
     article = article_qs.get()                      

     # all the magic is on this line
     # values() returns a key/values pairs and we use this to simulate a request.POST.
     form = ArticleForm(article_qs.values()[0], instance=article)
     if form.is_valid()
        print 'It seems that the title and body are filled properly.'
     else:
        print 'The Article object did not validate'

You can read more about the values() on the django docs.