简体   繁体   English

Django-视图未返回HttpResponse对象

[英]Django - view didn't return an HttpResponse object

I'm facing this exception error and I'm puzzled by it, as this method worked in similar system, appreciate any help or pointers. 我遇到此异常错误,对此感到困惑,因为此方法在类似的系统中有效,感谢任何帮助或指针。 Many Thanks! 非常感谢!

Exception Value: The view Project.qna.views.add_vote didn't return an HttpResponse object. 异常值:视图Project.qna.views.add_vote没有返回HttpResponse对象。

def add_vote(request):

if request.method == "POST":
    q_id = request.POST['vote_form_q_id']
    a_id = request.POST['vote_form_a_id']
    vote_value = request.POST['vote_form_value']

    ok = False
    vote_num = None
    name = None

    if q_id:
        try:
            question = Question.objects.get(id=q_id)
            question.num_vote += int(vote_value)
            question.save()
            vote_num = question.num_vote
            name = 'Question_'+str(q_id)
            ok = True

        except Question.DoesNotExist:
            pass
    elif a_id:
        try:
            answer = Answer.objects.get(id=a_id)
            answer.num_vote += int(vote_value)
            answer.save()
            vote_num = answer.num_vote
            name = 'Answer_'+str(a_id)
            ok = True
        except Answer.DoesNotExist:
            pass

    if ok and request.is_ajax:
        result = simplejson.dumps({
            "vote_num": vote_num,
        }, cls=LazyEncoder)
        response = HttpResponse(result, mimetype='application/javascript')

        response.set_cookie(name, datetime.now)
    return response

Fix your indention please, also you seem to have a lot of workarounds that could be simplified. 请修复您的缩进,您似乎还有很多可以简化的解决方法。

Every django view should return a HttpResponse object, you seem to have a lot of places where this would not be the case. 每个django视图都应返回一个HttpResponse对象,您似乎在很多地方都不是这种情况。 To narrow down your problem change every pass to a print statement to see where your code actually fails. 为了缩小你的问题的变更都passprint语句来看看你的代码实际上失败。 It would be quite helpful if you could present your POST data. 如果可以显示POST数据,将非常有帮助。

Well it's hard to tell without seeing what kind of request you are making to the view. 好吧,如果不看您对视图的要求是很难分辨的。 But are you sending a POST request? 但是,您要发送POST请求吗? Because you don't handle GET requests in any way. 因为您不以任何方式处理GET请求。 Also the indentation is wrong. 缩进也是错误的。 But that might just be cutting and pasting gone awry. 但这可能只是剪切和粘贴出错了。

This is untested, but it's a cleaner and more robust design, which I believe fits in with your logic and highlights the points where returning an HttpResponse is necessary: 这未经测试,但是它是一种更简洁,更健壮的设计,我认为它符合您的逻辑并突出了需要返回HttpResponse的要点:

def add_vote(request):
    if not (request.method == 'POST' and request.is_ajax):
        return # Some suitable response here
    try:
        vote_value = int(request.POST.get('vote_form_value',''))
    except ValueError as e:
        pass # Some suitable response here

    def saveobj(model, key, val): # helper function to reduce code repetition
        item = model.objects.get(id=key)
        item.num_vote += val
        item.save()
        return item.num_vote, '%s_%s' % (model.__class__.__name__, key)

    for model, key in [(Question, 'vote_form_q_id'), (Answer, 'vote_form_a_id')]):
        try:
            new_vote_value, name = saveobj(model, request.POST[key], vote_value)
            break
        except (KeyError, ObjectDoesNotExist) as e:
            continue # or error out
    else:
        pass # neither question or answer found - so suitable response here

    # return ajax response here....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM