0

i am creating a blog just for practice and i want to allow some users to add/delete a post how to add this BooleanField to users and check if allowed in templates

mohammedgqudah
  • 568
  • 4
  • 15
  • Django comes with a [permissions system](https://docs.djangoproject.com/en/1.11/topics/auth/default/#topic-authorization). You should give your users `add_post` and `delete_post` permissions (or add them to a group that has those permissions), rather than trying to add a new boolean field to your model. – Alasdair Oct 03 '17 at 17:19
  • thank you but how to check them in templates is it {% if user.allowed%}.... – mohammedgqudah Oct 03 '17 at 17:32
  • There are many tutorials and documentations about this, quick google: [Django Tutorial Part 8: User authentication and permissions](https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/Authentication). Search for `Permissions` on that page, and you'll jump right into the permissions part in the models and how to check them in your templates. Not satisfied? [google](https://www.google.nl/search?q=django+permissions+template&oq=django+permissions+template&aqs=chrome.0.0l2j69i64l3.5679j0j7&client=ubuntu&sourceid=chrome&ie=UTF-8) – Nrzonline Oct 03 '17 at 18:03
  • The page I linked to shows you how, in the [authentication data in templates](https://docs.djangoproject.com/en/1.11/topics/auth/default/#authentication-data-in-templates) section. – Alasdair Oct 03 '17 at 18:29

1 Answers1

0

It sounds like what you are talking about is an extension beyond the default user auth. Something like this should work:

from django.db import models
from django.contrib.auth.models import User

PERMS = (('E', 'Editor'),('P', 'Poster'),...)
class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    my_custom_permission_levels = modelsCharField(max_length=1, choices=PERMS)
    ...

And you could access consistent with this post - Django accessing OneToOneField

profile = Profile.objects.get(user=request.user)

And add that to the context to access in the template.

sahutchi
  • 2,223
  • 2
  • 19
  • 20