【问题标题】:Shared Django contact form across different views跨不同视图共享 Django 联系表
【发布时间】:2016-01-21 17:20:38
【问题描述】:

我本可以发誓这在过去是有效的,但显然我没有进行应有的测试。我有一个表格,允许用户请求有关特定房地产列表的信息。有四 (4) 个单独的视图,一个显示全部,另外三个仅按属性过滤列表,即租赁、仅房地产等。

表单的工作方式与在视图中列出 objects.all() 的方式一样,我想我可以在其他视图中调用 form_class,但是当我在过滤的页面上时,表单不起作用...浏览器或任何内容中没有错误消息,电子邮件永远不会到达我们的收件箱。

如果您可以在下面查看我的观点并提供一些反馈,我将非常感激,如果您需要查看其他任何内容,请告诉我。谢谢。

from django.template.loader import get_template
from django.core.mail import EmailMessage
from django.template import Context
from django.shortcuts import render, redirect
from .models import Listing
from .forms import ContactForm


def listing_list(request):
    listings = Listing.objects.all().order_by('listing_order')
    form_class = ContactForm

    if request.method == 'POST':
        form = form_class(data=request.POST)

        if form.is_valid():
            contact_name = request.POST.get('contact_name', '')
            contact_phone = request.POST.get('contact_phone', '')
            contact_email = request.POST.get('contact_email', '')
            contact_listing = request.POST.get('contact_listing', '')
            form_content = request.POST.get('content', '')

            # Email the profile with the
            # contact information
            template = get_template('contact_template.txt')
            context = Context({
                'contact_name': contact_name,
                'contact_phone': contact_phone,
                'contact_email': contact_email,
                'contact_listing': contact_listing,
                'form_content': form_content,
            })
            content = template.render(context)

            email = EmailMessage("Form Submission from Listings Page", content, contact_email,
                                 ['to1@emailaddress.com', 'to2@emailaddress.com'], ['bcc@emailaddress.com'])

            email.send()

            return redirect('listing_list')
    return render(request, 'listing/listing_list.html', {'listings': listings, 'form': form_class})


def full_service(request):
    listings = Listing.objects.filter(location_type='FULL_SERVICE').order_by('listing_order')
    form_class = ContactForm
    return render(request, 'listing/listing_list.html', {'listings': listings, 'form': form_class})


def quick_serve(request):
    listings = Listing.objects.filter(location_type='QUICK_SERVE').order_by('listing_order')
    form_class = ContactForm
    return render(request, 'listing/listing_list.html', {'listings': listings, 'form': form_class})


def with_real(request):
    listings = Listing.objects.filter(location_type='WITH_REAL').order_by('listing_order')
    form_class = ContactForm
    return render(request, 'listing/listing_list.html', {'listings': listings, 'form': form_class})

【问题讨论】:

  • 在其他 3 个视图中,您没有 form.is_valid 整个块来实际处理表单并发送电子邮件......或者我错过了什么?只需将其作为 mixin 并将其包含到每个视图中即可。此外,整个设计也很糟糕,您必须将代码复制到每个视图中,并使用不同的location_type
  • 感谢您的快速反馈和使用 mixins 的参考。是的,我可耻地将我的脏代码放在这里供大家查看 :) 我知道它可能会好得多,但我仍在通过学习 Python/Django 来破解我的方式,并且会随着我的前进而折射。谢谢。
  • 我通常使用基于类的视图,所以我使用了 mixin,因为这是我想到的第一件事,但是您可以将普通函数作为视图,但只需将其全部设为一个函数即可参数。

标签: django django-models django-forms django-templates django-views


【解决方案1】:

Class-based views(和someneatDjango shortcuts)来救援:

from django.core.mail import send_mail
from django.views import generic
from django.template import render_to_string


class ContactFormView(generic.FormView):
    form_class = ContactForm
    template_name = 'listing/listing_list.html'
    success_url = 'listings'
    location_type = None

    def get_context_data(self, **kwargs):
        context = super(ContactFormView, self).get_context_data(**kwargs)
        context['listings'] = (Listing.objects
                               .filter(location_type=self.location_type)
                               .order_by('listing_order'))
        return context

    def form_valid(self, form):
        message = render_to_string('contact_template.txt', form.cleaned_data)
        send_mail('Form Submission from Listings Page', message,
                  form.cleaned_data['contact_email'])
        return super(ContactFormView, self).form_valid(form)


class FullService(ContactFormView):
    location_type='FULL_SERVICE'


class QuickServe(ContactFormView):
    location_type='QUICK_SERVE'

【讨论】:

  • 哇,这比我所拥有的要干净得多.... 感谢您提供指向快捷方式和 render_to_string 的直接链接。我没有使用 send_mail,所以我需要重新使用 SendGrid,但这应该不会太难。再次感谢。
  • 不客气。顺便说一句,如果您使用的是 Sendgrid,则无需更改任何内容,只需为您的项目添加一个后端:github.com/elbuo8/sendgrid-django
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-13
相关资源
最近更新 更多