how to give permission to a logged in specific user in a list of users profile

34 views
Skip to first unread message

Exactly musty

unread,
Jul 5, 2020, 3:58:25 PM7/5/20
to Django users
how can i specify a user in django templates, for example i have a page which consist of list of users available but i want it to show "Talk to a patient" for only the logged in user on his own profile here is what am saying in picture and here is my code, i actually don't know how to put the code i have tried many code but it does show Talk to a patient for every users or it wont show at all,i attached a picture to it,that shows what am saying,i checked the doc but couldn't find anything to use,so i wanted to try the custom templates tags,but i still don't how to comport the code in the templatetags views

Models.py



class Doctor(models.Model):
    GENDER_CHOICES = (
        ('Male', 'Male'),
        ('Female', 'Female'),
    )
    user = models.OneToOneField(CustomUser, on_delete=models.CASCADE, null=True, related_name="doctor")
    image = models.ImageField(default='jazeera.jpg', upload_to='profile_pics')
    full_name = models.CharField(max_length=100)
    bio = models.TextField()
    speciality = models.CharField(max_length=300)
    describtion = models.CharField(max_length=100)
    status = models.ManyToManyField(Status)
    gender = models.CharField(max_length=6, choices=GENDER_CHOICES)
    location = models.CharField(max_length=100)
    certification = models.CharField(max_length=300)
    place_of_work = models.CharField(max_length=300)
    
   


##template

{% if request.user == doctor %}
                    <p><a href="{% url 'chat' %}class="btn btn-primary">Talk to a Patient</a></p>
                      {% endif %}




tk.jpg

Arpana Mehta

unread,
Jul 5, 2020, 5:44:25 PM7/5/20
to django...@googlegroups.com
You can user permission classes in your code. Or decorators before the function view you want to be only visible to logged in users.

from django.contrib.auth.decorators import login_required


@login_required
def your_view_here(request):


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/4278d2eb-53a1-4b9c-89fa-a5d09f96c458o%40googlegroups.com.

Arpana Mehta

unread,
Jul 5, 2020, 5:47:45 PM7/5/20
to django...@googlegroups.com

Gabriel Araya Garcia

unread,
Jul 5, 2020, 6:06:26 PM7/5/20
to django...@googlegroups.com
I also, I have build one program with health environment, and the user controlls is following manner:

def login_ini(request):
variable1 = 'Pantalla de Acceso al Sistema'
error_log = 'ok'
username = request.POST.get('username')
password = request.POST.get('password') # valor del template
user = auth.authenticate(username=username, password=password)
if request.method == "POST":
if user is not None and user.is_active:
# Correct password, and the user is marked "active"
auth.login(request, user)
request.session['username_x'] = username # variable gobal
request.session['id_x'] = user.id # variable gobal
#return HttpResponse(str(user.id))
return HttpResponseRedirect("principal")

error_log = "error"

context ={"variable1":variable1,"error_log":error_log,}
return render(request,"login_ini.html",context)
context ={
'user':user,
"variable1":variable1,
"error_log":error_log,
}
return render(request,'login_ini.html',context)


def log_out(request):
id_x = request.session.get('id_x')
log_out_x  = datetime.now()
cursor = connection.cursor()
cursor.execute(
"update auth_user set log_out=%s where id=%s",[log_out_x,id_x]  #
)
logout(request)
return redirect('login_ini')

and in template:
   {% if perms.ai.btn_acep_fichacui %}
<button type="submit" id="submit" name = "submit" class="btn btn-primary">Aceptar</button>
{%else%}
<a style="font-size:14px" role="button" class="btn btn-primary" onclick="alert('Usted no esta autorizado a grabar cambios a la ficha')">Aceptar</a>
{%endif%}

if you have some question, contact me to: gabriela...@gmail.com


Gabriel Araya Garcia
GMI - Desarrollo de Sistemas Informáticos
99.7721.15.70



Reply all
Reply to author
Forward
0 new messages