使用Django和Python路由插入页面时获取TypeError

使用Django和Python路由插入页面时获取TypeError,python,django,Python,Django,使用Django和Python打开插入页时出错。以下是错误: 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

使用Django和Python打开插入页时出错。以下是错误:

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

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({'message': 'Data Saved'}))

试试这个,否则你需要在模板中传递你需要提到的内容,渲染只需要这样一个命令:

img_context = {
    'user' : user,
    'object': advertisment,
}

ret = template.render(img_context)

为什么您要提交请求?模板中应该有一个包含数据的dict上下文。很高兴这有助于bro@Satya,但当此insert方法执行时抛出另一个错误。给出了失败的原因:CSRF令牌丢失或不正确。我在insert.html页面的表单中使用了{%CSRF_令牌%}。模板中是否有表单??然后将{%csrf_token%}放在打开后