[Answer]-Django test client get row id from query

1👍

The problem is not with your tests, but with the view itself. In Django a view always has to return a HttpResponse object. Sometimes this is achieved using a shortcut function like render(), but it in turn also returns an HttpResponse object.

If for some reason you just want to return an otherwise empty page with this single value you could change

return up.id

to

return HttpResponse(up.id)

Also, I wonder: Did you create the view just to test UserProfile and don’t use it as a view on the actual site? If so, this code doesn’t belong in a view, it should be put into the unittest itself. You should only use the test client to test actual, real views.


On an mostly unrelated, but quite important note. This:

try:
    # your view code
except:
    pass

is a strong antipattern. Why would you want to silence all the potential problems? You should really stop doing that.

Leave a comment