【发布时间】:2018-02-14 23:59:48
【问题描述】:
我正在关注 Django 表单的文档,但我不知道为什么我的表单不想显示! 我正在创建一个表单,该表单将收到一封电子邮件并为用户创建使用此应用程序登录的邀请:https://github.com/bee-keeper/django-invitations
我的forms.py:
class InviteForm(forms.Form):
email1 = forms.EmailField(label='Email 1')
我的观点.py:
from django.shortcuts import render
from django.views.generic import TemplateView
from .forms import InviteForm
class candidateIndex(TemplateView):
template_name= 'candidateIndex.html'
class HRIndex(TemplateView):
template_name= 'HRindex.html'
def create_invite(request):
if request.method == 'POST':
form = InviteForm(request.POST)
if form.is_valid:
email = form.cleaned_data['email1']
invite = Invitation.create('form.email1')
invite.send_invitation(request)
print("The mail was went")
else:
print("Your form is not valid")
else:
form = InviteForm()
return render(request, 'HRindex.html', {'form': form})
我的 HTML:
{% extends 'base.html' %}
{% block body %}
<div class="jumbotron">
<h1>Welcome to SoftScores.com</h1>
<h2>Team analytics platfom</h2>
<h3>Welcome to {{user.username}}, it is your Page</h3>
</div>
<div class="container">
<p>
<a class="btn btn-primary" data-toggle="collapse" href="#collapseExample" aria-expanded="false" aria-controls="collapseExample">
Create a new team
</a>
</p>
<div class="collapse" id="collapseExample">
<div class="card card-body">
In order to create a new team please invite new members. A link will be sent to them in order to give the access to the application
</div>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit" />
</form>
</div>
</div>
urls.py: 从 django.conf.urls 导入 url 从网站导入视图
app_name = '网站'
urlpatterns = [
url(r'^candidateIndex/$', views.candidateIndex.as_view(), name='candidate_index'),
url(r'^HRIndex/$', views.HRIndex.as_view(), name='HR_index'),
]
当它呈现页面时,我只得到按钮,但表单似乎不起作用 你有什么想法吗?
【问题讨论】:
-
#与问题无关 -
.is_valid是一种方法,而不是一个属性。 -
您试图在哪个 URL 上查看表单?您尚未显示视图
candidateIndex或HRIndex,并且CreateInvite没有 URL 模式。 -
注意大小写。表单中的
Email1与form.cleaned_data['email1']不匹配。我通常希望看到{{ user. username }}而不是{{ User.username }}。 Django 中的常规方法是将字段命名为email1,基于函数的视图命名为create_invite,以及基于类的视图命名为CandidateIndex和HRIndex。 -
感谢 Alasdair 的评论,我修改了 misCapitalisations。我希望我的表单显示在 HRIndex.html 页面上,您所说的 CreateInvite URL 模式是什么意思?奇怪的是我什至没有在我的页面上看到表单.. 只有按钮;(
-
呈现表单的视图是
create_invite(一个函数视图),但您没有使用它的 URL 模式。正如@Alasdair 所问,您在浏览器中输入的 URL 是什么,希望看到该表单?您在views.py中定义了 3 个视图,但在urls.py中只有 2 个 URL 模式。
标签: python django django-models django-forms django-templates