Django视图未返回HttpResponse对象.它返回None [英] Django The view didn't return an HttpResponse object. It returned None instead

查看:78
本文介绍了Django视图未返回HttpResponse对象.它返回None的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试检查变量"avail.end_time:"是否为空,如果是,则重定向至url.但是我得到了 视图mysite.views.enter_queue没有返回HttpResponse对象.而是返回了None."错误

从我的可用性"模型中引用了

Endtime变量,其中end_time = models.TimeField(null = False,blank = False)

我知道以前曾问过这个问题,但是给出的解决方案都无法帮助解决问题.

  @login_requireddef enter_queue(要求):#从Django请求中获取用户&映射到变量django_user = request.user#link user_profile与django用户个人资料模型&获取用户资料user_profile = django_user.profile#user_profile = Profile.objects.get(user = request.user)#将user_availabilities变量映射到可用性模型中的配置文件users_availabilities = Availability.objects.filter(profile = user_profile)#将user_avail映射到用户个人资料#创建一个数组来存储所有匹配的会话all_matching_sessions = []#可用是每个Availability对象在users_availabilities中可用:如果avail.end_time:返回HttpResponseRedirect(render(request,'mysite/profile.html'))别的:matching_sessions = Session.objects.filter(end_time__lte = avail.end_time)#循环遍历与可用性匹配的所有会话结束时间#将它们添加到数组all_matching_sessions = all_matching_sessions + matching_sessions#如果没有匹配的会话可用如果len(all_matching_sessions)== 0:#创建一个会话player_session =会话(游戏='random_game',start_time = users_availabilities [0] .start_time,end_time = users_availabilities [0] .end_time,)player_session.save()返回render(request,'mysite/profile.html')别的:player_session =会话(会话= all_matching_sessions [0],个人资料= user_profile)player_session.save()#return HttpResponse('等待队列')返回render(request,'mysite/profile.html') 

错误图片以供参考

**错误*

/account/enter_queue/中的

  ValueError视图mysite.views.enter_queue没有返回HttpResponse对象.它而是返回None.请求方法:GET要求网址:http://127.0.0.1/account/enter_queue/Django版本:2.0.3异常类型:ValueError异常值:视图mysite.views.enter_queue没有返回HttpResponse对象.它而是返回None.异常位置:/usr/local/lib/python3.6/site-_get_response中的package/django/core/handlers/base.py,第139行Python可执行文件:/usr/local/bin/python3.6的Python版本:3.6.4Python路径:['/home/mihir/meshwell/capstone-project/siteroot','/usr/local/lib/python36.zip','/usr/local/lib/python3.6','/usr/local/lib/python3.6/lib-dynload','/usr/local/lib/python3.6/site-packages','/var/www/CapstoneProject/siteroot','/var/www/CapstoneProject/siteroot/mysite']服务器时间:2018年4月5日,星期四04:07:23 +0000追溯切换到复制和粘贴视图/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py在内响应= get_response(请求)...▶本地变量_get_response中的/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py改为不返回任何内容."%(callback .__ module__,view_name)...▶本地变量 

解决方案

HttpResponseRedirect 应该使用URL而不是使用 render() <<返回的 HttpResponse 对象/p>

Trying to check if variable 'avail.end_time:' is empty,and if so redirect to a url. But instead, I get "The view mysite.views.enter_queue didn't return an HttpResponse object. It returned None instead." ERROR

Endtime variable is being referenced from my Availabilities model where end_time = models.TimeField(null=False, blank=False)

I know this questions been asked before but none of the solutions given have helped solve the problem.

@login_required
def enter_queue(request):
# get the user from the Django request & map to variable
django_user = request.user
  #link user_profile to django users profile model & get user's profile
user_profile = django_user.profile
#user_profile = Profile.objects.get(user=request.user)
  #Map user_availabilities variable to profile from Availability model
users_availabilities = Availability.objects.filter(profile=user_profile) 
#mapping user_avail to user profile

#creating an array to store all matching sessions
all_matching_sessions = []
  # avail is each Availability object
for avail in users_availabilities:

    if avail.end_time:
        return HttpResponseRedirect(render(request,'mysite/profile.html'))
    else:
        matching_sessions = Session.objects.filter(end_time__lte=avail.end_time)#looping through all the sessions end times that match to availability
#adding them to the array
        all_matching_sessions = all_matching_sessions + matching_sessions

#If no matching sessions are available
        if len(all_matching_sessions) == 0:
    #create a session
            player_session = Session(
                game = 'random_game',
                start_time = users_availabilities[0].start_time,
                end_time = users_availabilities[0].end_time,
            )
            player_session.save()
            return  render(request, 'mysite/profile.html')

        else:
            player_session = Session(
                session = all_matching_sessions[0],
                profile = user_profile
            )
            player_session.save()
            #return HttpResponse('Waiting in queue')
            return  render(request, 'mysite/profile.html')

Image of the error for reference

**ERROR*

ValueError at /account/enter_queue/

The view mysite.views.enter_queue didn't return an HttpResponse object. It 
returned None instead.

Request Method:     GET
Request URL:    http://127.0.0.1/account/enter_queue/
Django Version:     2.0.3
Exception Type:     ValueError
Exception Value:    

The view mysite.views.enter_queue didn't return an HttpResponse object. It 
returned None instead.

Exception Location:     /usr/local/lib/python3.6/site- 
packages/django/core/handlers/base.py in _get_response, line 139
Python Executable:  /usr/local/bin/python3.6
Python Version:     3.6.4
Python Path:    

['/home/mihir/meshwell/capstone-project/siteroot',
 '/usr/local/lib/python36.zip',
 '/usr/local/lib/python3.6',
 '/usr/local/lib/python3.6/lib-dynload',
 '/usr/local/lib/python3.6/site-packages',
 '/var/www/CapstoneProject/siteroot',
 '/var/www/CapstoneProject/siteroot/mysite']

Server time:    Thu, 5 Apr 2018 04:07:23 +0000
Traceback Switch to copy-and-paste view

/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py in 
inner

                response = get_response(request)

     ...
▶ Local vars
/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py in _get_response

                    "returned None instead." % (callback.__module__, view_name)

     ...
▶ Local vars 

解决方案

HttpResponseRedirect should take a URL instead of a HttpResponse object which you are returning with render()

这篇关于Django视图未返回HttpResponse对象.它返回None的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆