不同视图中的共享Django联系表单

时间:2016-01-21 17:20:38

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

我本来可以发誓这是在过去工作,但显然我没有像我应该的那样进行测试。我有一个表单,允许用户请求有关特定房地产列表的信息。有四(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})

1 个答案:

答案 0 :(得分:1)

Class-based views(和some neat Django 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'