【发布时间】:2017-12-05 04:40:27
【问题描述】:
我的网页由两部分组成,上半部分是让用户输入数据的部分,而下半部分以表格形式显示数据库中的所有数据。当用户选择上部的“添加”按钮时,数据将被保存到数据库中,并在网页底部的表格中输出。一旦我选择“添加”按钮,是否有显示表格? Right now what the code is doing is when "Add" button is being selected, it will load a new form but then the whole table will disappear.我必须再次手动输入此地址“http://127.0.0.1:8000/hrfinance/lscholarship/”,然后才会出现表格。即使刷新页面也不起作用。以下是我在
中的代码views.py:
def scholarship(request, id=None):
query_results = []
if request.method == "POST":
form = ScholarshipForm(request.POST)
if form.is_valid():
scholarship = form.save(commit=False)
scholarship.save()
else:
form = ScholarshipForm()
id = request.GET.get('scholarship')
query_results = Scholarship.objects.all()
data = {
'query_results':query_results,
'form': form
}
return render(request, 'hrfinance/add_remove_scholarship.html', data)
models.py
class Application(models.Model):
studentID = models.CharField("Student ID", max_length=8, validators=[MinLengthValidator(8)], primary_key=True, default="")
studentName = models.CharField("Student Name", max_length=500, default="")
scholarship = models.TextField("Scholarship")
add_remove_scholarship.html
<div align="center" >
<form method="POST" onsubmit="return validation()" action="">
{% csrf_token %}
{{ form.errors }}
<p>Upload File: {{ form.doc }}</p>
<p>Faculty: {{ form.faculty }} </p>
<p>Opening date: <input id="odate" type="date" name="openDate"> </p>
<p>Closing date: {{ form.closeDate }} </p>
<input type="submit" name="AddScholarship" value="Add Scholarship" >
</form>
</div>
<br></br>
<button id="button" type="button">Delete Selected Scholarship</button>
<br></br>
{{query_results}}
<form method="POST" action="">
{% csrf_token %}
<table id="example" class="display" cellspacing="0" width="100%" border="1.5px">
<tr align="center">
<th> Scholarship </th>
<th> Faculty </th>
<th> Open Date </th>
<th> Close Date </th>
</tr>
{% for item in query_results %}
<tr align="center">
<td>{{item.doc}}</td>
<td>{{item.faculty}}</td>
<td>{{item.openDate}}</td>
<td>{{item.closeDate}}</td>
</tr>
{% endfor %}
</table>
</form>
【问题讨论】: