satya
satya

Reputation: 3560

Getting TypeError while routing to insert page using Django and Python

I am getting an error while opening my insert page using Django and Python. Here is the error:

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

Here is my code:

# -*- 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))

I am using Django 1.11.

Upvotes: 0

Views: 83

Answers (2)

mbieren
mbieren

Reputation: 1118

render takes just a dict like this :

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

ret = template.render(img_context)

Upvotes: 0

Exprator
Exprator

Reputation: 27503

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

try this, else what you need to pass in the template that you need to mention

Upvotes: 1

Related Questions