iT邦幫忙

0

如何對指定篩選完的資料並下載?

  • 分享至 

  • xImage

我使用"models.objects.all()" 會變成全部資料都撈出來並存檔(這不是正確結果);
改用"models.objects.filter()"則會報錯(值太多未整理?!),錯誤如下圖

Error

00

filter錯誤

Error

Erro2

Error3

Error4

Error5

想請教各位,這要怎麼修改才能得到我要的正確結果?(想對指定篩選完的"學號"資料並下載)

謝謝各位!!

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

畫面:
Fornt

看更多先前的討論...收起先前的討論...
froce iT邦大師 1 級 ‧ 2022-05-12 16:10:42 檢舉
error麻煩給齊全一點...
你給的連哪一行code出錯我都不知道怎麼幫你debug?
謝謝提醒,已經補上了
froce iT邦大師 1 級 ‧ 2022-05-12 23:01:10 檢舉
還是沒看到關鍵的錯誤在哪,你debug訊息沒給完整,請給最後幾個,最後一個應該會跟你說執行到你寫的view哪裡錯,而不是django內部的錯誤。
如果是要查詢執行到寫的view哪裡錯的話,我看訊息是指177行這一段,但我不確定是否為您所指的debug訊息
in down_file
sops = CourseData.objects.filter("sid").values_list("sid","name","phone","email","sdept","scls","scampus","cid","ctype","cdept","ccls","ccampus","teacher","cdeptemail","teacheremail","ctime","place","subject","mcls","snum","duration")
froce iT邦大師 1 級 ‧ 2022-05-13 09:54:04 檢舉
1. debug info你要給到跟你的code有關的,因為很少是別人的套件有問題,通常都是自己搞錯。

2. 啊你CourseData總共也才12個欄位,變成values_list哪來那麼多欄位?
"teacher","cdeptemail","teacheremail","ctime","place","subject","mcls","snum","duration"
這些不是CourseData的東西怎麼讓他變values_list...他也根本沒這些欄位啊

3. 我猜你會需要外鍵
https://docs.djangoproject.com/zh-hans/4.0/topics/db/examples/many_to_one/
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答