如何解决方法不允许(发布)http错误405

时间:2019-09-29 05:49:54

标签: django django-forms django-views

视图中的代码

我是django的新手,我无法纠正出现问题的地方,有人可以帮我吗?

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

form.py中的代码

我已使用此表单与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

这是视图的url映射。

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模板

这是我使用的模板。

{% 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 个答案:

答案 0 :(得分:1)

由于您的表单已发送到不允许POST请求的其他路径而导致的问题。 vote_form_url不是您在视图上下文中添加的,请改用vote_url_form

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

顺便说一句,您的MovieDetail视图可以像if self.request.user.is_authenticated:视图那样使用LoginRequiredMixin来摆脱UpdateVote

希望有帮助!