PUT和DELETE Django

时间:2016-04-06 15:09:56

标签: python django

我在django和python中相对较新,现在我想知道如何通过django表单发送PUT和DELETE请求。

我发现了这个主题: https://baxeico.wordpress.com/2014/06/25/put-and-delete-http-requests-with-django-and-jquery/

Sending a DELETE request from a form in Django

但是这个主题解决这个问题的方式对我而言并不容易。所以问题是 - 是否有任何简单的方法可以在Django中通过表单发送PUT和DELETE请求。

现在我有了这个:

views.py

class AllRoutes(View):

model = Schedule
template_name = 'trains_schedule/all_routes.html'

def get(self,request,train_id=None):
    if train_id:
        train = Schedule.objects.get(pk=int(train_id))
        context = {'train':train}
    else:
        context = {'schedule_list':Schedule.objects.all()}
    return render(request,'trains_schedule/all_routes.html',context)

def delete(self,request,train_id=None):
    route = get_object_or_404(Schedule, pk=train_id)
    response = u'Successful delete route {}'.format(route.display_name())
    route.delete()
    return HttpResponse(response)

urls.py

urlpatterns = [
url(r'^trains/(?P<train_id>[0-9]+)?$', views.AllRoutes.as_view(), name='trains')]

all_routes.html

{% if schedule_list %}
<h3>{{ "List of all train routes" }}</h3>
<ul>
{% for train_route in schedule_list %}
    <li><a href="{% url 'trains' train_route.id %}">{{ train_route.display_name }}</a></li>
{% endfor %}
</ul>
{% else %}
<p></p>
{% endif %}

{% if train %}
<h1>{{ train.train }}</h1>

<h3>{{ "Route info" }}</h3>
{{ train.display_train }}

<h3>{{ "Train info" }}</h3>
{{train.display_train_info}}

<form  method="delete">
{% csrf_token %}
<input type="submit" value="Delete" />
</form>
{% else %}
<p></p>
{% endif %}

现在它的工作原理如下:

  • / train /显示所有列车路线列表并提供每条路线的链接
  • / trains / 12 - 在views.py中获取()得到路线ID并显示有关此路线的信息

我希望当我按“删除”时 - 查看接收DELETE请求。

我以这种方式结束:

<form  method="post">
{% csrf_token %}
<input id="action_id" type="hidden" name="action" value="Delete">
<input type="submit" value="Delete" />
</form>

并在views.py中我使用action_id ==“删除”

添加对帖子请求的处理

但我想做出真正的DELETE和PUT请求,而不是解析POST请求以了解我需要做的事情 - 删除或更改路由或添加新路由。

我知道我需要改变这一部分:

<form  method="delete">
{% csrf_token %}
<input type="submit" value="Delete" />
</form>

但我不知道该怎么做。

1 个答案:

答案 0 :(得分:11)

根据HTML standard,表单的有效方法是GETPOST。所以你不能这样做<form method="delete">。 然而Django正确处理&#39; PUT&#39;和&#39;删除&#39; (和所有其他人)http方法。

from django.views.generic import View


class TestView(View):
    http_method_names = ['get', 'post', 'put', 'delete']

    def put(self, *args, **kwargs):
        print "Hello, i'm %s!" % self.request.method

    def delete(self, *args, **kwargs):
        print "Hello, i'm %s!" % self.request.method


Hello, i'm PUT!
[06/Apr/2016 23:44:51] "PUT /de/teacher/test/ HTTP/1.1"
Hello, i'm DELETE!
[06/Apr/2016 23:57:15] "DELETE /de/teacher/test/ HTTP/1.1"

您可以将AUT设为PUT和DELETE http调用。 如果您需要使用它,您可以采取一些解决方法:

<form action="{% url 'teacher:test' %}" method="post">
    {% csrf_token %}
    <input type="hidden" name="_method" value="delete">
    <input type="submit" value="Delete">
</form>


class TestView(View):
    http_method_names = ['get', 'post', 'put', 'delete']

    def dispatch(self, *args, **kwargs):
        method = self.request.POST.get('_method', '').lower()
        if method == 'put':
            return self.put(*args, **kwargs)
        if method == 'delete':
            return self.delete(*args, **kwargs)
        return super(TestView, self).dispatch(*args, **kwargs)

    def put(self, *args, **kwargs):
        print "Hello, i'm %s!" % self.request.POST.get('_method')

    def delete(self, *args, **kwargs):
        print "Hello, i'm %s!" % self.request.POST.get('_method')


Hello, i'm delete!
[07/Apr/2016 00:10:53] "POST /de/teacher/test/ HTTP/1.1"
Hello, i'm put!
[07/Apr/2016 00:10:31] "POST /de/teacher/test/ HTTP/1.1"

它不是真正的PUT,但你可以使用相同的界面进行表单和ajax / api调用。