顧客情報管理システムを作成しています。
今回、顧客詳細画面(custom_detail.html)に
出力ボタンを押下すると画面に出力されているデータをエクセルに出力して
ダウンロードしたいと思っています。
ただ、出力ボタンを押下すると以下のメッセージが出力されます。
'NameError at /hoge/download/ name 'HttpResponse' is not defined'
エラー発生箇所は以下になります。
view
1response = HttpResponse(content_type='application/vnd.ms-excel')
ファイルをダウンロードするための定義処理の部分でエラーになっているようなのですが
何故こうなるのかが分かりません。
初歩的な質問で申し訳ありませんが、ご教授いただければ幸いです。
html
1<form action="{% url 'talemane:download'%}" method="POST"> 2 {% csrf_token %} 3 <input type="submit" name="submit" value="出力" > 4 </form> 5 <input type="text" readonly class="form-control-plaintext" id="number" value="{{ number }}"> 6 <input type="text" class="form-control" id="last_name" value="{{ last_name }}"> 7 <input type="text" class="form-control" id="first_name" value="{{ first_name }}"> 8 <input type="text" class="form-control" id="last_name_kana" value="{{last_name_kana }}"> 9 <input type="text" class="form-control" id="first_name_kana" value="{{first_name_kana }}"> 10</form>
view
1import openpyxl as px 2#... 3#ダウンロード処理 4class DateDolwnload(LoginRequiredMixin, generic.ListView): 5 template_name = 'hoge/custom_detail.html' 6 model = Employee 7 8 def index(request): 9 10 return HttpResponse('hoge/custom_detail.html') 11 12 def post(self, request, *args, **kwargs) : 13 if request.method == "POST": 14 d = { 15 'name':request.GET.get('last_name_kana') 16 } 17 18 wb = px.Workbook() 19 ws = wb.active 20 21 ws.cell(row=2, column=3).value = d["name"] 22 23 response = HttpResponse(content_type='application/vnd.ms-excel') 24 response['Content-Disposition'] = 'attachment; filename=%s' % 'report.xlsx' 25 26 27 return render(request, 'hoge/custom_detail.html', {'form': form})
回答1件
あなたの回答
tips
プレビュー