使用Django和Python打開插入頁面時出現一個錯誤。我正在解釋下面的錯誤。在使用Django和Python插入頁面時獲取TypeError
TypeError at /insert/
context must be a dict rather than WSGIRequest.
Request Method: GET
Request URL: http://127.0.0.1:8000/insert/
Django Version: 1.11.2
Exception Type: TypeError
Exception Value:
context must be a dict rather than WSGIRequest.
Exception Location: /usr/local/lib/python2.7/dist-packages/django/template/context.py in make_context, line 287
Python Executable: /usr/bin/python
Python Version: 2.7.6
我在下面解釋我的代碼。
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.shortcuts import render
from django.http import HttpResponse
from django.template import loader,Context,RequestContext
from crud.models import Person
# Create your views here.
def index(request):
t = loader.get_template('index.html')
#c = Context({'message': 'Hello world!'})
return HttpResponse(t.render({'message': 'Hello world!'}))
def insert(request):
# If this is a post request we insert the person
if request.method == 'POST':
p = Person(
name=request.POST['name'],
phone=request.POST['phone'],
age=request.POST['age']
)
p.save()
t = loader.get_template('insert.html')
#c = RequestContext(request)
return HttpResponse(t.render(request))
我正在使用Django 1.11
。請幫我解決這個問題。
爲什麼你想呈現的請求?應該有一個與您的模板數據字典(上下文)。 – vZ10