使用ModelForm的Django联系表

时间:2015-03-10 22:40:01

标签: python django view modelform

我正在尝试创建一个既可以通过电子邮件发送邮件也可以存储邮件的联系表单。我认为我有 model.py forms.py admin.py ,我可以创建和存储(不是电子邮件)来自管理员的消息。但我正在努力解决必须同时通过电子邮件发送和存储邮件的 view.py

model.py:

from django.db import models
from django.contrib import admin

class Contact(models.Model):
    name = models.CharField(max_length=100)
    email = models.EmailField()
    message = models.TextField(max_length=10) 
    date_created = models.DateField(verbose_name="Created on date", auto_now_add="True")

class ContactAdmin(admin.ModelAdmin):
    list_display = ('name', 'email', 'message', 'date_created')

forms.py:

from django import forms
from .models import Contact


class ContactForm(forms.Form):
    name = forms.CharField()
    email = forms.EmailField()

对于 views.py ,我需要一些指导才能汇总def contact(request):的代码。我想我必须包含这些模块

from django.conf import settings
from django.shortcuts import render, HttpResponseRedirect, HttpResponse, Http404
from django.core.mail import send_mail
from .forms import ContactForm
from .models import Contact

说到模板部分,我不知道如何使用模板标签在html中呈现联系表单。

所以,我需要帮助来找出正确的视图模板代码..我当然愿意接受其他代码的建议 - 作为你猜对了,这是我第一个真正的Django应用程序。

谢谢!

1 个答案:

答案 0 :(得分:2)

这样的事情:


我会使用django ModelForm来生成表单:

class ContactForm(forms.ModelForm):

    class Meta:
        model = Contact
        exclude = ('date_created', )

文档:https://docs.djangoproject.com/en/1.7/topics/forms/modelforms/#modelform

用于实际视图的FormView:

from django.conf import settings
from django.core.mail import send_mail
from django.views.generic import FormView

from .forms import ContactForm

class ContactFormView(FormView):

    form_class = ContactForm
    template_name = "email_form.html"
    success_url = '/email-sent/'

    def form_valid(self, form):
        message = "{name} / {email} said: ".format(
            name=form.cleaned_data.get('name'),
            email=form.cleaned_data.get('email'))
        message += "\n\n{0}".format(form.cleaned_data.get('message'))
        send_mail(
            subject=form.cleaned_data.get('subject').strip(),
            message=message,
            from_email='contact-form@myapp.com',
            recipient_list=[settings.LIST_OF_EMAIL_RECIPIENTS],
        )
        form.save()
        return super(ContactFormView, self).form_valid(form)

文档:https://docs.djangoproject.com/en/1.7/ref/class-based-views/generic-editing/#formview

你的模板:

{% extends 'base.html' %}
{% block title %}Send an email{% endblock %}

{% block content %}
    <div class="row">
        <div class="span6">
            <h1>Send an email</h1>
            <form action="." method="post">
                {% csrf_token %}
                {{ form }}
                <input type="submit" value="Submit" />
            </form>
        </div>
    </div>
{% endblock %}

{% block extrajs %}
<script src="{{ STATIC_URL }}js/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
$(function() {
    $('#id_name').focus()
});
</script>
{% endblock %}

文档:https://docs.djangoproject.com/en/1.7/topics/forms/#the-template