我使用"models.objects.all()" 會變成全部資料都撈出來並存檔(這不是正確結果);
改用"models.objects.filter()"則會報錯(值太多未整理?!),錯誤如下圖
Error
想請教各位,這要怎麼修改才能得到我要的正確結果?(想對指定篩選完的"學號"資料並下載)
謝謝各位!!
Models:
class CourseData(models.Model):
sid = models.CharField(max_length=20, verbose_name="學號")
name = models.CharField(max_length=20, verbose_name="姓名")
phone = models.CharField(max_length=20, verbose_name="手機")
email = models.CharField(max_length=100, verbose_name="信箱")
sdept = models.CharField(max_length=50, verbose_name="學生系所")
scls = models.CharField(max_length=50, verbose_name="班級")
scampus = models.CharField(max_length=20, verbose_name="校區")
cid = models.CharField(max_length=20, verbose_name="選課代號")
ctype = models.CharField(max_length=20, verbose_name="學制")
cdept = models.CharField(max_length=50, verbose_name="開課系所")
ccls = models.CharField(max_length=50, verbose_name="開課班級")
ccampus = models.CharField(max_length=20, verbose_name="上課校區")
teacher = models.CharField(max_length=20, verbose_name="上課教師")
cdeptemail = models.CharField(max_length=100, verbose_name="開課單位信箱")
teacheremail = models.CharField(max_length=100, verbose_name="上課教師信箱")
ctime = models.CharField(max_length=50, verbose_name="上課時間")
place = models.CharField(max_length=20, verbose_name="上課教室")
subject = models.CharField(max_length=50, verbose_name="科目")
mcls = models.CharField(max_length=50, verbose_name="合班班級")
snum = models.PositiveIntegerField(default=0, verbose_name="人數 ")
duration = models.CharField(max_length=50, verbose_name="假期")
def __str__(self):
return self.name
Templates:
{% extends "base.html" %}
{% block headmessage %}查詢系統{% endblock %}
{% block main-content %}
<hr>
<script>
function changesid(x) {
window.location = "/bysid/"+x.value+"/";
}
function changedepart(x) {
window.location = "/byssdepart/"+x.value+"/";
}
</script>
<form action="/bysid/" method="POST">
{% csrf_token %}
學號查詢:<input name="sid" size="50" placeholder="Search">
<input type="submit" value="查詢">
學生系所查詢:
<select style="width: 200px" onchange="changedepart(this)">
<option value=0>請選擇系所</option>
{% for dept in sdepartments %}
<option value={{ dept.id }}>{{ dept.name }}</option>
{% endfor %}
</select>
</form>
<br>
{% csrf_token %}
{{form}}
<button onclick="window.location.href='/down_file'" >下載檔案</button>
<hr>
以下共顯示{{ num }}筆資料:
<table class="table table-striped table-hover table-xxl text-nowrap">
<tr>
<td>學號</td><td>姓名</td><td>手機</td><td>學生信箱</td><td>學生系所</td>
<td>學生班級</td><td>學生校區</td><td>選課代號</td><td>開課學制</td><td>開課系所</td>
<td>開課班級</td><td>上課校區</td>
</tr>
{% for c in course %}
<tr>
<td><small>{{c.sid}}</small></td> <!--學號-->
<td><small>{{c.name}}</small></td> <!--學生姓名-->
<td><small>{{c.phone}}</small></td> <!--手機-->
<td><small>{{c.email}}</small></td><!--學生信箱-->
<td><small>{{c.sdept}}</small></td><!--學生系所-->
<td><small>{{c.scls}}</small></td><!--學生班級-->
<td><small>{{c.scampus}}</small></td> <!--學生校區-->
<td><small>{{c.cid}}</small></td> <!--選課代號-->
<td><small>{{c.ctype}}</small></td> <!--開課學制-->
<td><small>{{c.cdept}}</small></td><!--開課系所-->
<td><small>{{c.ccls}}</small></td><!--開課班級-->
<td><small>{{c.ccampus}}</small></td><!--上課校區-->
</tr>
{% endfor %}
</table>
{% endblock %}
Views:
def down_file(request):
sdepartments = Department.objects.all()
cdepartments = CDepartment.objects.all()
ssid = CourseData.objects.all()
sops = CourseData.objects.all().values_list("sid","name","phone","email","sdept","scls","scampus","cid","ctype","cdept","ccls","ccampus")
response = HttpResponse(content_type="text/csv")
response.write(codecs.BOM_UTF8)
response['Content-Disposition'] = 'attachment; filename = "Report.csv"'
writer = csv.writer(response)
writer.writerow(["學號","學生姓名","手機","學生信箱","學生系所","學生班級","學生校區","選課代號","開課學制","開課系所","開課班級","上課校區"])
for sop in sops:
writer.writerow(sop)
return response
畫面: