【问题标题】:creating forms in django - error in output在 django 中创建表单 - 输出错误
【发布时间】:2017-12-20 23:26:24
【问题描述】:

我在各个地方都有以下代码,试图在 Django 中生成一个表单,该表单获取数据并将其写入数据库

views.py

from django.shortcuts import render
from django.http import HttpResponse
from django.http import HttpResponseRedirect

from .forms import TeacherSignup

"""def teachers(request):
    return render(request,'teachers/teachers.html')
"""
def teachers(request):
    # if this is a POST request we need to process the form data
    if request.method == 'POST':
        # create a form instance and populate it with data from the request:
        form = TeacherSignup(request.POST)
        # check whether it's valid:
        if form.is_valid():
            # process the data in form.cleaned_data as required
            # ...
            # redirect to a new URL:
            return HttpResponseRedirect('/thanks/')

    # if a GET (or any other method) we'll create a blank form
    else:
        form = TeacherSignup()

    return render(request, 'teachers/teachers.html', {'form': form})

teachers.html(这是包含表单的 html 页面)

<form action="/teachers/" method="post" style="border:1px solid #ccc">
  {% csrf_token %}
  {{ form }}
  <div class="container">
    <label><b>Email</b></label>
    <input id="email" input type="text" placeholder="Enter Email" name="email" required>

    <label><b>Centre/School Name</b></label>
    <input id="school_name" input type="text" placeholder="Enter School or Centre Name" name="school_name" required>

    <label><b>Password</b></label>
    <input id="password" input type="password" placeholder="Enter Password" name="password" required>

    <label><b>Repeat Password</b></label>
    <input id="password_repeat" input type="password" placeholder="Repeat Password" name="password_repeat" required>
    <input type="checkbox" checked="checked"> Remember me
    <p>By creating an account you agree to our <a href="#">Terms & Privacy</a>.</p>



    <div class="clearfix">
      <button type="button" class="cancelbtn">Cancel</button>
      <button type="submit" class="signupbtn">Sign Up</button>
    </div>
  </div>
</form>

</body>
</html>

forms.py

from django import forms

class TeacherSignup(forms.Form):
    email=forms.CharField(label='email',max_length=100)
    school_name=forms.CharField(label='school_name',max_length=100)
    password=forms.CharField(label='password',max_length=100)
    password_repeat=forms.CharField(label='password_repeat',max_length=100)  

然而,在运行服务器时,它会产生两种形式:一种是我在 html 中创建的,另一种可能是在 forms.py 中创建的。

如何使用 Teachers.html 中的表单(位于页面末尾)并使用此创建的表单将数据写入数据库,或者 Django 是否要求我使用 forms.py 创建它?

如果可能,请提供详细说明,并提供示例。

【问题讨论】:

  • 为什么要对表单进行硬编码? {{ form }} 为您呈现字段,这就是您有两个表单的原因。

标签: html django forms


【解决方案1】:

在您的模板中,您可以逐个字段地呈现表单

teachers.html

中的类似内容
<form action="/teachers/" method="post" style="border:1px solid #ccc">
  {% csrf_token %}
  <div class="container">
    <label><b>Email</b></label>
    {{ form.email }}

    <label><b>Centre/School Name</b></label>
    {{ form.school_name }}

    <div class="clearfix">
      <button type="button" class="cancelbtn">Cancel</button>
      <button type="submit" class="signupbtn">Sign Up</button>
    </div>
  </div>
</form>

其余的也一样

如果要添加占位符属性,则需要将小部件参数添加到字段中,如果您希望密码字段可以隐藏文本,则相同

forms.py

from django import forms

class TeacherSignup(forms.Form):
    email=forms.CharField(label='email',max_length=100, widget=forms.TextInput(attrs={'placeholder': 'Enter Email'})
    school_name=forms.CharField(label='school_name',max_length=100, widget=forms.TextInput(attrs={'placeholder': 'Enter School or Centre Name'})
    password=forms.CharField(label='password',max_length=100, widget=forms.PasswordInput)
    password_repeat=forms.CharField(label='password_repeat',max_length=100, widget=forms.PasswordInput)  

【讨论】:

  • 不幸的是,每次我尝试检查什么是有效的并运行 makemigrations 我都会收到错误....“警告:?:(urls.w005)URL命名空间'admin'不是唯一的。您可能无法反转此命名空间中的所有 URL。知道这意味着什么吗?
  • 您是否在您的网址文件中多次导入此“管理员”?
猜你喜欢
  • 2018-03-27
  • 2019-06-27
  • 2011-08-06
  • 2013-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-06
  • 2015-04-18
相关资源
最近更新 更多