1

I new to django and I am trying to make a web application. I have this page page image and I want to delete one post when I press the delete button. How can I do that? This is my modal for 'Post' :

class Post(models.Model):
    created_date = models.DateTimeField()
    title = models.CharField(max_length=100)
    profile_image = models.ImageField(upload_to='poze', blank=True, null=True)
    text = models.CharField(max_length=1000, default='Nimic', blank=True)
    user = models.ForeignKey(UserProfile, on_delete=models.CASCADE)

I've been looking for delete methods, but I've always found form-only methods and I don't use form. Thank you.

JPG
  • 82,442
  • 19
  • 127
  • 206
Alex
  • 65
  • 1
  • 2
  • 7

3 Answers3

8

In you html :

<a href="{% url 'delete' p.id %}">Delete</a>

assuming that you are using a for loop :

{% for p in posts %}   

In your urls :

path('delete/<post_id>',views.delete_post,name='delete')

In your views :

def delete_post(request,post_id=None):
    post_to_delete=Post.objects.get(id=post_id)
    post_to_delete.delete()
    return HttpResponseRedirect(#name of the view function that returns your posts page)

And that's it

EDIT

This method deletes data from your database directly. So I recommend you add the @login_required decorator to your delete_post view function to protect your post. You can also make it accessible only for admin users or post owners in your html (Example : only users who have staff role can see the delete link)

{% if user.is_staff %}
<a ...>Delete</a>
{% endif %}
Ines Tlili
  • 790
  • 6
  • 21
  • 1
    this is highly insecure because everybody could call these URLs and post would be deleted. at least add a "login_required" much better is to check the owner of the post and with an extra "if request.user = owner...delete()" – hansTheFranz Aug 10 '18 at 09:01
  • Sure, he will add those depending on his needs. I just explained the process and eventually that's what he asked for. – Ines Tlili Aug 10 '18 at 09:34
  • I edited my answer built on your comment, thank you. – Ines Tlili Aug 10 '18 at 09:51
  • yes just wanted to make it clear, some people simply "copy&paste" code and this piece here can be dangerous without any user authenticated checks. Glad you edited it :) – hansTheFranz Aug 10 '18 at 10:06
  • Totally agree ! – Ines Tlili Aug 10 '18 at 10:06
1

You need to pass argument with post id. It would like something like this

p = Post.objects.get(pk=2)
p.delete()
Tomasz
  • 1,895
  • 1
  • 14
  • 17
1

You should create a new field in the database table. Whenever you are deleting the data then you should change the delete field.

class Post(models.Model):
    created_date = models.DateTimeField()
    title = models.CharField(max_length=100)
    profile_image = models.ImageField(upload_to='poze', blank=True, null=True)
    text = models.CharField(max_length=1000, default='Nimic', blank=True)
    user = models.ForeignKey(UserProfile, on_delete=models.CASCADE)
    delete_flag = models.BooleanField(default=False)

And whenever you are should the data then you should filter the data on the delete flag

Post.objects.filter(delete_flag=False)

Though it is subjective and I don't know you use case, but still as a beginner its better to start with this practice.

Read More

Article 2

Arghya Saha
  • 5,599
  • 4
  • 26
  • 48