【问题标题】:Django contact form doesn't show upDjango联系表不显示
【发布时间】:2014-03-20 04:33:16
【问题描述】:

我正在尝试制作 django 教程中的联系表格,但不幸的是,该表格没有显示为 {{ form }}。唯一出现的是提交按钮。您可以在下面找到代码:

forms.py

from django import forms
# Create your forms here.

class ContactForm(forms.Form):
    name = forms.CharField()
    email = forms.EmailField()
    message = forms.CharField(widget=forms.Textarea)

views.py

from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.core.mail import send_mail
from contact.forms import ContactForm
# Create your views here.

def contact(request):
    if request.method == 'POST':
        form = ContactForm(request.POST)
        if form.is_valid():
            name = form.cleaned_data['name']
            email = form.cleaned_data['email']
            message = form.cleaned_data['message']

            recipients = ['myemail@outlook.com']

            send_mail(name, email, message, recipients)
            return HttpResponseRedirect(reverse('contact-thanks'))
    else:
        form = ContactForm()
    return render(request, 'contact/contact-form.html', {
        'form': form,
    })

contact-form.html

<form action="/contact/" method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit" />
</form>

【问题讨论】:

  • 这意味着您的视图仅适用于呈现 html 页面但没有 'from' ,所以现在您应该测试一些东西,return render(request, 'contact/contact-form.html', { 'form ': form,'test':'this is for testing purpose', }) and in your html page print {{test}}
  • 好像你的表单没有初始化。尝试form = ContactForm(),然后尝试if request.method 不带else 的部分,看看效果如何。
  • @hizbul25 仍然没有显示“这是用于测试目的”!
  • @sagarchalise 发生同样的事情,表单不显示。
  • 也许我的 urls.py @hizbul25 有问题? url(r'^$', TemplateView.as_view(template_name='contact/contact-form.html'), name="contact")

标签: django forms contact


【解决方案1】:

在 url 中使用 template_name 不是一个好习惯。
试试这个:

url(r'^/contact$','appname.views.contact',name='contact')

【讨论】:

  • 搞糊涂了!这与我发布的答案相同吗?
  • 只需更改您的网址。但一切可能都很好。
  • 但是这个 urls.py 在应用程序联系人中,并且应用程序“联系人”的 url 已经是“联系人”,所以这就是我没有将其添加到联系人/urls.py 的原因。避免使用 url /contact/contact.
【解决方案2】:

这应该可行..

from django.core.context_processors import csrf

def contact(request):
    if request.method == 'POST':
        form = ContactForm(request.POST)
        if form.is_valid():
            name = form.cleaned_data['name']
            email = form.cleaned_data['email']
            message = form.cleaned_data['message']

            recipients = ['myemail@outlook.com']

            send_mail(name, email, message, recipients)
            return HttpResponseRedirect(reverse('contact-thanks'))
    else:
        form = ContactForm()

    args = {'form' : form}
    args.update(csrf(request))
    return render(request, 'contact/contact-form.html', args)

【讨论】:

    【解决方案3】:

    两天后,我终于得到了解决方案。错误出现在 urls.py 文件中。 (所以下次我也会包含 urls.py)谢谢大家的帮助!

    我使用的是 TemplateView:

    url(r'^$', TemplateView.as_view(template_name='contact/contact-form.html'), name="contact-form"),
    

    而不是添加我创建的视图,以便可以显示和处理表单。

    url(r'^$', 'contact.views.contact', name="contact"),
    

    【讨论】:

      猜你喜欢
      • 2020-10-18
      • 1970-01-01
      • 2012-04-15
      • 2012-03-08
      • 2019-12-09
      • 2019-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多