---
slug: "CsvExportViewBase"
title: "Django SJISのCSVをダウンロードさせるビュー基底クラス"
description: "Django SJISのCSVをダウンロードさせるビュー基底クラス"
url: "https://www.ytyng.com/blog/CsvExportViewBase"
publish_date: "2017-05-09T08:49:33Z"
created: "2017-05-09T08:49:33Z"
updated: "2026-02-27T04:48:45.746Z"
categories: ["Django"]
keywords: ""
featured_image_url: "https://media.ytyng.com/resize/20230812/51b132e076af479d93b543c95aa0ab5a.png.webp?width=768"
has_video: false
has_music: false
video_urls: []
music_urls: []
lang: "ja"
---

# Django SJISのCSVをダウンロードさせるビュー基底クラス

<p>Django SJISのCSVをダウンロードさせるビュー基底クラス</p>
<pre style="background-color: #ffffff; color: #000000; font-family: 'Menlo'; font-size: 9.0pt;"><span style="color: #808080; font-style: italic;"># -*- coding: utf-8 -*-<br /></span><span style="color: #808080; font-style: italic;"><br /></span><span style="color: #000080; font-weight: bold;">from </span>__future__ <span style="color: #000080; font-weight: bold;">import </span>unicode_literals, print_function<br /><br /><span style="color: #000080; font-weight: bold;">import </span>csv<br /><span style="color: #000080; font-weight: bold;">import </span>io<br /><span style="color: #000080; font-weight: bold;">from </span>django.http <span style="color: #000080; font-weight: bold;">import </span>HttpResponse<br /><span style="color: #000080; font-weight: bold;">from </span>django.views.generic <span style="color: #000080; font-weight: bold;">import </span>View<br /><br /><br /><span style="color: #000080; font-weight: bold;">class </span>CsvExportViewBase(View):<br />    output_as_sjis = <span style="color: #000080;">True<br /></span><span style="color: #000080;"><br /></span><span style="color: #000080;">    </span><span style="color: #000080; font-weight: bold;">def </span>get(<span style="color: #94558d;">self</span>, *args, **kwargs):<br />        <span style="color: #000080; font-weight: bold;">if </span><span style="color: #94558d;">self</span>.output_as_sjis:<br />            <span style="color: #000080; font-weight: bold;">return </span><span style="color: #94558d;">self</span>._get_response_as_sjis(*args, **kwargs)<br /><br />        response = HttpResponse(<span style="color: #660099;">content_type</span>=<span style="color: #008080; font-weight: bold;">'text/csv'</span>)<br />        response[<span style="color: #008080; font-weight: bold;">'Content-Disposition'</span>] = \<br />            <span style="color: #008080; font-weight: bold;">'attachment; filename={}'</span>.format(<span style="color: #94558d;">self</span>.get_filename())<br />        writer = csv.writer(response)<br /><br />        <span style="color: #000080; font-weight: bold;">for </span>r <span style="color: #000080; font-weight: bold;">in </span><span style="color: #94558d;">self</span>.get_result_rows():<br />            writer.writerow(r)<br />        <span style="color: #000080; font-weight: bold;">return </span>response<br /><br />    <span style="color: #000080; font-weight: bold;">def </span>_get_response_as_sjis(<span style="color: #94558d;">self</span>, <span style="color: #808080;">*args</span>, <span style="color: #808080;">**kwargs</span>):<br />        response = HttpResponse(<span style="color: #660099;">content_type</span>=<span style="color: #008080; font-weight: bold;">'text/csv; charset=Shift-JIS'</span>)<br />        response[<span style="color: #008080; font-weight: bold;">'Content-Disposition'</span>] = \<br />            <span style="color: #008080; font-weight: bold;">'attachment; filename={}'</span>.format(<span style="color: #94558d;">self</span>.get_filename())<br />        sio = io.StringIO()<br />        writer = csv.writer(sio)<br /><br />        <span style="color: #000080; font-weight: bold;">for </span>r <span style="color: #000080; font-weight: bold;">in </span><span style="color: #94558d;">self</span>.get_result_rows():<br />            writer.writerow(r)<br /><br />        response.write(sio.getvalue().encode(<span style="color: #660099;">encoding</span>=<span style="color: #008080; font-weight: bold;">'cp932'</span>))<br />        <span style="color: #000080; font-weight: bold;">return </span>response<br /><br />    <span style="color: #000080; font-weight: bold;">def </span>get_connection(<span style="color: #94558d;">self</span>):<br />        <span style="color: #000080; font-weight: bold;">from </span>django.db <span style="color: #000080; font-weight: bold;">import </span>connection<br />        <span style="color: #000080; font-weight: bold;">return </span>connection<br /><br />    <span style="color: #000080; font-weight: bold;">def </span>get_cursor(<span style="color: #94558d;">self</span>):<br />        connection = <span style="color: #94558d;">self</span>.get_connection()<br />        <span style="color: #000080; font-weight: bold;">return </span>connection.cursor()<br /><br />    <span style="color: #000080; font-weight: bold;">def </span>get_sql(<span style="color: #94558d;">self</span>):<br />        <span style="color: #808080; font-style: italic;">"""<br /></span><span style="color: #808080; font-style: italic;">        Implement required.<br /></span><span style="color: #808080; font-style: italic;">        """<br /></span><span style="color: #808080; font-style: italic;">        </span><span style="color: #000080; font-weight: bold;">return </span><span style="color: #008080; font-weight: bold;">"SELECT NOW()"<br /></span><span style="color: #008080; font-weight: bold;"><br /></span><span style="color: #008080; font-weight: bold;">    </span><span style="color: #000080; font-weight: bold;">def </span>get_sql_args(<span style="color: #94558d;">self</span>):<br />        <span style="color: #000080; font-weight: bold;">return </span>[]<br /><br />    <span style="color: #000080; font-weight: bold;">def </span>row_filter(<span style="color: #94558d;">self</span>, row):<br />        <span style="color: #000080; font-weight: bold;">return </span>row<br /><br />    <span style="color: #000080; font-weight: bold;">def </span>get_result_rows(<span style="color: #94558d;">self</span>):<br /><br />        cursor = <span style="color: #94558d;">self</span>.get_cursor()<br /><br />        cursor.execute(<span style="color: #94558d;">self</span>.get_sql(), *<span style="color: #94558d;">self</span>.get_sql_args())<br /><br />        <span style="color: #808080; font-style: italic;"># Header<br /></span><span style="color: #808080; font-style: italic;">        </span><span style="color: #000080; font-weight: bold;">yield </span><span style="color: #94558d;">self</span>.row_filter(f[<span style="color: #0000ff;">0</span>] <span style="color: #000080; font-weight: bold;">for </span>f <span style="color: #000080; font-weight: bold;">in </span>cursor.description)<br /><br />        <span style="color: #808080; font-style: italic;"># Content<br /></span><span style="color: #808080; font-style: italic;">        </span><span style="color: #000080; font-weight: bold;">for </span>r <span style="color: #000080; font-weight: bold;">in </span>cursor.fetchall():<br />            <span style="color: #000080; font-weight: bold;">yield </span><span style="color: #94558d;">self</span>.row_filter(r)<br /><br />    <span style="color: #000080; font-weight: bold;">def </span>get_filename(<span style="color: #94558d;">self</span>):<br />        <span style="color: #000080; font-weight: bold;">return </span><span style="color: #008080; font-weight: bold;">"items.csv"<br /></span></pre>
<p></p>
<p></p>
<p></p>
