1
我試圖將示例應用程序從Rest Framework教程轉換爲模板應用程序。問題是我無法在單個記錄的詳細視圖中創建刪除按鈕。 我views.py如下:Django Rest_Framework:在模板中調用刪除方法
class SnippetViewSet(viewsets.ModelViewSet):
queryset = Snippet.objects.all()
serializer_class = SnippetSerializer
permission_classes = (permissions.IsAuthenticatedOrReadOnly,
IsOwnerOrReadOnly,)
renderer_classes = [renderers.TemplateHTMLRenderer]
template_name = 'list.html'
def list(self, request, **kwargs):
print(request.method, request.user)
queryset = Snippet.objects.all()
serializer = SnippetSerializer(context={'request': request})
return Response({'queryset': queryset, 'serializer': serializer}, template_name='list.html')
def retrieve(self, request, *args, **kwargs):
print(request.method, 'retrieve')
queryset = self.get_object()
serializer_class = SnippetSerializer(queryset, context={'request': request})
return Response({'queryset': queryset, 'serializer': serializer_class}, template_name='retrieve.html')
def create(self, request, *args, **kwargs):
print(request.method, 'create')
serializer = SnippetSerializer(instance=None, context={'request': request}, data=request.data)
if serializer.is_valid():
serializer.save(owner=self.request.user)
return redirect('snippet-list')
def post(self, request, *args, **kwargs):
print(request.method, 'post')
queryset = self.get_object()
serializer = SnippetSerializer(queryset, data=request.data, context={'request': request})
if not serializer.is_valid():
return Response({'serializer': serializer, 'queryset': queryset})
serializer.save()
return redirect('snippet-list')
def destroy(self, request, *args, **kwargs):
instance = self.get_object()
self.perform_destroy(instance)
return Response(status=status.HTTP_204_NO_CONTENT)
def perform_destroy(self, instance):
instance.delete()
urls.py保持不變:
snippet_list = SnippetViewSet.as_view({
'get': 'list',
'post': 'create',
})
snippet_detail = SnippetViewSet.as_view({
'get': 'retrieve',
'put': 'update',
'patch': 'partial_update',
'delete': 'destroy',
})
user_list = UserViewSet.as_view({ 'get': 'list'})
user_detail = UserViewSet.as_view({
'get': 'retrieve',
'put': 'update',
'patch': 'partial_update',
'delete': 'destroy'
})
urlpatterns = format_suffix_patterns([
url(r'^', snippet_list, name='snippet-list'),
url(r'^snippets/$', snippet_list, name='snippet-list'),
url(r'^snippets/(?P<pk>[0-9]+)$', snippet_detail, name='snippet-detail'),
url(r'^users/$', user_list, name='user-list'),
url(r'^users/(?P<pk>[0-9]+)/$', user_detail, name='user-detail'),
])
表單模板:
<form action="{% url 'snippet-detail' pk=queryset.pk %}" method="post">
{% csrf_token %}
{% render_form serializer %}
<input type="submit" value="Save">
<a href="{% url 'snippet-detail' pk=queryset.pk %}" methods="delete">delete</a>
所有點擊後,我能得到刪除按鈕將重新加載檢索視圖。除了刪除方法以外的所有東西都能正常工作我知道有一些我很想念的東西,但如果你能指出我朝着正確的方向看,我會很感激。
不會這樣做,不幸的是。該按鈕會生成一個帶有令牌的鏈接,如:'http:// localhost:8000/snippets/15 /?csrfmiddlewaretoken =',但保持在同一頁面上。 – ZmuA
你可以檢查單擊按鈕時所做的請求的詳細信息嗎?特別是方法和響應。你的銷燬視圖返回一個HTTP_204_NO_CONTENT響應,我想解釋爲什麼你在提交表單後保持同一頁面。 [見:](https://stackoverflow.com/questions/3283071/is-there-a-way-to-ignore-form-response) –
我得到GET方法。不確定響應的類型,但是HTTP響應是200. – ZmuA