【问题标题】:Django form not rendering properlyDjango 表单无法正确呈现
【发布时间】: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 上查看表单?您尚未显示视图candidateIndexHRIndex,并且CreateInvite 没有 URL 模式。
  • 注意大小写。表单中的Email1form.cleaned_data['email1'] 不匹配。我通常希望看到{{ user. username }} 而不是{{ User.username }}。 Django 中的常规方法是将字段命名为 email1,基于函数的视图命名为 create_invite,以及基于类的视图命名为 CandidateIndexHRIndex
  • 感谢 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


【解决方案1】:

HRIndex 视图正在处理您的 HR_index url,但这没有任何代码来处理表单。

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

由于 TemplateView 并不真正适合处理表单,最好修改 URL 模式以改用 create_invite 视图:

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

【讨论】:

  • Alasdair,非常感谢你 ;) 我可以再问你一个问题吗?现在,当我发布表单时,我收到错误:未定义名称“邀请”。邀请是我使用 pip install 安装的外部 ..
  • 您需要导入模型:from invitations.models import Invitation.
  • Alasdair 如何将我的表单插入其他页面。我又想了想,我需要我的用户创建一个项目,然后在项目详细信息页面上能够创建一个团队.. 但我的 url 看起来像这样:url(r'^project/(?P[ 0-9]+)/$',views.ProjectDetailView.as_view(), name='ProjectDetails'), 我怎样才能将它也添加到那里。 ?谢谢你;)
  • 这是一个不同的问题,所以最好问一个新问题。
猜你喜欢
  • 2022-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-20
  • 2017-04-06
  • 2014-03-19
相关资源
最近更新 更多