django1.11.8でアプリをqiita記事を参考に作っていますが、下記エラーが解消できないでいます。
AttributeError at /
Generic detail view FooPDFView must be called with either an object pk or a slug.
views.py
python
1 2 2 from __future__ import unicode_literals 3 3 4 4 # -*- coding: utf-8 -*- 5 5 import io 6 6 import os 7 7 from django.conf import settings 8 8 from django.views import generic 9 9 from django.http import HttpResponse 10 10 from django.template.loader import get_template 11 11 from xhtml2pdf import pisa 12 12 13 13 from django.shortcuts import render 14 14 from .models import Article 15 15 16 16 # Create your views here. 17 17 18 18 class FooPDFView(generic.DetailView): 19 19 model = Article 20 20 template_name = 'blog/pdf.html' 21 21 22 22 def render_to_response(self, context): 23 23 html = get_template(self.template_name).render(self.get_context_data()) 24 24 result = io.BytesIO() 25 25 pdf = pisa.pisaDocument( 26 26 io.BytesIO(html.encode('utf-8')), 27 27 result, 28 28 link_callback=link_callback, 29 29 encoding='utf-8', 30 30 ) 31 31 32 32 if not pdf.err: 33 33 return HttpResponse( 34 34 result.getvalue(), 35 35 content_type='application/pdf' 3636 ) 37 37 38 38 return HttpResponse('<pre>%s</pre>' % escape(html)) 39 39 40 40 def link_callback(uri, rel): 41 41 sUrl = settings.STATIC_URL 42 42 43 43 if not os.path.isFile(path): 44 44 raise Exception( 45 45 '%s must start with %s' % \ 46 46 (uri, sUrl) 47 47 ) 48 48 49 49 return path 50 50 51 51 52 52 def post_list(request): 53 53 return render(request, 'blog/post_list.html', {}) 54
urls.py
python
1 1 from django.conf.urls import include, url 2 2 from . import views 3 3 4 4 app_name = 'blog' 5 5 6 6 urlpatterns = [ 7 7 url(r'^$', views.FooPDFView.as_view(), name='pdf'), 8 8 ]
#やってみたこと
ググると、slug_fieldをセットしなさいのような回答があるのですが、上記のとおり現状正規表現を使った複雑なURLを設定してないのですが、http://localhost:8000/にアクセスしてpdfを出力するようにしたいのですがどのように改修すべきでしょうか。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/03/18 21:08
2018/03/18 23:39