Change root password

Posted: April 18th, 2010 | Author: | Filed under: Django | Tags: | No Comments »

The manage.py provides a changepassword option which allows you to change the password of any given User (django.contrib.auth).

To change the password of the root account:

python manage.py changepassword
Changing password for user 'root'
Password: xxx
Password (again): xxx
Password changed successfully for user 'root'

If you want to change the password for another User:

python manage.py changepassword someuser
Error: user 'someuser' does not exist

UUID

Posted: April 16th, 2010 | Author: | Filed under: Django | Tags: | No Comments »

Generating UUID(A Universally Unique IDentifier) example: (only from python 2.5+)

>>> import uuid
>>> unique_id = str(uuid.uuid4())
>>> print unique_id
e31c4c33-2d02-49ef-a501-a9f947f86697
>>>

Reference: http://docs.python.org/library/uuid.html


Sending HTML email

Posted: April 12th, 2010 | Author: | Filed under: Django | Tags: , | 9 Comments »

It is possible to send a HTML email using the EmailMultiAlternatives

Example:

from django.core.mail import EmailMultiAlternatives
from django.template.loader import render_to_string
from django.utils.html import strip_tags

subject, from_email, to = 'Order Confirmation', 'admin@yourdomain.com', 'someone@somewhere.com'

html_content = render_to_string('the_template.html', {'varname':'value'}) # ...
text_content = strip_tags(html_content) # this strips the html, so people will have the text as well.

# create the email, and attach the HTML version as well.
msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
msg.attach_alternative(html_content, "text/html")
msg.send()

render_to_string

Posted: April 12th, 2010 | Author: | Filed under: Django | Tags: | No Comments »

The render_to_string is similar to render_to_response but instead of sending the data to the response object it just saves the result as a string object.
This can be then used as normal string.

Example:

from django.template.loader import render_to_string
response = render_to_string('the_template.html', { 'varname': 'value' })

Be carefull, you CAN NOT do the following:

from django.template.loader import render_to_string
def my_view(request):
    ...
    return render_to_string('the_template.html', { 'varname': 'value' }) # CRASH...!

But you CAN DO this instead:

from django.http import HttpResponse
from django.template.loader import render_to_string
def my_view(request):
    ...
    return HttpResponse( render_to_string('the_template.html', { 'varname': 'value' }) )

TEMPLATE_STRING_IF_INVALID

Posted: April 8th, 2010 | Author: | Filed under: Django | Tags: | No Comments »

A quick way to track down the non existing variables in the template is to use the TEMPLATE_STRING_IF_INVALID.

1) set TEMPLATE_STRING_IF_INVALID in settings.py

TEMPLATE_STRING_IF_INVALID = 'VARIABLE ERROR'

2) in any template/html file:

{{ variable_which_does_not_exist }}

The variable_which_does_not_exist will be replaced by ‘VARIABLE ERROR’

Read more about how django handles invalid template variables