【问题标题】:Django form without model not appearing in template when rendered渲染时没有模型的Django表单没有出现在模板中
【发布时间】:2019-08-16 06:57:06
【问题描述】:

我有一个没有关联模型的表单,只是一个用于发送消息的联系表单。

我现在对 django 表单有一些经验,所以我认为我做的一切都是正确的,但是当在浏览器中查看页面时根本没有渲染,也没有任何错误需要解决。

我的forms.py:

from django import forms

class ContactForm(forms.Form):
    class Meta:
        fields = ['full_name', 'phone', 'email', 'message']
        full_name = forms.CharField(max_length=20)
        phone = forms.CharField(max_length=20)
        email = forms.CharField(max_length=30)
        message = forms.CharField(max_length=400)

我的观点是将表单变成有用的东西:

def contact_view(request):
    full_name = request.POST.get('full_name', False)
    phone = request.POST.get('phone', False)
    email = request.POST.get('email', False)
    message = request.POST.get('message', False)
    form = ContactForm()
    if request.method == 'POST':
        form = ContactForm(request.POST)
    if form.is_valid():
#        send_emails(first_name, last_name, email)
        template = loader.get_template('/myapp/mysite/main_page/templates/main_page/thankyoumsg.html')
        return HttpResponse(template.render({}, request))
    template = loader.get_template('/myapp/mysite/main_page/templates/main_page/contact.html')
    return HttpResponse(template.render({}, request))

还有我的模板:

<form class="leave-comment" action="." method="post">
{% csrf_token %}
{{form.as_p}}
    <button type="submit">Submit</button>
</form>

但是什么都没有显示,我不确定为什么。我该如何解决这个问题?

【问题讨论】:

  • 视图看起来有点粗略,因为if form.is_valid(): 很可能会生成True,即使没有发布表单。如果您缩进 if form.is_valid():-part 以包含在 if request.method == 'POST': 中会发生什么?
  • @Johan 说得很好,谢谢。按照您的建议缩进,但没有任何改变:\
  • 如果不出意外,请尝试在模板中打印任何文本(thankyoumsg.htmlcontact.html)并查看显示的页面。另外,如果你查看页面源码,是否可以看到提交按钮等其他表单组件?
  • @johan contact.html 显示正常,我无法提交表单来测试thankyoumsg.html。查看源代码是我尝试的第一件事,是的,提交按钮和打开的表单标签都在那里,只是没有任何字段。

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


【解决方案1】:

class Meta 仅在您拥有 模型 时使用。如果您只需要呈现没有特定模型的表单,请使用这种方式。更多信息请访问官方文档: https://docs.djangoproject.com/en/2.1/topics/forms/

forms.py

class ContactForm(forms.Form):
    full_name = forms.CharField(max_length=20)
    phone = forms.CharField(max_length=20)
    email = forms.CharField(max_length=30)
    message = forms.CharField(max_length=400)

views.py

 def contact_view(request):

    if request.method == 'POST':

       form = ContactForm(request.POST)

       if form.is_valid():

            full_name = form.cleaned_data['full_name']
            phone = form.cleaned_data['phone']
            email = form.cleaned_data['email']
            message = form.cleaned_data['message']

            template = loader.get_template('/myapp/mysite/main_page/templates/main_page/thankyoumsg.html')
            return HttpResponse(template.render({'form': form}, request))

        template = loader.get_template('/myapp/mysite/main_page/templates/main_page/thankyoumsg.html')
        return HttpResponse(template.render({'form': form}, request))

    form = ContactForm()
    template = loader.get_template('/myapp/mysite/main_page/templates/main_page/contact.html')
    return HttpResponse(template.render({'form': form}, request))

【讨论】:

  • 啊,我没有意识到 Meta 类只在我有模型时使用。谢谢你。我使用类 Meta 来使用 forms.TextInput 定义一些属性,现在这不起作用,我想这是一个新问题。
  • @JakeRankin 我更新了我的答案,如果它正确将其标记为有效,以便其他用户可以从中受益。
  • 谢谢@m1009ct0,我投了你的票,但选择 Johan 的答案是最好的,因为他打败了你,让我首先找到了可行的解决方案。不过非常感谢!
  • @m1009ct0 虽然我通常鼓励各种答案,但当您给出与我相同的答案(以及我提出的相同顺序)然后要求标记您的答案时,这似乎很奇怪答案作为给定的解决方案。如果对答案有不同的调整很好,如果你觉得我的答案可以改进,你也可以improve my answer。你可以阅读更多here about asking to accept a solution
【解决方案2】:

您没有在最后一行的响应中包含表单。这应该(可能)起到作用:

def contact_view(request):
    ...
    return HttpResponse(template.render({'form': form}, request))

我也相信您需要将字段直接添加到表单类中,而不是在元类中。

from django import forms

class ContactForm(forms.Form):
    # Move out the fields here instead
    full_name = forms.CharField(max_length=20)
    phone = forms.CharField(max_length=20)
    email = forms.CharField(max_length=30)
    message = forms.CharField(max_length=400)

    class Meta:
        # This may still be there but may also be a bit redundant since
        # you're choosing to show all applied fields.
        fields = ['full_name', 'phone', 'email', 'message']

【讨论】:

  • 感谢您的回答。但是,在查看呈现页面的源或任何其他行为时,我尝试了这个,但仍然没有改变:\
  • 啊,那很可能在视图中。尝试在HttpResponse 中将表单添加到上下文中,最后一行:return HttpResponse(template.render({'form': form}, request))
  • 就是这样!谢谢
  • @JakeRankin 干杯没问题!
猜你喜欢
  • 1970-01-01
  • 2019-05-15
  • 2016-01-05
  • 2020-01-17
  • 2016-08-23
  • 1970-01-01
  • 2020-10-17
  • 2020-08-27
  • 1970-01-01
相关资源
最近更新 更多