Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django-view没有';t返回HttpResponse对象_Python_Django - Fatal编程技术网

Python Django-view没有';t返回HttpResponse对象

Python Django-view没有';t返回HttpResponse对象,python,django,Python,Django,我面临着这个异常错误,对此我感到困惑,因为这个方法在类似的系统中工作,非常感谢任何帮助或指点。非常感谢 异常值:view Project.qna.views.add_投票未返回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_v

我面临着这个异常错误,对此我感到困惑,因为这个方法在类似的系统中工作,非常感谢任何帮助或指点。非常感谢

异常值:view Project.qna.views.add_投票未返回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

请修改您的缩进,而且您似乎有很多可以简化的变通方法


每个django视图都应该返回一个HttpResponse对象,您似乎有很多地方不是这样的。要缩小您的问题范围,请将每个
pass
更改为
print
语句,以查看代码实际失败的地方。如果你能展示你的帖子数据,那将非常有帮助。

如果不看你对视图提出了什么样的请求,很难判断。但是你是在发邮件吗?因为您不以任何方式处理GET请求。压痕也是错误的。但这可能只是剪切和粘贴出错

这是未经测试的,但它是一种更干净、更健壮的设计,我相信它符合您的逻辑,并强调了返回
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....

好。如果缩进在实际文件中是相同的,那么它将不起作用,因为这是错误的开始。提示:如果上次的
失败,会发生什么?