我想在单击Submit
并在此html中显示数据时将数据添加到数据库中。但是,当我单击时,URL将在之后添加/ submit_skill /,因此当我想添加更多数据时,我将在URL之前返回。
我想添加数据,显示数据而不更改URL。
这是我的代码
HTML:
{% if messages %}
<ul class="messages">
{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
<form action="submit_skill/" method="POST" enctype=multipart/form-data>
{% csrf_token %}
Add Skill : <input type="text" name="skill_name" placeholder="Skill">
Select Skill Type :
<select class="" name="skill_type">
<option value="1">Developer</option>
<option value="2">Network</option>
<option value="3">System</option>
<option value="4">Database Analysis</option>
</select>
<button type="submit" name="button" id="btnAdd" >Add</button>
</form>
// show Data in the new row of the tabel
<br>
<table border="3">
<thead>
<th>Skill Name</th>
<th>Skill Type</th>
</thead>
<tbody>
{% for skill in Skill %}
<tr>
<td>{{skill.skill_name}}</td>
<td>{{skill.skill_type}}</td>
</tr>
{% endfor%}
</tbody>
</table>
view.py
def submit_skill(request):
skill_name = request.POST["skill_name"]
skill_type = request.POST["skill_type"]
if skill_name:
skill = Skill(skill_name=skill_name,skill_type=skill_type)
skill.save()
else:
messages.error(request, all_skill)
return HttpResponseRedirect(reverse("index"))
all_skill = Skill.objects.all()
context = {
'Skill': all_skill,
}
template = loader.get_template("addskill.html")
return HttpResponse(template.render(context, request))
我尝试使用HttpResponseRedirect/reverse
,但我不知道发送上下文以显示数据。
请指导我如何使用T ^ T