Django Rest - 创建方法,返回错误405方法不允许

时间:2016-11-17 20:42:15

标签: html django django-views django-rest-framework http-status-code-405

我创建了一个视图来创建一个新对象。在DRF默认页面中,一切正常,但是当我尝试创建一个简单的HTML页面作为用户界面时(通过向视图添加renderer_classes和template_name),会发生以下错误:405方法不允许。

我创建了一个html页面来检索/更新客户端详细信息,它工作正常。但是,我无法创建新客户端。

models.py

class Client(models.Model):
    client_rut = models.CharField(max_length=12, unique=True, verbose_name="RUT")
    client_name = models.CharField(max_length=250, verbose_name="Nombre Cliente")
    client_region = models.CharField(max_length=50, verbose_name="Region")
    client_tel = models.CharField(max_length=20, blank=True, verbose_name="Telefono")
    client_email = models.EmailField(blank=True, verbose_name="E-mail")
    is_active = models.BooleanField(default=True, verbose_name="Activo")

    class Meta:
        ordering = ['client_rut']

    def __str__(self):
        return self.client_rut + ' - ' + self.client_name

urls.py

urlpatterns = [
    # /clients/
    url(r'^$', views.ClientView.as_view(), name='clients'),
    url(r'^(?P<pk>[0-9]+)/$', views.ClientDetailView.as_view(), name='clientdetail'),
    url(r'^create/$', views.ClientCreateView.as_view(), name='clientcreate')
]
urlpatterns = format_suffix_patterns(urlpatterns, allowed=['json', 'html', 'xml'])

serializers.py

class ClientListSerializer(ModelSerializer):
    url = HyperlinkedIdentityField(view_name='clients:clientdetail',)
    class Meta:
        model = Client
        fields = '__all__'

class ClientDetailSerializer(ModelSerializer):
    class Meta:
        model = Client
        fields = '__all__'

views.py

#this view works fine
class ClientDetailView(views.APIView):
    queryset = Client.objects.all()
    serializer_class = ClientDetailSerializer
    renderer_classes = [TemplateHTMLRenderer]
    template_name = 'clientdetail.html'

    def get(self, request, pk):
        client = get_object_or_404(Client, pk=pk)
        serializer = ClientDetailSerializer(client)
        return Response({'serializer': serializer, 'client': client})

    def post(self, request, pk):
        client = get_object_or_404(Client, pk=pk)
        serializer = ClientListSerializer(client, data=request.data)
        if not serializer.is_valid():
            return Response({'serializer': serializer, 'client': client})
        serializer.save()
        return redirect('clients:clients')

# this one does not
class ClientCreateView(generics.ListCreateAPIView):
    queryset = Client.objects.all()
    serializer_class = ClientListSerializer
    renderer_classes = [TemplateHTMLRenderer]
    template_name = 'clientcreate.html'

    def post(self, request):
        Client.objects.create()
        return redirect('clients:clients')

clientcreate.html

{% load rest_framework %}

<html><body>
    <form action="{% url 'clients:clientcreate' %}" method="POST">
        {% csrf_token %}
        <div class="col-xs-6 ">
            {% render_form serializer %}
        </div>
        <div>
            <input type="submit" value="Grabar">
        </div>
    </form>
</body></html>

clientdetail.html几乎与clientcreate.html完全相同,除此之外:

<form action="{% url 'clients:clientdetail' pk=client.pk %}" method="POST">

我想我已经看过所有405个相关的问题,但我无法解决我的问题(并不一定意味着答案不在那里)。

希望有人可以帮我这个。

谢谢!

0 个答案:

没有答案