Samir Tendulkar
Samir Tendulkar

Reputation: 1191

Adding forms to Django DetailView ERROR message:Method Not Allowed (POST)

Hi Djangonauts I am trying to Build a Question and Answer type app like "StackOverflow" or "Quora" I am new to Django. so please forgive any silly mistakes in logic or code. Below is my error

Before: I made the question and answer app and it worked fine. The Question Detail page had the Question and an answer button to let the user answer the Question. When the user clicks on the button he goes to a new "answer_form.html" writes his answer and clicks Save His answer then shows in the Question Detail page. Till here everything worked fine

The Problem: Now I don't want the user to leave the Question Detail page post his answer, I used JavaScript to make the answer_form to appear on the question detail page, However now when the user clicks Save I get a error

This page isn’t working
If the problem continues, contact the site owner.
HTTP ERROR 405 

How can I fix this error. I am sure a lot of people don't want people to leave the detail page to comment a post or answer a question. Below are my views and template details

views.py

class AnswerCreate(LoginRequiredMixin, CreateView):
    model = Answer
    form_class = AnswerForm

    def form_valid(self, form, *args, **kwargs):
        self.object = form.save(commit=False)
        self.object.author = self.request.user
        slug = self.kwargs.get('slug')
        print(slug)
        self.object.question = get_object_or_404(Question, slug=slug)
        self.object.save()
        return super().form_valid(form) 

Below are my forms.py

class AnswerForm(forms.ModelForm):

    class Meta:
        model = Answer
        fields = ('text', 'answer_image', 'answer_image2')

and below is my question_detail.html template

 <div class="container-fluid" id="answ" style="display:none">
        <div class="form-group row">
             {{field.errors}}
            <form action="" method="post" enctype="multipart/form-data">
                {% csrf_token %}
                {% bootstrap_form form2 %}
                <input type="submit" value="Save" class="btn btn-primary" />
                <button type="button" onclick="document.getElementById('answ').style.display='none'"
                        class="btn btn-default">Cancel</button> # To hide the form
            </form>
        </div>
    </div>

This is the button in the template that makes the form show

      <a class="btn btn-primary btn-sm col-sm-2"
onclick="document.getElementById('answ').style.display='block'">Answer</a>

before this button looked like this and everything worked:

<a href="{% url 'questions:add_answer' username=question.user.username slug=question.slug %}>Answer</a>

Below are my Urls.py

url(r'^(?P<username>[-\w]+)/(?P<slug>[-\w]+)/add_answer/$', views.AnswerCreate.as_view(), name='add_answer'),

Sure below is the view.py for QuestionDetail

class QuestionDetail(SelectRelatedMixin, DetailView):
    model = Question
    select_related = ('user', 'group')

    def get_queryset(self):
        queryset = super().get_queryset()
        return queryset.filter(user__username__iexact=self.kwargs.get('username'))

    def get_context_data(self, **kwargs):
        context = super(QuestionDetail, self).get_context_data(**kwargs)
        context['form2'] = AnswerForm
        return context

below is the URL for QuestionDetail

 url(r'^(?P<username>[-\w]+)/(?P<slug>[-\w]+)/$', views.QuestionDetail.as_view(), name='single'),

Upvotes: 1

Views: 479

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599946

You know how to create the URL to get your form to submit to your CreateView, but you're not using it; you have an empty action in the HTML form element so it will just submit back to the same DetailView. You need to use that {% url %} tag in the action attribute so that the form submits to the right place.

You will probably also want to define get_success_url() in your CreateView so that it redirects back to the DetailView.

Upvotes: 3

Related Questions