Showing posts with label Django. Show all posts
Showing posts with label Django. Show all posts

2011-12-29

Django "SMTPServerDisconnected Connection unexpectedly closed"

  1. Check mail server by "telnet servername port(25)"
  2. Check IMAP server (e.g. deovecot)

2009-01-26

SQL History in Django

Use django.db.connection to get SQL history.
Example:
from django.db import connection
connection.queries

2007-10-25

How to integrate reCAPTCHA with Django

1. Sign up with reCAPTCHA for get public and private key.
2. Download reCAPTCHA for Python.
3. Copy captcha.py to location in your PYTHON_PATH so that you can import it. (e.g /usr/local/python2.5/Lib/site/packages)
4. Now your Django site can use reCAPTCHA.
in your view use reCAPTCHA like this.

import capcha

def captcha_view(request):
captcha.displayhtml(RECAPTCHA_PUB_KEY) # use for display reCAPTCHA in template.
captcha_obj = captcha.submit( request.POST['recaptcha_challenge_field'],
request.POST['recaptcha_response_field'],
RECAPTCHA_PRIVATE_KEY,
request.META['REMOTE_ADDR'] ) # use for create captcha object for check result.
if captcha_obj.is_valid:
return success
else:
return error

2007-09-17

Using User info outside request in Django

1. Create "middleware" directory in project directory.
2. Create python file name "__init__.py and "threadlocals.py" in "middleware" directory.
3. in side "threadlocals.py" copy this code
    # threadlocals middleware
try:
from threading import local
except ImportError:
from django.utils._threading_local import local

_thread_locals = local()
def get_current_user():
return getattr(_thread_locals, 'user', None)

class ThreadLocals(object):
"""Middleware that gets various objects from the
request object and saves them in thread local storage."""
def process_request(self, request):
_thread_locals.user = getattr(request, 'user', None)

4. in "settings.py" add
"project_name.middleware.threadlocals.ThreadLocals" to the MIDDLEWARE_CLASSES variable.
5. using like this
from project_name.middleware import threadlocals
user = threadlocals.get_current_user()

For more information >> Go to CookBookThreadlocalsAndUser

2007-09-01

How to set multi django projects on one apache

Use PythonInterpreter in Location setting.
<Location "/something">
SetEnv DJANGO_SETTINGS_MODULE mysite.settings
PythonInterpreter mysite
</Location>
It don't need if you use different VirtualHost.

2007-08-23

How to send email with Django

1. In setting.py define and set values for EMAIL_HOST, EMAIL_HOST_USER and EMAIL_HOST_PASSWORD.
2. In your views import django.core.mail.EmailMessage and SMTPConnection.
3. Create instance of EmailMessage e.g
email = EmailMessage('subject', 'message', 'from_email', list of recipient)
4. Send email by use send method e.g email.send()
5. SMTPConnection use for set connection when you want to send multiple messages in one connection if not create SMTPConnection it will create connection every call send() method e.g
connection = SMTPConnection()
email = EmailMessage('subject', 'message', 'from email', [list of recipient email], connection=connection)