Django Many To Many Field Data Rendering
I wanted to create a blog site where an author and editor can both have the edit option and the editors will be assigned by author. Now my model field looks like this : class Edito
Solution 1:
First of all.
{% if request.user.author == blogs.author or blogs.editor %}
<ahref="#"class="btn btn-warning">Edit</a>
{% endif %}
In this piece of code, the condition is not very good defined. My suggestion is that you make this in this way:
{% if request.user.author == blogs.author or request.user.author == blogs.editor %}
<ahref="#"class="btn btn-warning">Edit</a>
{% endif %}
This is one suggestion.
Now another posibility is to use JavaScript.
In order to do that, I give you some code example of my own.
<scripttype="text/javascript">var editor = '{{user.author.editor}}'if (editor == 'False'){
document.getElementById('edit').innerHTML = ''
}
</script>
In this code, I'm defining that if the user is not a editor, I will not hide the element (In html) with id 'edit'. So in your template, set an id to your element.
<a id="edit" href="#" class="btn btn-warning">Edit</a>
For this, you will have to set a boolean field in your model. You can do it like this:
class Editor(models.Model):
name = models.OneToOneField(Author, on_delete=models.CASCADE, null=True, blank=True)
editor = models.Boolean()
Post a Comment for "Django Many To Many Field Data Rendering"