Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete Object in Template using DeleteView without Confirmation

I'm trying to write a DeleteView for deleting posts without getting displayed a confirmation page.

Del - delete button. How can I delete the object immediately?

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.py

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>

I have 2 classes in one template: 1 for displaying all posts and adding new posts and second for deleting, but deleting only redirect me on page, that I provide for DeleteView.

like image 771
Kirill Kiyko Avatar asked Aug 26 '26 05:08

Kirill Kiyko


1 Answers

DeleteView:

A view that displays a confirmation page and deletes an existing object. The given object will only be deleted if the request method is POST. If this view is fetched via GET, it will display a confirmation page that should contain a form that POSTs to the same URL.

You need a form element in order to send a POST request.

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>
like image 136
Yannic Hamann Avatar answered Aug 28 '26 22:08

Yannic Hamann



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!