【问题标题】:how to use html input form in django如何在 django 中使用 html 输入表单
【发布时间】:2018-10-28 09:39:15
【问题描述】:

如何在 django 中输入 HTML 表单中的值。我是 django 的新手,正在尝试克隆社交网络。我不确定要为 forms.py、views.py、urls.py 和 models.py 编写什么代码来输入数据。 此外,我还有 html 表单中的各种字段,例如文本框、复选框、单选按钮。 如何将这些字段的数据输入到后端?

html

<form method = 'POST' id = "new_post" action = "/posts/new/">
{% csrf_token %}
<div class="form-group col-md-6">
<label>Title</label><br>
<input type="text" class="form-control" id="inputTitle" placeholder="Enter title">
</div>

<div class="form-group col-md-12">
<label>Situation Description</label><br>
<textarea rows="4" cols="70" class="form-control" placeholder="Enter the situation description"></textarea>

</div>
<div class="form-group col-md-12">
<label>Outcome Description</label><br>
<textarea rows="4" cols="70" class="form-control" placeholder="Enter the outcome description"></textarea>
</div>

<div class="form-group col-md-4">
<label>Outcome Type</label><br>
<select id="inputO_type" class="form-control">
<option>Positive</option>
<option>Neutral</option>
<option>Negative</option>
</select>
</div>

<br>
<div class="form-group col-md-4">
<div class="multiselect">
<div class="selectBox" onclick="showCheckboxes()">

    <option>reviewed?</option><select></select>

  <div class="overSelect"></div>
</div>
<div id="checkboxes">
  <label for="one">
    <input type="checkbox" id="one" />Commissioning</label>
  <label for="two">
    <input type="checkbox" id="two" />Construction</label>
    </div>
     </div></div>

    <div class="form-row">
    <br>
    <div class="form-group col-md-4">
    <label>Tags</label><br>
    <select id="inputTag" class="form-control">

      <option selected>Algorithms</option>
      <option selected>Architecture</option>
      <option selected>Automobile</option>
     <option>...</option>
    </select>
  </div>

  <div class="form-row">
    <div class="form-group col-md-4">
    <form method="post" action="#">

  <div>

  <input type="checkbox" name="FormStuff" id="FormStuff" required>
  <label for="FormStuff">Can't find your tag?</label>
  <div class="reveal-if-active">
   <label for="which-tag">Please fill in your desired tag below</label><br>
    <input type="text" id="which-tag" name="which-tag" class="require-if-active" data-require-pair="#choice-tags-typetags">
    </div>
    <input type="submit" value="Add Tag">
    <form method="post" action="#">
     </div>
    </div>
  </div>

   <br>
   <div class="form-group col-md-4">
  <label>Does this warrant a process improvement action?</label>
    <div class="form-check">
  <input type = "radio" class="form-check-input" value="" class="form-control">
      <label>Yes</label>
    </div>
    <div class="form-check">
  <input type = "radio" class="form-check-input" value="" class="form-control">
      <label>No</label>
    </div>
   </div>


</div>

【问题讨论】:

  • 你试过Django教程练习吗?

标签: python html django django-forms


【解决方案1】:

如果您查看 Django 文档,他们有一个使用 Django 构建表单的特定部分: https://docs.djangoproject.com/en/2.0/topics/forms/

在最简单的上下文中,您的表单类将如下所示:

from django import forms

class NameForm(forms.Form):
    your_name = forms.CharField(label='Your name', max_length=100)

如果您阅读了 django 文档的其余部分,他们会逐步说明如何使用 HTML 和 django 创建表单。

【讨论】:

    【解决方案2】:

    正如上面一些人建议的那样,你需要更多地了解 Django,文档非常易于理解。可以让Django渲染表单,Django Link

    要回答您的问题,您只需将name 属性添加到标签,您就可以在后端检索它,request.POST.get('name') 用于发布请求,request.GET.get('name') 用于获取请求。比如

    <form action='url' method='post'>{% csrf_token %}
    
        <textarea name='description' rows="4" cols="70" class="form-control" placeholder="Enter the situation description"></textarea>
        <select id="inputO_type" name='inputO' class="form-control">
              <option>Positive</option>
              <option>Negative</option>
       </select>
    </form>
    

    在后台:

    if request.method == 'POST':
        description = request.POST.get('description')
        inputO = request.POST.get('inputO')
    

    【讨论】:

      猜你喜欢
      • 2011-01-26
      • 2020-12-16
      • 2020-01-31
      • 2017-04-30
      • 2020-01-23
      • 2018-01-19
      • 2019-12-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多