Sudheesh Singanamalla Sudheesh Singanamalla - 9 months ago 36
Python Question

TypeError while returning Response from django view

I've been trying to create a view for the url pattern

url(r'^task/', include('app.urls')), inside

url(r'^(?P<task_id>[0-9]+)/$', views.task, name='task'),

Which can be accessed by
. My corresponding view for

def task(request, task_id):
obj = Task.objects.get(pk=task_id)
return HttpRequest("You're looking at a %s Task." % obj.type)

The corresponding model for Task is this.

class Task(models.Model):
image = models.ImageField(upload_to='tasks')
description = models.CharField(max_length=500)
language = models.CharField(max_length=7, default='en', choices=LANGUAGES)
type = models.CharField(max_length=20, default='Transliteration', choices=TASK_TYPES)
date = models.DateTimeField(auto_now_add=True)

def __str__(self):
return self.language + ' ' + self.description

I am successfully able to debug the
values which exist but my webpage returns the following error.

TypeError at /task/1/
__init__() takes exactly 1 argument (2 given)

How can such an error be fixed?

Answer Source

Return HttpResponse instead of HttpRequest

def task(request, task_id):
    obj = Task.objects.get(pk=task_id)
    return HttpResponse("You're looking at a %s Task." % obj.type)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download