Javascript 我正在开发一个测验应用程序,并试图与django一起调整对一个问题的不同回答
我正在开发一个测验应用程序,并试图用python django恢复对一个问题的冷淡回答;但它不起作用 需要帮忙吗 这是我的views.py,当我尝试添加一个问题的不同回答时;唯一插入的是最后一个Javascript 我正在开发一个测验应用程序,并试图与django一起调整对一个问题的不同回答,javascript,python,html,django,Javascript,Python,Html,Django,我正在开发一个测验应用程序,并试图用python django恢复对一个问题的冷淡回答;但它不起作用 需要帮忙吗 这是我的views.py,当我尝试添加一个问题的不同回答时;唯一插入的是最后一个 def add_question_form_submission(request): print("hello form is submitted.") Commentaire= request.POST["Commentaire"] questions = request.PO
def add_question_form_submission(request):
print("hello form is submitted.")
Commentaire= request.POST["Commentaire"]
questions = request.POST["question"]
matiere= request.POST["matiere"]
dif = request.POST["dif"]
reponses= request.POST["reponse"]
comment = request.POST["comment"]
flag = request.POST["flag"]
ques=question(questionA=questions,explication=Commentaire, difficulte=dif,id_matiere=matiere)
ques.save()
rep =reponse(response=reponses,commentaire=comment,estVrai=flag,id_question=ques.id_question)
rep.save()
return render(request,"myblog/questionnaire.html")
这是我的javascript模板,我使用javascript添加了更多字段,以便添加更多问题的回答
{% extends "myblog/base.html" %}
{% block content %}
<html>
<head>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script>
$(document).ready(function() {
var max_fields = 10; //maximum input boxes allowed
var wrapper = $(".input_fields_wrap"); //Fields wrapper
var add_button = $(".add_field_button"); //Add button ID
var x = 1; //initlal text box count
$(add_button).click(function(e){ //on add input button click
e.preventDefault();
if(x < max_fields){ //max input box allowed
x++; //text box increment
$(wrapper).append('<div><input placeholder="reponse" type="text" name="reponse"/> <input placeholder="Comment" align="center" type="text" name="comment"><select name="flag"> <option value=True>Vrai</option><option value=False>Faux</option></select> <a href="#" class="remove_field">Remove</a></div>'); //add input box
}
});
$(wrapper).on("click",".remove_field", function(e){ //user click on remove text
e.preventDefault(); $(this).parent('div').remove(); x--;
})
});
</script>
</div>
</div>
<div align="center">
<div align="center">
<h3 align="center">ajouter questions</h3></br>
<form method="post" action="add_question_form_submission">
{% csrf_token %}
<input placeholder="question" align="center" type="text" name="question"> <select name="matiere"><option value=" ">matiere...</option> {% for mati in mat %}
<option value={{mati.idmatiere}}>
<p> {{mati.nomMatiere }}</p>
</option> {% endfor %}
</select>
<input placeholder="Commentaire" align="center" type="text" name="Commentaire">
<select name="dif"><option value="">Niveau...</option>
<option value=1>facile</option>
<option value=2>intermediaire</option>
<option value=3>difficile</option>
</select></br></br>
<div class="input_fields_wrap"></br>
<button class="add_field_button">Add More Fields</button>
<div><input placeholder ="reponse" type="text" name="reponse">
<input placeholder="Comment" align="center" type="text" name="comment">
<select name="flag">
<option value=True>Vrai</option>
<option value=False>Faux</option>
</select>
</br></br></div></div>
</br></br>
<input align="center" type="submit" value="ajouter">
</form>
{% endblock content %}
{%extends“myblog/base.html”%}
{%block content%}
$(文档).ready(函数(){
var max_fields=10;//允许的最大输入框数
var wrapper=$(“.input_fields_wrapp”);//字段包装器
var add_button=$(“.add_字段_button”);//添加按钮ID
var x=1;//初始文本框计数
$(添加按钮)。单击(函数(e){//在添加输入按钮上单击
e、 预防默认值();
如果(x
{%csrf_令牌%}
马蒂埃。。。{mat%中的mati为%1}
{{mati.nomMatiere}
{%endfor%}
Niveau。。。
轻率的
中间人
难以捉摸
添加更多字段
弗拉伊
人造的
{%endblock内容%}
您需要将响应
字段设置为数组。您可以通过在name属性中添加这样的开始方括号和结束方括号来完成此操作:[]
例如:
然后,在服务器端,您可以使用reponse
字段中的数据作为数组,遍历项目并将其插入数据库
我对Django没有事先的了解。所以,我能给你的就是这些建议。希望你有这个想法
<input placeholder ="reponse" type="text" name="reponse[]">