3

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.

1 Answer 1

4

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>
Sign up to request clarification or add additional context in comments.

9 Comments

Yes, previously i did it with form and post-method, but had only redirection on {% url 'delete_summary' user.id.hex summary.id.hex %}
Does it work now? Is the object deleted without asking for a confirmation?
No, it doesn't work, when i'm clicking on Del-button, I've only redirect to this page {% url 'delete_summary' user.id.hex summary.id.hex %}
As you can see in code I have print in get function, but i don't have any prints in console.
Try to replace get with post since my example triggers this function on the view.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.