Django表单无法正确呈现

时间:2017-09-06 13:08:46

标签: python django django-models django-forms django-templates

我正在关注Django Forms的文档,但我不知道为什么我的表单不想显示! 我正在创建一个表单,可以通过电子邮件创建邀请,以便用户使用此应用登录:https://github.com/bee-keeper/django-invitations

我的forms.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})

我的Views.py:

{% 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>

我的HTML:

urlpatterns = [
    url(r'^candidateIndex/$', views.candidateIndex.as_view(), name='candidate_index'),
    url(r'^HRIndex/$', views.HRIndex.as_view(), name='HR_index'),
]

urls.py: 来自django.conf.urls import url 来自网站导入视图

app_name =&#39;网站&#39;

viewDidload

当它呈现页面时,我只得到按钮,但表单似乎不起作用 你有什么想法吗?

1 个答案:

答案 0 :(得分:1)

HR_index视图正在处理您HRIndex个网址,但是没有任何代码可以处理该表单。

url(r'^HRIndex/$', views.HRIndex.as_view(), name='HR_index'),

由于TemplateView并不适合处理表单,因此最好修改网址格式以改为使用create_invite视图:

url(r'^HRIndex/$', views.create_invite, name='HR_index'),