0

Working on school project. I am trying to delete messages by only the people that were logged in and have created it. I want other user to see them but only the creator can delete it. I have code that will delete everything in the database. I researched and found that Mysecret.objects.filter(id=request.session['user_id']).delete() should work but when i do this the page wont delete anything just refreshes the page.

I will only post views and model as i know everything else works. I think its just the format that I cant seem to nail down.

SO what I have gotten up to now is I have my message id which is secret.id and i have the creators id which is secret.creator.id and the session id request.session['user_id']. how do i compare them to delete only tha tmessage

Views.py 

from django.shortcuts import render, redirect
from . models import Mysecret
from ..logReg.models import User


# Create your views here.

def index(request):
    context = {

    "secret": Mysecret.objects.all(),

    }

    return render(request, 'secretdojo/index.html', context)


def create(request):
    secreteid= User.objects.get(id=request.session['user_id'])

    Mysecret.objects.create( secret=request.POST['message'], creator=secreteid)

    return redirect( 'secretdojo:index')

def removesecret(request):


    Mysecret.objects.filter(id=request.session['user_id']).delete()



    return redirect( 'secretdojo:index')


def topsecret(request):
    context = {


    }

    return redirect( '/')

model.py 


from __future__ import unicode_literals
from django.db import models
from ..logReg.models import User

class Mysecret(models.Model):
    secret = models.CharField(max_length =500)
    created_at = models.DateTimeField(auto_now_add = True)
    updated_at = models.DateTimeField(auto_now = True)
    loguser = models.ManyToManyField(User, related_name='loguser')
    creator = models.ForeignKey(User, related_name='creator')

The foreignKey part of the User model:

class User(models.Model):
    first_name = models.CharField(max_length = 50)
    last_name = models.CharField(max_length = 50)
    email = models.CharField(max_length = 100)
    password = models.CharField(max_length =100)
    created_at = models.DateTimeField(auto_now_add = True)
    updated_at = models.DateTimeField(auto_now = True)

    objects = UserManager()
4
  • please post mysecret model too Commented Mar 22, 2017 at 18:07
  • If creator contains the creator then why are you comparing the user to id? Commented Mar 22, 2017 at 18:07
  • ta is the my seceret model Commented Mar 22, 2017 at 18:07
  • because that id is only in the other def i doesn't follow down. At least when i tried that it broke Commented Mar 22, 2017 at 18:08

5 Answers 5

1

You should something like this :

Mysecret.objects.filter(creator=request.user).delete()
Sign up to request clarification or add additional context in comments.

1 Comment

@DavidHollenbeck : it would work fine if you use Django Auth.. or make changes changes accordingly.
1

Suggestion: don't keep your own User model, but use the Django's Authentication system

In terms of your code I believe you "create" code also doesn't work and it is because of this line Mysecret.objects.filter(id=request.session['user_id']). As this will not return anything. You are searching in model Mysecret, but with User ID, which will not return anything. Instead you need to have:

 Mysecret.objects.filter(creator=request.session['user_id'])

If you use Danjgo's Auth, you will have request.user and can do following:

 Mysecret.objects.filter(creator=request.user)

One other note: What is the reason of having loguser field for Mysecret model? It seems redundant for your case,

4 Comments

its for a many to many for the loguser for more apps later to be added. and your method above does not break but does not delete just refreshes the page.
Please try debugging by printing the value of Mysecret.objects.filter(creator=request.session['user_id']) to see what does it return. If it returns None, then your query doesn't work. Are you sure that you store your user ID in the session?
I know im getting the id due to the fact that I have them printing currently in my html Id user Id message Course Name Date Added Action 24 1 This is a test I hope it works March 22, 2017, 10:50 a.m. Remove 25 1 March 22, 2017, 11:12 a.m. Remove 26 1 March 22, 2017, 11:16 a.m. Remove
Can you please try printing the result of your query in the view and look in the log to see what it prints?
0

To delete all the messages created by creator

Mysecret.objects.filter(creator=request.session['user_id']).delete()

To delete message by id and only by its creator

eg: assume secret_id is the id of the message

Mysecret.objects.filter(creator=request.session['user_id'],id =secret_id ).delete()

Comments

0

The answer is as follows.

mainid = Mysecret.objects.get(id=id)
userid =User.objects.get(id=request.session['user_id'])

if mainid.creator != userid:
    return False
mainid.delete()

 <td>{{ dog.created_at }}</td>
                {% if dog.creator.id == request.session.user_id %}
                <td><form action="{% url 'secretdojo:remove' id=dog.id %}">{% csrf_token %}<input class="btn btn-default" type="submit" value="Remove"></form></td> 
                {% endif %}
            </tr>
            {% endfor %}
        </table>

Comments

0

To Delete a specific product from the session cart by id:

` def DeleteProductView(request,id):

cart = request.session.get('cart')
product = Product.objects.get(id=id)
var = str(product.id)
if request.method == 'POST':
    request.session.modified = True
    cart.pop(var)`

Comments

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.