Sudheesh Singanamalla Sudheesh Singanamalla - 5 months ago 20
Python Question

TypeError while returning Response from django view

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

Urls.py

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


Urls.py inside
app.urls


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


Which can be accessed by
/task/:task_id
. My corresponding view for
task
is

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
task_id
and
obj
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)