H
H
howuu2019-07-02 02:40:46
Django
howuu, 2019-07-02 02:40:46

Django adding attributes to input via if?

I have a user profile page, it is displayed in a form that the user can edit and submit, I want the other users who enter this page to have a read-only attribute in the form, here is the condition

{% if request.user.username !=  user_name%} readonly {% endif %}

however, just inserting it into input at this point does not work, nor does it work to specify false in the readonly attribute, how can I do that?
<input    {% if request.user.username != user_name%} readonly {% endif %}
    name="{{form.user_name.html_name}}" required type="text" class="form-control" id="username" placeholder="Username" required value="{{user_name}}">

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
marazmiki, 2019-07-02
@marazmiki

But what happens if one of these other users, through the inspector, removes the readonly attribute from the field, changes the value and submits the form? :)
As a means of protecting readonly - well, that's it.
I would advise you to simply display the fields, styling them to fit the form. The rest of the solutions involve creating your own forms, widgets and similar entities, the use of which is an overkill

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question