0

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 

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

8 Comments

i.e return HttpResponseRedirect(reverse('account/profile/'))?
No, reverse takes a url name and converts it into a url path. If you know your url path, you don't need to use reverse - just do HttpResponseRedirect('/account/profile/'), include the first '/' too
Url is mapped as path('account/profile/', site_views.profile, name='profile') but even once changing it to return HttpResponseRedirect('account/profile/') , error does not dissapear
could you copy the stack trace below to see where the error is thrown? Can't see it in your image.
Ah, I think it's because your user_availabilities is empty, so it's not looping through the list. And since it's not looping through the list, it's unable to render any response. You should provide a fallback response outside of your for loop.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.