2017-09-03 78 views
1

每一個評論:我沒有改變我的代碼:使用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函數?

回答

2

的主要問題在於錯誤的輸出線

File "D:\Python27\lib\site-packages\django_xhtml2pdf\utils.py", line 39, in generate_pdf_template_object 
html = template_object.render(Context(context)) 

。這是一個問題,django-xhtml2pdf軟件包不能及時更新爲1.11。渲染調用已經從

html = template_object.render(Context(context)) 

根據升級到1.11筆記https://docs.djangoproject.com/en/1.11/ref/templates/upgrading/ django.template.loader部分改爲

html = template_object.render(context) 

您可以通過提交錯誤報告或實施包中提供的views.py

來等待他們修復它
相關問題