如何将动态url值传递到上下文字典中



blog_confirm_delete.html

<form method="POST" action="">
{% csrf_token %}
<h2>Are you sure you would like to delete this post?</h2>
<button type="submit" value="send" class="btn btn-danger">Yes, Delete</button>
<button class="btn btn-info mb-8">
<a class="text-white" href="{% url 'blog-detail' post.id %}">Cancel</a>
</button>
</form>

views.py

class BlogDeleteView(SuccessMessageMixin,LoginRequiredMixin,TeacherOwnerPassesTestMixin,DeleteView):
model = Post
template_name = "postings/blog_confirm_delete.html"
success_url = reverse_lazy('blog-list')
success_message = "Your post has been deleted!"
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['list_title'] = "Our Blog"
context['list_url'] = "blog-list"
context['detail_title'] = "Delete Post"
context['cancel'] = ???
return context

所以目前我不得不硬编码";取消";按钮的url用于每个";X_confirm_delete.html";我拥有的模板。现在我想为每个DeleteView传递一个动态url值,这样我就可以使用一个简单的模板来减少所有DeleteView。我该怎么做?

谢谢你提前帮我!

您可以使用'HTTP_REFERER'返回该视图。像这样:

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['list_title'] = "Our Blog"
context['list_url'] = "blog-list"
context['detail_title'] = "Delete Post"
context['cancel'] = self.request.META.get('HTTP_REFERER', '/')
return context

并将其添加到模板:

<a class="text-white" href="{{cancel}}">Cancel</a>

相关内容

  • 没有找到相关文章

最新更新