title: ValueError: Field 'id' expected a number but got '<built-in function id>'; POST request redirect
body: <p>When a User successfully makes a <code>POST</code> request to edit their own question, they are redirected to a detail page for the same question. When a subsequent <code>GET</code> request is sent to the <code>PostedQuestionPage</code> view after editing a question, the following <em>ValueError</em> is raised when trying retrieve an instance with <code>get_object_or_404()</code>:</p>
  <ul>
  <li><code>ValueError: Field 'id' expected a number but got '&lt;built-in function id&gt;'</code></li>
  </ul>
  <p>Why is <code>PostedQuestionPage</code> being passed <code>'&lt;built-in function id&gt;'</code> and not a value that is suppose to represent a question instance id?</p>
  <pre><code>&gt; c:\..\posts\views.py(137)get()
  -&gt; question = get_object_or_404(Question, id=question_id)
  (Pdb) ll
  134         def get(self, request, question_id):
  135             import pdb; pdb.set_trace()
  136             context = self.get_context_data()
  137  -&gt;         question = get_object_or_404(Question, id=question_id)
  138             context['question'] = question
  139             return self.render_to_response(context)
  (Pdb) question_id
  '&lt;built-in function id&gt;'
  (Pdb) n
  ValueError: Field 'id' expected a number but got '&lt;built-in function id&gt;'.
  &gt; c:\..\posts\views.py(137)get()
  -&gt; question = get_object_or_404(Question, id=question_id)
  (Pdb) n
  --Return--
  &gt; c:\..\posts\views.py(137)get()-&gt;None
  -&gt; question = get_object_or_404(Question, id=question_id)
  </code></pre>
  <pre><code>class TestPostEditQuestionPage(TestCase):
      '''Verify that a message is displayed to the user in the event
      that some aspect of the previous posted question was edited.'''
  
      @classmethod
      def setUpTestData(cls):
          cls.user = get_user_model().objects.create_user(
              username=&quot;OneAndOnly&quot;,
              password=&quot;passcoderule&quot;
          )
          profile = Profile.objects.create(user=cls.user)
          tag = Tag.objects.create(name=&quot;TagZ&quot;)
          question = Question.objects.create(
              title=&quot;This is Question Infinity&quot;,
              body=&quot;The is the content body for This is Question Infinity&quot;,
              profile=profile
          )
          question.tags.add(tag)
  
          cls.data = {
              &quot;title&quot;: &quot;This is Question Zero&quot;,
              &quot;body&quot;: &quot;The is the content body for This is Question Infinity&quot;,
              &quot;tags_0&quot;: &quot;TagN&quot;, &quot;tags_1&quot;: &quot;TagZ&quot;
          }
  
      def test_posted_question_content_changed(self):
          self.client.force_login(self.user)
          response = self.client.post(
              reverse(&quot;posts:edit&quot;, kwargs={&quot;question_id&quot;: 1}),
              data=self.data
          )
          self.assertRedirects(
              response, reverse(&quot;posts:question&quot;, kwargs={&quot;question_id&quot;: 1}), 
              status_code=303
          )
  
  
  
  class TestGetEditQuestionPage(TestCase):
  
      @classmethod
      def setUpTestData(cls):
          cls.user = get_user_model().objects.create_user(&quot;The_User_000&quot;)
          profile = Profile.objects.create(user=cls.user)
          tag = Tag.objects.create(name=&quot;TagABC&quot;)
          question = Question.objects.create(
              title=&quot;What is the difference between A &amp; B?&quot;,
              body=&quot;I'm trying to figure out this out. Can you help me?&quot;,
              profile=profile
          )
          question.tags.add(tag)
  
  
      def test_user_question_instance_populates_edit_form(self):
          self.client.force_login(self.user)
          response = self.client.get(
              reverse(&quot;posts:edit&quot;, kwargs={
                  'question_id': 1
              })
          )
          self.assertEqual(response.status_code, 200)
          self.assertTemplateUsed(response, &quot;posts/ask.html&quot;)
          self.assertContains(response, &quot;Edit your question&quot;)
  
  
  
  </code></pre>
  <pre><code>from django.views.generic.base import TemplateView
  from django.contrib import messages
  from django.db import IntegrityError
  from django.shortcuts import get_object_or_404
  from django.urls import reverse
  
  from authors.models import Profile
  from .forms import SearchForm, QuestionForm, AnswerForm
  from .models import Question, Tag, Answer
  
  from django.http import HttpResponseRedirect
  from authors.http_status import SeeOtherHTTPRedirect
  
  
  class Page(TemplateView):
  
      def get_context_data(self, **kwargs):
          context = super().get_context_data()
          context['search_form'] = SearchForm()
          return context
  
  
  
  class QuestionListingPage(Page):
  
      template_name = &quot;posts/main.html&quot;
      extra_context = {
          &quot;title&quot;: &quot;Top Questions&quot;,
          &quot;query_buttons&quot;: [&quot;Interesting&quot;, &quot;Hot&quot;, &quot;Week&quot;, &quot;Month&quot;]
      }
  
      def get_context_data(self, **kwargs):
          context = super().get_context_data(**kwargs)
          context['questions'] = Question.objects.all()
          return context
  
      def get(self, request):
          context = self.get_context_data()
          return self.render_to_response(context)
  
  
  class AskQuestionPage(Page):
  
      template_name = &quot;posts/ask.html&quot;
      extra_context = {
          'title': &quot;Ask a public question&quot;
      }
  
      def attach_question_tags(self, tags):
          question_tags = []
          for name in tags:
              try:
                  tag = Tag.objects.get(name=name)
              except Tag.DoesNotExist:
                  tag = Tag.objects.create(name=name)
              finally:
                  question_tags.append(tag)
          return question_tags
  
      def get_context_data(self, **kwargs):
          context = super().get_context_data(**kwargs)
          context['form'] = QuestionForm
          return context
  
  
      def post(self, request):
          context = self.get_context_data()
          form = context['form'](request.POST)
          if form.is_valid():
              tags = self.attach_question_tags(form.cleaned_data.pop(&quot;tags&quot;))
              try:
                  question = form.save(commit=False)
                  question.profile = request.user.profile
                  question.save()
              except IntegrityError:
                  form.add_error(None, &quot;This post is already posted&quot;)
                  context['form'] = form
              else:
                  question.tags.add(*tags)
                  form.save_m2m()
                  return SeeOtherHTTPRedirect(
                      reverse(&quot;posts:question&quot;, kwargs={&quot;question_id&quot;: question.id})
                  )
          return self.render_to_response(context)
  
  
  class EditQuestionPage(AskQuestionPage):
  
      template_name = &quot;posts/ask.html&quot;
      extra_context = {
          'title': &quot;Edit your question&quot;
      }
  
      def get(self, request, question_id):
          question = get_object_or_404(Question, id=question_id)
          context = self.get_context_data()
          context['form'] = context['form'](instance=question)
          return self.render_to_response(
              context, headers={
                  'Content-Type': &quot;text/html&quot;
              }
          )
  
      def post(self, request, question_id):
          question = get_object_or_404(Question, id=question_id)
          context = self.get_context_data()
          form = context['form'](request.POST, instance=question)
          if form.is_valid():
              if form.has_changed():
                  messages.success(request, &quot;Question updated!&quot;)
              x = form.cleaned_data.pop(&quot;tags&quot;)
              tags = self.attach_question_tags(x)
              question = form.save()
              question.tags.set(tags)
              return SeeOtherHTTPRedirect(reverse(
                  &quot;posts:question&quot;, kwargs={&quot;question_id&quot;: id}
              ))
          return self.render_to_response(context)
  
  
  class PostedQuestionPage(Page):
  
      template_name = &quot;posts/question.html&quot;
  
      def get_context_data(self, **kwargs):
          context = super().get_context_data(**kwargs)
          context['answer_form'] = AnswerForm
          return context
  
      def get(self, request, question_id):
          context = self.get_context_data()
          question = get_object_or_404(Question, id=question_id)
          context['question'] = question
          return self.render_to_response(context)
  
  
      def post(self, request, question_id):
          question = get_object_or_404(Question, id=question_id)
          context = super.get_context_data()
          form = context['answer_form'](request.POST)
          if form.is_valid():
              form.cleaned_data.update({&quot;profile&quot;: request.user.profile})
              answer = Answer.objects.create(
                  **form.cleaned_data, question=question
              )
              return SeeOtherHTTPRedirect(
                  reverse(&quot;posts:question&quot;, kwargs={
                      &quot;question_id&quot;: answer.question.id
                  })
              )
  
  class EditPostedAnswerPage(PostedQuestionPage):
  
      def get(self, request, question_id, answer_id):
          context = super().get_context_data()
          question = get_object_or_404(Question, id=question_id)
          answer = get_object_or_404(Answer, id=answer_id)
          context.update({
              &quot;answer_form&quot;: context['answer_form'](instance=answer),
              'question': question,
              'answer': answer
          })
          return self.render_to_response(context)
  
      def post(self, request, question_id, answer_id):
          context = super().get_context_data()
          question = get_object_or_404(Question, id=question_id)
          answer = get_object_or_404(Answer, id=answer_id)
          # import pdb; pdb.set_trace()
          if context['answer_form'](request.POST, instance=answer).is_valid():
              return SeeOtherHTTPRedirect(
                  reverse(&quot;posts:question&quot;, kwargs={
                      'question_id': 1
                  })
              )
  
  </code></pre>