For my django app I want to enable the user to select whether a post is public or private.
This is my best attempt so far:
views.py
def home(request):
recipes = models.Recipe.objects.filter(recipe_private, recipes)
context = {
'recipes': recipes
}
def recipe_private():
for recipe in recipes:
if recipe(is_private=True) and recipe.author == self.request.user:
True
else:
False
return render(request, "recipes/home.html", context)
And here is my models.py:
class Recipe(models.Model):
title = models.CharField(max_length=100)
description = models.TextField()
author = models.ForeignKey(User, on_delete=models.CASCADE)
serving = models.PositiveIntegerField(default=1)
temperature = models.PositiveIntegerField(default=1)
prep_time = models.PositiveIntegerField(default=1)
cook_time = models.PositiveIntegerField(default=1)
##tags = models.ManyToManyField('Tag')
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
id = models.CharField(max_length=100, default=uuid.uuid4, unique=True, primary_key=True, editable=False)
is_private = models.BooleanField(default=True)
def get_absolute_url(self):
return reverse('recipes-detail', kwargs={"pk": self.pk})
def __str__(self):
return str(self.title)
The logic works in my head but not in practice, how would I improve this? I want it so only the author can see their own private posts, anyone who isnt the author shouldnt be able to see a post marked as private.