1

I am using django-widget-tweaks and its render_field. I have an odd situation and I wonder if there is a workaround or a different approach that I should use?

I am using django and allauth (I am not using the social auth part yet). I have the default django user model and I have a "Profile" model that extends the user model. That's all working.

This is a specialized "Membership App". Only adults over 18 should be able to login. Once logged in a person can create "member" one or more "member" records. The logged in person is responsible for payng for memberships but might not be a member themselves; for example, a parent might log in and create member records for youth under 18 while not being a member themselves.

When creating a new member record I need to give the logged in person the opportunity to create a member record for themselves (if they don't already have one). If they chose this option then the logged in person's "Profile" info should auto-fill the new member form. To facilitate this I am capturing and serializing the logged in user's Profile info and am passing it to the CBV MemberCreate method. This method renders the Member Create form using django-widget-tweaks and its render_field method, a la:

{% with field=newMemberForm.first_name %}
    {% render_field field class="form-control form-control-sm" %}
{% endwith %}

I want to create a data-loggedindata= attribute that uses the field.name value as the "member name" of the logged in person's profile. SOmething like:

{% with field=newMemberForm.first_name %}
    {% render_field field class="form-control form-control-sm" data-loggedindata=LoggedInProfile.{{field.name}} %}
{% endwith %}

But django-widget-tweaks doesn't seem to understand that and I am not wise enough in the world of django to know a workaround.

I can hear someone typing: "but you know the field name from the {% with ... %} construct". Yes, that is true but the Member form has a lot of fields so I wrapped the render_field part in a small html file that I include. So the reall segment above might look like:

{% with field=profileForm.first_name %}
    <div class="form-group col col-3">
        {% include "Members/template_includes/field_render.html" %}
    </div>
{% endwith %}

And the included file has the render_field in it. Inside that included html file I have no idea of the name of the field.

ideas?

Edit

I have tried Pankaj's suggestion below but

{% render_field field|attr:"data-loggedindata=LoggedInProfile.{{field.name}}" %}

does not appear to interpolate the object LoggedInProfile or the {{field.name}} value. What I get is the un-interpolated string "LoggedInProfile.{{field.name}}".

I have tried:

{% render_field field|attr:"data-loggedindata:getattr(LoggedInProfile, field.name" %}

but that is not interpolated either. Is there a way in a Django template and/or widget-tweaks to programatically get the value of an object attribute?

Edit 2

I decided to go another way. I am still using widget-tweaks but I am not trying to use render_field in this way anymore. I am serializing the data that would be placed in the data- elements and placing the JSON version in a hidden form field. I am letting javascript deal with it now.

7 Reeds
  • 2,419
  • 3
  • 32
  • 64

1 Answers1

1

If you want to add attribute in field with django-widget-tweaks you can simply use attr for that, in your case it would be like -

{% with field=newMemberForm.first_name %}
    {% render_field field|add_class:"form-control form-control-sm"|attr:"data-loggedindata=LoggedInProfile.{{field.name}}" %}
{% endwith %}

Still have any doubts let me know

Pankaj Sharma
  • 2,185
  • 2
  • 24
  • 50