【问题标题】:Django render got an unexpected keyword argument 'context_instance'Django 渲染有一个意外的关键字参数“context_instance”
【发布时间】:2019-03-28 22:27:36
【问题描述】:

我正在使用 Python 3.6,我想使用 Django 框架在 PyCharm 中运行一个项目,但是我收到了这个错误:

TypeError at /
render() got an unexpected keyword argument 'context_instance'
Request Method: GET
Request URL:    http://127.0.0.1:8000/
Django Version: 2.1.7
Exception Type: TypeError
Exception Value:    
render() got an unexpected keyword argument 'context_instance'
Exception Location: C:\Users\hp\AppData\Local\Programs\Python\Python36\dj\xaon\app\views.py in home, line 16
Python Executable:  C:\Users\hp\AppData\Local\Programs\Python\Python36\dj\f\Scripts\python.exe
Python Version: 3.6.5
Python Path:    
['C:\\Users\\hp\\AppData\\Local\\Programs\\Python\\Python36\\dj\\xaon',
 'C:\\Users\\hp\\AppData\\Local\\Programs\\Python\\Python36\\dj\\f\\Scripts\\python36.zip',
 'C:\\Users\\hp\\AppData\\Local\\Programs\\Python\\Python36\\dj\\f\\DLLs',
 'C:\\Users\\hp\\AppData\\Local\\Programs\\Python\\Python36\\dj\\f\\lib',
 'C:\\Users\\hp\\AppData\\Local\\Programs\\Python\\Python36\\dj\\f\\Scripts',
 'c:\\users\\hp\\appdata\\local\\programs\\python\\python36\\Lib',
 'c:\\users\\hp\\appdata\\local\\programs\\python\\python36\\DLLs',
 'C:\\Users\\hp\\AppData\\Local\\Programs\\Python\\Python36\\dj\\f',
 'C:\\Users\\hp\\AppData\\Local\\Programs\\Python\\Python36\\dj\\f\\lib\\site-packages']
Server time:    Thu, 28 Mar 2019 22:20:33 +0000 

我的view.py 看起来像这样:

def home(request): 
    assert isinstance(request, HttpRequest)
    return render(
        request, 
        'app/index.html', 
        context_instance=RequestContext(request, { 'title':'Home Page', })
        ) 

【问题讨论】:

  • 您好,欢迎来到 Stack Overflow。 xaon\app\views.py 第 16 行有问题。你能给我们看看这个文件吗?
  • def home(request): assert isinstance(request, HttpRequest) return render(request, 'app/index.html', context_instance=RequestContext(request, { 'title':'Home Page', } ) )
  • 第 16 行是:{ 'title':'Home Page', }
  • 下一次,最好编辑您的问题,而不是将代码放入 cmets。那里看起来是不是有点挤? ;)
  • 好的,如果您需要帮助,将代码发布为图片并不是一个好主意。任何想要回答您的问题的人都会想要复制和粘贴您的代码,或者至少在没有链接的情况下看到它。我建议你在问下一个问题之前阅读this guide。您会发现遵守这些规则会使您的问题更有可能找到答案!祝你好运!

标签: python django pycharm


【解决方案1】:

您使用的是 Django 2.1.7,但 context_instance 位置参数是 deprecated since version 1.8 并在 Django 2.0 中删除。您应该在调用 render() 快捷方式函数时直接使用 use context instead

有关context_instance 的更多信息,请参阅this other Stack Overflow post

【讨论】:

    猜你喜欢
    • 2016-12-08
    • 2016-09-12
    • 1970-01-01
    • 2012-11-06
    • 2013-08-06
    • 1970-01-01
    • 2021-10-29
    • 2015-01-27
    • 1970-01-01
    相关资源
    最近更新 更多