Answer the question
In order to leave comments, you need to log in
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 %}
<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
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 questionAsk a Question
731 491 924 answers to any question