How to solve method not allowed (Post) http error 405

 ̄綄美尐妖づ 提交于 2021-01-29 18:05:51

问题


Codes in views

I am new to django I couldn't able to rectify where it went wrong can anyone please help me on this.

class UpdateVote(LoginRequiredMixin,UpdateView):
    form_class = VoteForm
    queryset = Vote.objects.all()

    def get_object(self,queryset=None):
        vote = super().get_object(queryset)
        user = self.request.user
        if vote.user != user:
            raise PermissionDenied('can not change another user vote')
        return vote
    def get_success_url(self):
        movie_id = self.object.movie.id
        return reverse('core:movie_detail', kwargs={'pk':movie_id})
    def render_to_response(self, context, **response_kwargs):
        movie_id = context['object'].id
        movie_detail_url = reverse('core:movie_detail',kwargs={'pk':movie_id})
        return redirect(to=movie_detail_url)

class MovieDetail(DetailView):
    queryset = Movie.objects.all_with_prefetch_persons()
    def get_context_data(self, **kwargs):
        ctx = super().get_context_data(**kwargs)
        if self.request.user.is_authenticated:
            vote = Vote.objects.get_vote_or_unsaved_blank_vote(movie=self.object,user=self.request.user)
            if vote.id:
                vote_url_form = reverse('core:UpdateVote',kwargs={'movie_id':vote.movie.id,'pk':vote.id})
            else:
                vote_url_form = (reverse('core:create_vote',kwargs={'movie_id':self.object.id}))
            vote_form = VoteForm(instance=vote)
            ctx['vote_form'] = vote_form
            ctx['vote_url_form'] = vote_url_form
        return ctx

Codes in form.py

I have used this form to link with UpdateView

from django import forms
from django.contrib.auth import get_user_model
from .models import Movie,Vote
class VoteForm(forms.ModelForm):
    user = forms.ModelChoiceField(widget=forms.HiddenInput,queryset=get_user_model().objects.all(),disabled=True)
    movie = forms.ModelChoiceField(widget=forms.HiddenInput,queryset = Movie.objects.all(),disabled=True)
    value = forms.ChoiceField(widget=forms.RadioSelect,choices=Vote.VALUE_CHOICE)
    class Meta:
        model = Vote
        fields = ('value','user','movie',)

urls.py

This is the url mapping for the view.

from django.contrib import admin
from django.urls import path
from .views import MovieList,MovieDetail,PersonDetail,CreateVote,UpdateVote
app_name = 'core'
urlpatterns = [
    path('movies/', MovieList.as_view(), name='movie_list'),
    path('movie/<int:pk>/', MovieDetail.as_view(), name='movie_details'),
    path('person/<int:pk>/', PersonDetail.as_view(), name='person_details'),
    path('movie/<int:movie_id>/vote/', CreateVote.as_view(), name='create_vote'),
    path('movie/<int:movie_id>/vote/<int:pk>', UpdateVote.as_view(), name='UpdateVote'),
]

HTML template

This is the template I used.

{% block sidebar %}
    <div>
    {% if vote_form %}
        <form action="{{vote_form_url}}" method="post" enctype="multipart/form-data">
            {% csrf_token %}
            {{ vote_form.as_p }}
            <button class="btn btn-primary" type="submit" >Vote</button>
        </form>
    {%  else %}
        <p>Login to vote for this movie</p>
    {% endif %} </div> {% endblock %}

回答1:


The problem caused because your form was sent to another path which doesn't allow POST request. vote_form_url is not which you added in the view context, use vote_url_form instead.

...
<form action="{{ vote_url_form }}" method="post" enctype="multipart/form-data">
...

Btw, your MovieDetail view can get rid of if self.request.user.is_authenticated: by using LoginRequiredMixin like UpdateVote view.

Hope that helps!



来源:https://stackoverflow.com/questions/58152622/how-to-solve-method-not-allowed-post-http-error-405

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!