Question

I have some specifics to what I am doing and could not find it elsewhere here. I have a ModelForm that I present to the user and one of the fields is an input field because I set it as Charfield in my model. However I want it to be something like this on my form:

MRN ID:

and the value supplied to it is generated in my code so I know I need to pass that variable to my template.

I am not sure what the best way to do this. I use crispy forms for my forms.

My relevant form code is:

class PatientModelForm(ModelForm):

helper = FormHelper()
helper.form_tag = False
helper.layout = Layout(
    Fieldset("Patient Information:",
        Div(Div('first_name', css_class='span3'),
            Div('middle_name', css_class='span3'),
            Div('last_name', css_class='span3'),
            Div("suffix", css_class='span3'),
            css_class='row-fluid'),
        Div(Div('date_of_birth', css_class='span3'),
            Div("gender", css_class='span3'),
            Div("marital_status", css_class='span3'),
            css_class='row-fluid'),
        Div(Div("ethnicity", css_class='span3'),
            Div("age", css_class='span3'),
            css_class='row-fluid')),
    Fieldset("Contact Information:",
        Div(Div("address1", css_class='span3'),
            Div("address2", css_class='span3'),
            css_class='row-fluid'),
        Div(Div("city", css_class='span3'),
            Div("state", css_class='span3'),
            Div("zipcode", css_class='span3'),
            css_class='row-fluid'),
        Div(Div("addresstype", css_class='span3'),
            Div("county", css_class='span3'),
            Div("country", css_class='span3'),
            css_class='row-fluid'),
        Div(Div("phone", css_class='span3'),
            Div("mobile_phone", css_class='span3'),
            Div("emailaddress", css_class='span3'),
            css_class='row-fluid')),
        "comment",
        "is_newborn",
    Fieldset("Patient ID:",
         Div(Div("medical_record_number", css_class='span3'),
            Div("government_id_number", css_class='span3'),
            css_class='row-fluid')),

)

And my template is:

{% block content %}
<form method="post" class="uniForm">
    {% csrf_token %}
    {% crispy form %}
    <div class="form-actions">
        <button type="submit" class="btn btn-primary">Save</button>
        <a href="{% url "patient_list" %}" class="btn">Cancel</a>
    </div>
</form>
{% endblock %}

What would be the best way for me to show the field medical_record_number as MRN: and my generated value but not as an input, just a plain text field?

Thanks.

Was it helpful?

Solution

With crispy_forms, you can specify a template to use to render a field.

forms.py

...
Field("medical_record_number", template='your_app/uni_form/display_mrn_field.html'),
...

display_mrn_field.html

<div class="span3">MRN: {{ field.value }}</div>

That's it, it will now render that html instead of a normal input.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top