如何在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>
答案 0 :(得分:0)
如果你看看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创建表单。
答案 1 :(得分:0)
正如上面有人建议的那样,你需要了解更多关于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')