gpt4 book ai didi

Django - 使用基于类的 View 发送电子邮件

转载 作者:行者123 更新时间:2023-12-05 09:19:49 27 4
gpt4 key购买 nike

我一直在制作一个包含 ListView 和表单 View 的网站。我有一个 form.py 看起来像这样:

from django import forms

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

和 views.py :

from django.shortcuts import render
from .forms import ContactForm
from django.views.generic import ListView
from django.views.generic.edit import FormMixin
from blog.models import Post
from django.core.mail import EmailMessage

class PostListAndFormView(FormMixin,ListView):
queryset = Post.objects.all().order_by("-date")[:2]
template_name = 'personal/index.html'
form_class = ContactForm
success_url = 'personal/index.html'

此 View 还处理您可以忽略的 ListView 功能。

我的 HTML 模板是:

<form action="/" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="SEND MESSAGE" />
</form>

最后是 urls.py :

from django.conf.urls import url,include
from . import views
from homepage.views import PostListAndFormView

urlpatterns = [
url(r'^$', PostListAndFormView.as_view(), name='PostListAndFormView'),
]

我在这里要做的是从表单中获取数据,然后将输入的数据通过电子邮件发送到我的地址。有人帮我做这个。

最佳答案

不知道为什么要混淆 listview 和 formview。从您的模型来看,它是一种博客应用程序。通常在博客应用程序中,文章/帖子列表页面不包含任何带有电子邮件字段的表单,除了文章详细信息或联系我们页面。我更喜欢将它分成不同的类而不是使用 mixin。

我的答案基于 Daniel Greenfeld 的例子 here稍作修改。

在你的应用程序/forms.py 中:

from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit
import floppyforms as forms

class ContactForm(forms.Form):

name = forms.CharField(required=True)
email = forms.EmailField(required=True)
subject = forms.CharField(required=True)
message = forms.CharField(widget=forms.Textarea)

def __init__(self, *args, **kwargs):
self.helper = FormHelper()
self.helper.add_input(Submit('submit', 'Submit'))
super(ContactForm, self).__init__(*args, **kwargs)

在你的app/views.py中:

from django.views.generic.list import ListView
from django.views.generic.edit import FormView
from django.conf import settings
from django.core.mail import send_mail
from yourapp.models import Post
from yourapp.forms import ContactForm

class PostListView(ListView):

model = Post

def get_context_data(self, **kwargs):
context = super(ArticleListView, self).get_context_data(**kwargs)
context['latest_articles'] = Post.objects.all()[:5]
return context

class ContactFormView(FormView):

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

# this is what you want
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],
)
return super(ContactFormView, self).form_valid(form)

关于Django - 使用基于类的 View 发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39515236/

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