Django Emailthis Setup

From splike.com
Jump to: navigation, search

I wanted to try http://pypi.python.org/pypi/django-emailthis/ but there weren't any docs describing how to set it up. This is what I figured out:

Install:

sudo easy_install django-emailthis

In settings.py, add to INSTALLED_APPS:

'emailthis',

In urls.py, add:

(r'^email/', include('emailthis.urls')),

Setup your "Site" (http://docs.djangoproject.com/en/dev/ref/contrib/sites/) by going to /admin and changing the Site from "example.com" to your actual domain. I assume you'll need 'django.contrib.sites' in INSTALLED_APPS

Make sure your django model has a get_absolute_url method set up

If you want to customize the default subject, add to settings.py

def emailthis_subject_getter(model_object):                                
    str(model_object) + " is really cool!"                          
EMAILTHIS_SUBJECT_GETTER = emailthis_subject_getter

In your templates folder create an emailthis/form.html something like this:

<h2>{{ item }}</h2>
<form method="POST" action="{% url emailthis_submit content_type.id item.id%}" id="emailthis">
{{ form }}
<input type="button" value="Send" id="emailthis_button"/>
</form>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery-1.3.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        $('#emailthis_button').click(function(){
            // TODO: client-side input validation
            $form = $(this).parents('form');
            values = {};
            $form.children(':input').each(function(){
                values[$(this).attr('name')] = $(this).val();
            });
            $.ajax({
                type: 'POST',
                url: $form.attr('action'),
                data: values,
                error: function(xhr, textStatus, errorThrown) {
                    alert(textStatus);
                },
                success: function(data, textStatus) {
                    alert(textStatus);
                },
            });
            return false;
        });
    });
</script>

In your templates folder create an emailthis/form.html something like this:

Hey there,

{{ email_from }} ({{ user }}) sent you this link about {{ item }}:

{{ url }}

Subject: {{ subject }}
{{ message }}

{{ site }}
{{ site_url }}

Make sure your django EMAIL_ settings are set up (http://docs.djangoproject.com/en/dev/topics/email/)

Create database table by running:

python manage.py syncdb

Then you can go to (or load with AJAX) /email/1/2 where (for example) 1 is a specific object type, and 2 is an id of an object of that type. You must submit via ajax.