Django渲染得到一个意外的关键字参数'context_instance'

问题描述 投票:-1回答:1

我正在使用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', })
        ) 
python django pycharm
1个回答
1
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.