python - Preview form in a template based on a dropdown options in Django -
i working on application preview html emails.
on initial creation page, want user able preview email before submitting it.
here have far
views.py
def mail_create(request): context = requestcontext(request) if request.method == 'post': form = mailform(request.post) if form.is_valid(): if '_preview' in request.post: post = form.save(commit=false) context_dict = {} subject_line = form.cleaned_data['subject_line'] headline = form.cleaned_data['headline'] mail_body = form.cleaned_data['mail_body'] image = form.cleaned_data['image'] context_dict = {} context_dict['mail'] = mail context_dict['subject_line'] = subject_line context_dict['headline'] = headline context_dict['mail_body'] = mail_body context_dict['image'] = image t = form.cleaned_data['template'] preview = t.render(context(context_dict)) return httpresponse(preview) elif '_save' in request.post: form.save(commit=true) return httpresponseredirect(reverse('posta.mail')) else: print form.errors else: form = mailform() return render_to_response('mail_create.html', {'form' : form}, context)
forms.py
from django import forms posta.models import mail, mail_template, image django.contrib.auth.models import user class mailform(forms.modelform): name = forms.charfield(max_length=140, help_text="please enter name email.") subject_line = forms.charfield(max_length=140, help_text="please enter subject line email.") target = forms.charfield(max_length=140, help_text="who email going to?") date_scheduled = forms.datefield(help_text="when need email sent?", widget=forms.textinput(attrs={'id':'datepicker'})) template = forms.modelchoicefield(queryset=mail_template.objects.all(), help_text="select template") headline = forms.charfield(max_length=140, help_text="please enter headline email.") mail_body = forms.charfield(widget=forms.textarea) image = forms.modelchoicefield(queryset=image.objects.all()) class meta: model = mail fields = ('name','subject_line','target','date_scheduled','template', 'headline', 'image', 'mail_body')
when click preview button, error exception value:
'mail_template' object has no attribute 'render'
should using render response? template being data form template selection?
edit: here models.py
from django.db import models django.contrib.auth.models import user # create models here. class mail(models.model): draft = 0 ready = 1 sent = 2 status_choices= ((draft, 'draft'),(ready, 'ready'), (sent, 'sent')) name = models.charfield(max_length=140, unique=true) date_created = models.datetimefield(auto_now=true) date_scheduled = models.datefield() target = models.charfield(max_length=140) template = models.foreignkey('mail_template') subject_line = models.charfield(max_length=140) headline = models.charfield(max_length=140) mail_body = models.textfield() status = models.integerfield(choices=status_choices, default=draft) image = models.foreignkey('image') def __unicode__(self): return self.name class image(models.model): image = models.imagefield(upload_to='gallery/') class mail_template(models.model): name = models.textfield() template_body = models.textfield() def __unicode__(self): return self.name
example mail_template:
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>{{mail.subject_line}}</title> </head> <body leftmargin="0" marginwidth="0" topmargin="0" marginheight="0" offset="0" style="-webkit-text-size-adjust:none; margin:0; padding:0; background-color:#fafafa; width:100%; "> <center> <table border="0" cellpadding="0" cellspacing="0" height="100%" width="100%" id="backgroundtable" style="margin:0; padding:0; background-color:#fafafa; height:100%; width:100%; "> <tr> <td align="center" valign="top" style="border-collapse:collapse; "> <table border="0" cellpadding="0" cellspacing="0" width="600" id="templatecontainer" style="border:0; background-color:#fdfdfd; "> <tr> <td align="center" valign="top" style="border-collapse:collapse; "> <table border="0" cellpadding="0" cellspacing="0" width="600" id="templateheader" style="background-color:#ffffff; border-bottom:5px solid #505050; "> <tr> <td align="center" class="headercontent" style="border-collapse:collapse; color:#202020; font-family:arial; font-size:34px; font-weight:bold; line-height:100%; padding:10px; text-align:center; vertical-align:middle; "> <div class="mkteditable" id="column_text1"> <img title="{{mail.subject_line}}" src="{{ mail.image}}" alt="{{mail.subject_line}}" height="120" width="600"> </div> </td> </tr> </table> </td> </tr> <tr> <td align="center" valign="top" style="border-collapse:collapse; "> <table border="0" cellpadding="10" cellspacing="0" width="600" id="templatebody"> <tr> <td valign="top" class="bodycontent" style="border-collapse:collapse; background-color:#fdfdfd; "> <table border="0" cellpadding="10" cellspacing="0" width="100%"> <tr> <td valign="top" style="border-collapse:collapse; "> <div mc:edit="std_content00" style="color:#505050; font-family:arial; font-size:14px; line-height:125%; text-align:justify; "> {{ mail.body }} </div> </td> </tr> </table> </td> </tr> </table> </td> </tr> </table> </td> </tr> <tr> <td align="center" valign="top" style="border-collapse: collapse;"></td> </tr> </table> <br> </center> </body> </html>
ok, explains things. problem you're not creating actual template
object @ point. changing 2 lines fix it:
def mail_create(request): context = requestcontext(request) if request.method == 'post': form = mailform(request.post) if form.is_valid(): if '_preview' in request.post: post = form.save(commit=false) # ... t = form.cleaned_data['template'] tmpl = template(t.template_body) # <-- preview = tmpl.render(context(context_dict)) # <-- return httpresponse(preview)
make sure have from django.template import template
top.
btw, mail_template.name
should charfield
.
Comments
Post a Comment