Django联系表单输入将不会显示 - 仅查看提交按钮

时间:2017-05-07 01:54:24

标签: python django forms contact

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

email.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.')

1 个答案:

答案 0 :(得分:0)

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