该视图未返回 HttpResponse 对象。它返回 None 相反

2024-01-21

我有以下简单的看法。为什么会导致这个错误呢?

The view auth_lifecycle.views.user_profile didn't return an HttpResponse object. It returned None instead.

"""Renders web pages for the user-authentication-lifecycle project."""
from django.shortcuts               import render
from django.template                import RequestContext
from django.contrib.auth            import authenticate, login

def user_profile(request):
    """Displays information unique to the logged-in user."""

    user = authenticate(username='superuserusername', password='sueruserpassword')
    login(request, user)

    render(request, 'auth_lifecycle/user_profile.html',
           context_instance=RequestContext(request))

因为视图必须return render,而不仅仅是调用它。 (注意render是一个简单的包装HttpResponse https://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpResponse)。将最后一行更改为

return render(request, 'auth_lifecycle/user_profile.html',
           context_instance=RequestContext(request))

(另请注意render(...) https://docs.djangoproject.com/en/dev/topics/http/shortcuts/#django.shortcuts.render函数返回a HttpResponse幕后对象 https://github.com/django/django/blob/f30c7e381c94f41d361877d8a3e90f8cfb391709/django/shortcuts.py#L17-L25.)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

该视图未返回 HttpResponse 对象。它返回 None 相反 的相关文章

随机推荐