読者です 読者をやめる 読者になる 読者になる

idという変数を使っていてTypeErrorでハマった

こんなviews.pyかいてた

リファクタリングしていた

def thread(request,answer_id):
    '''QAスレッドの閲覧と投稿'''

    user = get_user(request)
    is_answered = Answer.objects.filter(user=user,question__pk=id).count() > 0

    if request.method == "GET":
        question = get_object_or_404(Question,pk=id)
        ans_list = question.answer_set.all().order_by('id')
        return direct_to_template(request,"thread.html",{'question':question, 'ans_list':ans_list,
                                                         'form':AnswerForm(), 'is_answered':is_answered})
    elif request.method == "POST":
        form = AnswerForm(request.POST)
        if not form.is_valid():
            #return direct_to_template(request,"thread.html",{'q':AnswerForm()})
            return HttpResponseRedirect(reverse('thread', args=(answer_id)))
        ans = form.save(commit = False)
        question = get_object_or_404(Question,pk=id)
        ans.question = question
        ans.user= user
        ans.save()
        #user = user
        user.save()
        form = AnswerForm()
        question = get_object_or_404(Question,pk=id)
        ans_list = question.answer_set.all().order_by('id')
        return direct_to_template(request,"thread.html",{ 'question':question, 'ans_list':ans_list,
                                                        'form':form, 'id':id,'is_answered':is_answered})

しかしTypeErrorになってしまった。原因はidという変数が組み込みの変数であり、urls.pyから渡されるanswer_idとごっちゃにしていた。

どうにかなった

def thread(request,answer_id):
    '''QAスレッドの閲覧と投稿'''

    user = get_user(request)
    is_answered = Answer.objects.filter(user=user,question__pk=answer_id).count() > 0

    if request.method == "GET":
        question = get_object_or_404(Question,pk=answer_id)
        ans_list = question.answer_set.all().order_by('id')
        return direct_to_template(request,"thread.html",{'question':question, 'ans_list':ans_list,
                                                         'form':AnswerForm(), 'is_answered':is_answered})
    elif request.method == "POST":
        form = AnswerForm(request.POST)
        if not form.is_valid():
            #return direct_to_template(request,"thread.html",{'q':AnswerForm()})
            return HttpResponseRedirect(reverse('thread', args=({answer_id:answer_id})))
        ans = form.save(commit = False)
        question = get_object_or_404(Question,pk=answer_id)
        ans.question = question
        ans.user= user
        ans.save()
        user.save()
        form = AnswerForm()
        question = get_object_or_404(Question,pk=answer_id)
        ans_list = question.answer_set.all().order_by('id')
        #return direct_to_template(request,"thread.html",{ 'question':question, 'ans_list':ans_list,
        #                                               'form':form, 'id':answer_id,'is_answered':is_answered})
        return HttpResponseRedirect(reverse('thread', args=({answer_id:answer_id})))

結論

組み込みの変数を使わないほうが思わぬバグが減って宜しい。idの他にもhash,listなんかは危ない

先立の言葉

@xxxxx

んーと filterの条件でid (組み込み関数)を渡してしまってて それをDjangoのORMがid()と呼びだそうとして id関数は引数が必要なので
> TypeError at /thread/19
id() takes exactly one argument (0 given)
こういうエラーになってた でもそもそもid関数を渡してること自体が問題だった、というオチ

広告を非表示にする