0

I have this request_control.html :

<form method="post" action="{% url 'cost_control_app:request_update' form_request_update.instance.request %}">
    {% csrf_token %}
    <div class="row">
            {%include "partials/field.html" with field=form_request_update.request %}<br clear="all"/>
            {%include "partials/field.html" with field=form_request_update.request_date %}<br clear="all"/>
            {%include "partials/field.html" with field=form_request_update.request_type %}<br clear="all"/>
            {%include "partials/field.html" with field=form_request_update.request_description %}<br clear="all"/>
            {%include "partials/field.html" with field=form_request_update.user_login %}<br clear="all"/>
            {%include "partials/field.html" with field=form_request_update.state %}<br clear="all"/>
            {%include "partials/field.html" with field=form_request_update.closed_date %}<br clear="all"/>
    </div>
    {% for group in user.groups.all %}
        {% if group.name == 'IC Admins' %}
            <input type="submit" class="btn btn-primary" value="Atender"/>  
        {% endif %}
    {% endfor %}

</form>

And i need to set some of these fields as readonly, how can i do it ??

Thanks for your help !!

jsanchezs
  • 1,992
  • 3
  • 25
  • 51
  • ideally, you should be doing this at the form level. Example: http://stackoverflow.com/questions/324477/in-a-django-form-how-to-make-a-field-readonly-or-disabled-so-that-it-cannot-b – karthikr Dec 28 '15 at 20:09
  • I can't, because depending of which user access, i need to enable or disable some of this fields.... – jsanchezs Dec 28 '15 at 20:16
  • 2
    Would you not be able to manage that in the `__init__` ?you can pass the request object into the form, and handle this logic in there.. (just a suggestion to reduce the logic in the template) – karthikr Dec 28 '15 at 20:18
  • There has to be another way jeje...thanks, gonna try it anyway. – jsanchezs Dec 28 '15 at 20:25
  • I am not saying it is not possible. It is just easier when django generates those templates for you. You can look at how the admin template implements readonly fields : https://github.com/django/django/blob/53ccffdb8c8e47a4d4304df453d8c79a9be295ab/django/contrib/admin/templates/admin/includes/fieldset.html – karthikr Dec 28 '15 at 20:29
  • based on that, i'll have to create a different form for each user ?? – jsanchezs Dec 28 '15 at 22:04
  • Not really. You just need different widgets/attributes on widgets based on criteria – karthikr Dec 28 '15 at 22:05
  • Exactly, i've been trying this : {%include "partials/field.html" with field=form_request_update.request|attrs['readonly']=True %} But doesn't work either.... – jsanchezs Dec 29 '15 at 14:17

1 Answers1

1

Got it !!

First, i must call this line in the template so it can recognize the widget tweaks:

{% load widget_tweaks %}

Then call the property inside the include like this:

{%include "partials/field.html" with field=form_request_update.request|attr:"readonly:True" %}

Works like a charm.

jsanchezs
  • 1,992
  • 3
  • 25
  • 51