我正在尝试创建一个位于HTML表格中的按钮,单击此按钮将从数据库中删除记录(本例中的文章)。我通过文章pk就像我已经完成其他链接,但我无法弄清楚如何使删除发生。我在网上搜索了帮助文件,但我是新手,真的需要有人为我解决这个问题。 URL和视图应该是什么样的?
HTML:
<a href="{% url 'remove_article', article_pk=articles.pk %}"><span class="glyphicon glyphicon-remove-circle"></span></a>
URLS(第二个网址):
urlpatterns = [
url(r'^$', views.CompanyList.as_view(), name='company_list'),
url(r'^company/(?P<pk>[0-9]+)/$', views.CompanyDetails.as_view(), name='company_details'),
url(r'^company/(?P<pk>[0-9]+)/remove$', views.CompanyDetails.delete_article(), name='remove_article'),
url(r'^company/transcript/(?P<transcript_id>[0-9]+)/$', views.TranscriptList.as_view(), name='transcript_details'),
]
查看(本页):
class CompanyDetails(generic.DetailView):
model = Company
template_name = 'company_details.html'
context_object_name = 'articles'
def get_queryset(self):
return Articles.objects.filter(company_id=self.kwargs.get('company_id')).order_by('-date')
def get_context_data(self, **kwargs):
pk = self.kwargs.get('pk')
context = super(CompanyDetails, self).get_context_data(**kwargs)
context['articles'] = Articles.objects.filter(company_id=pk).order_by('-date')
context['company'] = Company.objects.filter(id=pk)
context['transcripts'] = Transcripts.objects.filter(company_id=pk)
return context
# Here is where I'm struggling...
def delete_article():
article = Articles.objects.get(pk='article_pk')
article.delete()
答案 0 :(得分:1)
Django内置https://math.meta.stackexchange.com/questions/5020/mathjax-basic-tutorial-and-quick-reference,所以最好使用它。此外,出于安全原因,您确实应该使用表单,以便可以POST到此视图。 (链接的DeleteView提供了一个示例。)