2017-06-13 57 views
0

使用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。請幫我解決這個問題。

+0

爲什麼你想呈現的請求?應該有一個與您的模板數據字典(上下文)。 – vZ10

回答

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

嘗試這個,否則你需要,你需要提及

+0

錯誤消失了,它的工作正常。 – satya

+0

很高興幫助bro @satya – Exprator

+0

但是,當這個'insert'方法執行它拋出另一個錯誤。 – satya

0

渲染只需一個字典這樣的模板傳遞什麼:

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

ret = template.render(img_context) 
相關問題