表单提交后Django表单中缺少可变错误

时间:2016-11-28 07:37:25

标签: django django-forms django-views

我在我网站的页脚区域添加了一个简报注册表单,因此我不能使用inclus_tag,因为我无法将其绑定到视图。它运作良好并且符合预期,但我发生了一件奇怪的事情,我显然不够聪明,无法弄清楚自己:)

提交表单后,我会收到确认电子邮件,但有两件事情发生:

  1. 直到我手动刷新页面后才会显示Django成功消息。
  2. 我的表单所在的位置有语法'Missing Variable'错误。我提供了一个截图供参考,我的表单代码如下。重新刷新页面后,表单字段将重新显示并且错误消失。
  3. enter image description here

    home_tags.py

    @register.inclusion_tag('pages/tags/footer_newsletter_signup.html', takes_context=True)
    def footer_newsletter_signup(context):
        request = context['request']
        title = 'Newsletter Signup'
        form = MailingListForm(request.POST or None)
    
        if form.is_valid():
            mailing_list_full_name = form.cleaned_data.get('mailing_list_full_name')
            mailing_list_phone = form.cleaned_data.get('mailing_list_phone')
            mailing_list_email = form.cleaned_data.get('mailing_list_email')
            mailing_list_subject = 'Submission from Newsletter Signup'
            mailing_list_message = 'Yes, please add me to marketing emails.'
            from_email = settings.DEFAULT_FROM_EMAIL
            recipient_list = [from_email, 'charles@studiorooster.com']
            ctx = {
                'mailing_list_subject': mailing_list_subject,
                'mailing_list_full_name': mailing_list_full_name,
                'mailing_list_email': mailing_list_email,
                'mailing_list_phone': mailing_list_phone,
                'mailing_list_message': mailing_list_message
            }
    
            message = get_template('pages/newsletter_signup_email.html').render(Context(ctx))
            msg = EmailMessage(mailing_list_subject, message, to=recipient_list, from_email=from_email)
            msg.content_subtype = 'html'
            msg.send()
    
            messages.success(request, "Thank you, you've been added to our list.")
            return HttpResponse('/')
    
        context = {
            'form': form,
            'title': title,
        }
        return context
    

    footer_newsletter_signup.html

    <form action='' method='POST' role='form' class="form-inline">
      {% csrf_token %}
      <div class="form-group">
        {{ form.mailing_list_full_name }}
      </div>
      <div class="form-group">
        {{ form.mailing_list_phone }}
      </div>
      <div class="form-group">
        {{ form.mailing_list_email }}
      </div>
      <button class="button button-lg button-square button-pasific hover-ripple-out" type='submit'>Subscribe</button>
    </form>
    

    然后我只需将标签添加到我的模板中,如:

    {% footer_newsletter_signup %}
    

1 个答案:

答案 0 :(得分:1)

回答这个

  

好的,所以这里是我感到困惑的地方。我有十几个视图,这个表单是一个位于页脚顶部的Call-to-Action表单。如何在不重复代码的情况下将此表单绑定到每个视图?谢谢你的帮助。

您需要创建单独的视图来处理此表单,并在action标记中提供指向此视图的form参数。

这是一般性的想法,代码我不工作

#template
<form action='{% url "send-mail" %}' method='POST' role='form' class="form-inline">
  ...
#views
def send_mail(request):
    form = MailingListForm(request.POST or None)
    if request.method == 'POST':
        if form.is_valid():
            mailing_list_full_name = form.cleaned_data.get('mailing_list_full_name')
            mailing_list_phone = form.cleaned_data.get('mailing_list_phone')
            mailing_list_email = form.cleaned_data.get('mailing_list_email')
            mailing_list_subject = 'Submission from Newsletter Signup'
            mailing_list_message = 'Yes, please add me to marketing emails.'
            from_email = settings.DEFAULT_FROM_EMAIL
            recipient_list = [from_email, 'charles@studiorooster.com']
            ctx = {
                'mailing_list_subject': mailing_list_subject,
                'mailing_list_full_name': mailing_list_full_name,
                'mailing_list_email': mailing_list_email,
                'mailing_list_phone': mailing_list_phone,
                'mailing_list_message': mailing_list_message
            }

            message = get_template('pages/newsletter_signup_email.html').render(Context(ctx))
            msg = EmailMessage(mailing_list_subject, message, to=recipient_list, from_email=from_email)
            msg.content_subtype = 'html'
            msg.send()

            messages.success(request, "Thank you, you've been added to our list.")
            return HttpResponse('/')

#tags
@register.inclusion_tag('pages/tags/footer_newsletter_signup.html', takes_context=True)
def footer_newsletter_signup(context):
    title = 'Newsletter Signup'
    form = MailingListForm()
    context = {
        'form': form,
        'title': title,
    }
    return context

#url
url('r^send-mail/$', send_mail, name='send-email')