I just started learning django. i created a model and i expect when a user logs in, the user should see only info it saved but when another user logs in, it sees saved information from other users
view.py
def user_locker(request):
saved_info = Locker.objects.all()
all_saved_info = {'saved_info': saved_info}
return render(request, 'pwdbank/user_locker.html', all_saved_info)
model.py
class Locker(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
site_name = models.CharField(max_length=55)
site_url = models.URLField(max_length=55)
username = models.CharField(max_length=55)
email = models.EmailField(max_length=100)
password = models.CharField(max_length=55)
created_date = models.DateField(auto_now_add=True)
updated_date = models.DateField(auto_now=True)
def __str__(self):
return f'{self.site_name}'
Thank you. Please Help!
CodePudding user response:
You should keep the request
to be allowed only for logged in users.
from django.contrib.auth.decorators import login_required
@login_required
def user_locker(request):
saved_info = Locker.objects.filter(user=request.user)
all_saved_info = {'saved_info': saved_info}
return render(request, 'pwdbank/user_locker.html', all_saved_info)
Reference : https://docs.djangoproject.com/en/4.1/topics/auth/default/#the-login-required-decorator