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

时间:2017-06-13 09:14:00

标签: 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 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。

2 个答案:

答案 0 :(得分:1)

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'}))

试试这个,否则你需要传递的模板需要提及

答案 1 :(得分:0)

render只需要这样的字典:

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

ret = template.render(img_context)