mercredi 16 juin 2021

what exactly does request.user return in django, can we compare it with a string?

I am new to Django and trying out somethings. I am thinking to return a page named "in_accessable.html" if a normal user access the "\create" page (which calls the function named medicine_create_view"), else medicine_create.html.

The name of admin is "npend" so I gave a if statement to check if the user name is 'npend' or not, I used request.user method to get the user name, even if I access the page with admin I am getting a false in if statment I tried printing request.user == "npend", which prints False every time.

Can anyone help me rectifing this problem, Thank You.

Code Used:

def medicine_create_view(request, *args,**kwargs):
    my_form = Raw_Medicine_Form()
    if request.method == 'POST':
        my_form = Raw_Medicine_Form(request.POST)
        if my_form.is_valid():
            print(my_form.cleaned_data)
            Medicine.objects.create(**my_form.cleaned_data)
        else:
            print(my_form.errors)
    my_form = Raw_Medicine_Form()
    context ={
        "form" : my_form,
    }

    #print(request.user=="npend") 
    if(request.user=="npend"): return render(request,"App_1/medicine_create.html",context)
    else: return render(request,"error_files/not_acessable.html",{})

Aucun commentaire:

Enregistrer un commentaire