【问题标题】:Django Contact Form Inputs Won't Show Up - Only See Submit ButtonDjango 联系表单输入不会显示 - 仅查看提交按钮
【发布时间】:2017-05-07 01:54:24
【问题描述】:

当我尝试在我的个人网页上设置联系表格时,我遇到了一些麻烦。无论出于何种原因,我只能看到提交按钮,其余的输入字段都没有填充。有人可以帮忙吗?谢谢!

电子邮件.html

 {% load static %}
 {% block content %}



 <div id='paddingtop'>
 <div class="row" id="title">
    <div class="col"><img src='{% static 'personal/img/contactme.png' %}' style="height: 100%; width: 100%; max-width: 530px; max-height: 1000px; min-height:500; min-width: 300px;">
    </div>
 </div>
 </div>


 <div class="col"  style='margin-top: -40px;  max-height: 100%; max-width: 100%;'><hr class='underline'></div>

<div class='email'  style='background-color: blue; width: 100%; height: 100%;'>
<form method="post" role="form" action="">
    {% csrf_token %}
    {{ form.as_p }}
    <div class="form-actions">
      <button type="submit" style='background-color: red;'>Send</button>
    </div>
</form>
 </div>



{% endblock %}

forms.py

from django import forms

class ContactForm(forms.Form):
    from_email = forms.EmailField(required=True)
    subject = forms.CharField(required=True)
    message = forms.CharField(widget=forms.Textarea, required=True)

urls.py

from django.conf.urls import include, url
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf import settings
from django.conf.urls.static import static
from . import views

urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^home/$', views.index, name='home'),
    url(r'^header/$', views.header, name='header'),
    url(r'^email/$', views.email, name='email'),
    url(r'^success/$', views.success, name='success'),

   ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

views.py

from django import template
from django.conf import settings
from django.core.mail import send_mail, BadHeaderError
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render, redirect
from .forms import ContactForm


def index(request):
    return render(request, 'personal/home.html')

def test(request):
 return render(request, 'personal/test.html')

def header(request):
 return render(request, 'personal/header.html')


# send_email/views.py
def email(request):
    if request.method == 'GET':
        form = ContactForm()
    else:
        form = ContactForm(request.POST)
        if form.is_valid():
            subject = form.cleaned_data['subject']
            from_email = form.cleaned_data['from_email']
            message = form.cleaned_data['message']
            try:
                send_mail(subject, message, from_email, ['admin@example.com'])
            except BadHeaderError:
                return HttpResponse('Invalid header found.')
            return redirect('success')
    return render(request, "email.html", {'form': form})


def success(request):
    return HttpResponse('Success! Thank you for your message.')

【问题讨论】:

  • 没有错误报告?
  • 没有,没有收到
  • 尝试{{ form.fieldname }} 看看是否可以呈现个别字段。
  • 您的 email.html 在哪里?意思是你能给出模板的目录结构吗?

标签: python django forms contact


【解决方案1】:

您使用的是 Bootstrap 2 吗? form-actions 类存在于 Bootstrap 2 中,不存在于 Bootstrap 3 中。另外,我不知道 col 类是什么。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-24
    • 1970-01-01
    • 2016-03-14
    • 2018-02-12
    • 1970-01-01
    • 2020-02-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多