2017-06-13 21 views
0

DjangoとPythonを使用して挿入ページを開いているときにエラーが発生します。私は以下のエラーを説明しています。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を使用しています。この問題を解決するのを手伝ってください。

+0

なぜあなたは要求をレンダリングしようとしていますか?あなたのテンプレートのデータを含むdict(コンテキスト)が必要です。 – 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

助けてくれてありがとう@satya – Exprator

+0

しかし、この 'insert'メソッドは、別のエラーを投げて実行します。 – satya

0

レンダリング言及する必要があるテンプレートに渡すために必要なものを他に、これを試して、このような単なる辞書をとります。

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

ret = template.render(img_context) 
関連する問題