每一個評論:我沒有改變我的代碼:使用django_xhtml2pdf與Django 1.11「錯誤:上下文必須是一個字典而不是上下文。」
providers = Provider.objects.all()
context = { 'providers':providers}
我知道它沒有發揮作用,但想我會繼續嘗試它,因爲奇怪的事情發生。我擔心這個錯誤是在我的django版本上運行的模塊本身。
我沒有看到這個其他的答案,因爲我只是用什麼記錄在這裏讓我感到困惑:
https://spapas.github.io/2015/11/27/pdf-in-django/#django-integration
用於獲取django_xhtml2pdf東西的工作。我的看法是這樣:
def providers_plain_old_view(request):
resp = HttpResponse(content_type='application/pdf')
context = {
'providers': Provider.objects.all()
}
result = generate_pdf('ipaswdb/provider/providers_plain_old_view.html', file_object=resp, context=context)
return result
我現在知道的是在Django 1.11.14我用的不好,但不知道如何修復錯誤:
Traceback (most recent call last):
File "D:\Python27\lib\site-packages\django\core\handlers\exception.py", line 41, in inner
response = get_response(request)
File "D:\Python27\lib\site-packages\django\core\handlers\base.py", line 249, in _legacy_get_response
response = self._get_response(request)
File "D:\Python27\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "D:\Python27\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "D:\Programming\web\ipa_django\mysite\ipaswdb\views.py", line 312, in providers_plain_old_view
result = generate_pdf('ipaswdb/provider/providers_plain_old_view.html', file_object=resp, context=context)
File "D:\Python27\lib\site-packages\django_xhtml2pdf\utils.py", line 62, in generate_pdf
generate_pdf_template_object(tmpl, file_object, context)
File "D:\Python27\lib\site-packages\django_xhtml2pdf\utils.py", line 39, in generate_pdf_template_object
html = template_object.render(Context(context))
File "D:\Python27\lib\site-packages\django\template\backends\django.py", line 64, in render
context = make_context(context, request, autoescape=self.backend.engine.autoescape)
File "D:\Python27\lib\site-packages\django\template\context.py", line 287, in make_context
raise TypeError('context must be a dict rather than %s.' % context.__class__.__name__)
TypeError: context must be a dict rather than Context.
"GET /ipaswdb/provider_roster/ HTTP/1.1" 500 86485
我的意思是要我在最新的Django版本中以不同的方式調用generate_pdf函數?