使用DeleteView删除模板中的对象而不确认
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用DeleteView删除模板中的对象而不确认相关的知识,希望对你有一定的参考价值。
我正在尝试写一个DeleteView
来删除帖子而不显示确认页面。
Del - delete button。如何立即删除对象?
urls.py
:
urlpatterns = [
# url(r'^$', views.index, name='index'),
url(
r'^feed$',
views.FeedView.as_view(),
name='feed'
),
url(r'^summary(?P<pk>w{0,50})',
views.SummaryCreate.as_view(),
name='summary'),
url(r'^summary(?P<user_id>w{0,50})/(?P<pk>w{0,50})/',
views.SummaryDelete.as_view(),
name='delete_summary'),
url(r'^dashboard$',
permission_required('reed.view_dashboard')
(views.DashboardListView.as_view()),
name='dashboard'),
url(r'^dashboard/(?P<pk>w{0,50})',
permission_required('reed.view_dashboard')
(views.DashboardUpdate.as_view()),
name='review_summary'),
]
views.朋友
class SummaryCreate(LoginRequiredMixin, generic.CreateView):
template_name = 'template/summary_list.html'
model = Summary
form_class = AddUrlForm
login_url = '/login_page/login/'
redirect_field_name = 'login_page'
def get_context_data(self, **kwargs):
return dict(
super(SummaryCreate, self).get_context_data(**kwargs),
summary_list=reversed(Summary.objects.filter(user_id=self.kwargs['pk']).reverse())
)
def get_success_url(self):
return reverse('summary', args=(self.request.user.id.hex,))
def form_valid(self, form):
print(self.request.user.id.hex)
url_inst = form.save(commit=False)
keywords_inst = Keywords
article = Article(form.cleaned_data['url'], language='en')
article.download()
article.parse()
title = article.title
print(title)
try:
image = article.top_image
print(image)
except Exception:
image = ''
article.nlp()
try:
keywords = article.keywords
print(keywords)
except Exception:
keywords = 'Sorry,no,keywords,found'
try:
summary = article.summary
print(summary)
except Exception:
summary = 'Sorry, no summmary found'
try:
publish_date = article.publish_date
publish_date = publish_date.date()
print(publish_date)
except Exception:
publish_date = '1900-01-01'
user = User.objects.get(id=self.request.user.id.hex)
url_inst.url=form.cleaned_data['url']
url_inst.image=image
url_inst.title=title
url_inst.summary=summary
url_inst.date=publish_date
url_inst.user_id=user
url_inst.save()
summary = Summary.objects.get(url=form.cleaned_data['url'])
#
for keyword in keywords:
new_keyword = keywords_inst(keyword=keyword, keyword_id=summary)
new_keyword.save()
#
return super(SummaryCreate, self).form_valid(form)
class SummaryDelete(SummaryCreate, generic.DeleteView):
model = Summary
pk_url_kwarg = 'pk'
slug_url_kwarg = 'pk'
def get_success_url(self):
return reverse('summary', args=(self.request.user.id.hex,))
def dispatch(self, request, *args, **kwargs):
return super(SummaryDelete, self).dispatch(request, *args, **kwargs)
template.html:
<form action="{% url 'delete_summary' user.id.hex summary.id.hex %}" method="post">{% csrf_token %}
<h3>
<input type="submit" class="delete" aria-hidden="true" value="X">
{{summary.title}}
</h3>
</form>
我在一个模板中有两个类:1个用于显示所有帖子并添加新帖子,第二个用于删除,但删除仅重定向我在页面上,我为DeleteView提供。
答案
显示确认页面并删除现有对象的视图。仅当请求方法为POST时才会删除给定对象。如果通过GET获取此视图,它将显示一个确认页面,其中应包含POST到同一URL的表单。
您需要一个表单元素才能发送POST
请求。
template.html
:
<form id="my_form" method="post" action="{% url 'delete_summary' user.id.hex summary.id.hex %}">
{% csrf_token %}
</form>
<a href="#" onclick="document.getElementById('my_form').submit();">Del</a>
以上是关于使用DeleteView删除模板中的对象而不确认的主要内容,如果未能解决你的问题,请参考以下文章
我可以同时使用 UpdateView 和 DeleteView 制作视图吗?
Django:我可以以相同的形式使用 CreateView 和 DeleteView 吗?