gpt4 book ai didi

python - 使用 ModelForm 的 Django 联系表单

转载 作者:太空宇宙 更新时间:2023-11-04 03:36:09 27 4
gpt4 key购买 nike

我正在尝试创建一个既可以通过电子邮件发送又可以存储消息的联系表单。我想我得到了 model.pyforms.pyadmin.py,并且我能够创建和存储(不是电子邮件) 来自管理员的消息。但是我正在努力处理必须同时通过电子邮件发送和存储消息的 view.py

模型.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')

表单.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 中呈现联系表单。

所以,我需要帮助来找出正确的viewtemplate 代码。我当然也乐于接受有关其余代码的建议 - As你可能猜到了,这是我的第一个真正的 Django 应用程序。

谢谢!

最佳答案

像这样:


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

class ContactForm(forms.ModelForm):

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

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

和实际 View 的 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

关于python - 使用 ModelForm 的 Django 联系表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28975428/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com