Can anybody answe how i can access infromation from the parent modell i my HTML template in Django?

17 views
Skip to first unread message

The Sha

unread,
Jun 30, 2020, 4:37:16 AM6/30/20
to Django users


I have a two formsets rendered in a view called ContactIndex, the parent model for this view is the CustomUser model. I want to present the first_name of the user object in my html template. I've tried these tags without success any advise?

This is the tags in my HTML template:

{{ customuser.first_name }} - does not work

{{ object.first_name }} - does not work


This is my view:

def ContactIndex(request, CustomUser_id):
        customuser = CustomUser.objects.get(pk=CustomUser_id)
        if request.method == "POST":
            ContactFormset = ContactInlineFormSet(request.POST, request.FILES, instance=customuser)
            AddressFormset = AddressInlineFormSet(request.POST, request.FILES, instance=customuser)
            if ContactFormset.is_valid() or AddressFormset.is_valid():
                AddressFormset.save()
                ContactFormset.save()
                # Do something. Should generally end with a redirect. For example:
                return redirect ('ContactIndex', CustomUser_id=customuser.id)
        else:
            ContactFormset = ContactInlineFormSet(instance=customuser)
            AddressFormset = AddressInlineFormSet(instance=customuser)
        return render(request, 'members/member_contact_form.html', {'ContactFormset':ContactFormset, 'Address

Formset':AddressFormset })

Andréas Kühne

unread,
Jun 30, 2020, 5:00:53 AM6/30/20
to django...@googlegroups.com
Hi,

You need to pass the variables that you want to use in the template to the template itself. You do this here:
return render(request, 'members/member_contact_form.html', {'ContactFormset':ContactFormset, 'AddressFormset':AddressFormset })
The dictionary you add to the render method there contains all the objects that you can get in the template itself. So if you want to add something called object you need to add that to the dictionary.

Med vänliga hälsningar,

Andréas

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/865ff205-9a1d-4f62-bbe4-d2d12da22374o%40googlegroups.com.

The Sha

unread,
Jun 30, 2020, 5:05:32 AM6/30/20
to Django users
Ok i understand, how du i put the CustomUser in the render method so i cant get {{ CustomUser.first_name }} in this case?
To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.

Andréas Kühne

unread,
Jun 30, 2020, 7:22:11 AM6/30/20
to django...@googlegroups.com
All you need to do is update the render call a dictionary with the customuser variable in it:

return render(request, 'members/member_contact_form.html', {'ContactFormset':ContactFormset, 'Address Formset':AddressFormset, 'customuser': customuser })

Regards,

Andréas


To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/2ab18788-3a31-4c7f-bba7-890580540153o%40googlegroups.com.

The Sha

unread,
Jun 30, 2020, 4:03:47 PM6/30/20
to Django users
It worked thank you my friend!


Den tisdag 30 juni 2020 kl. 11:00:53 UTC+2 skrev Andréas Kühne:
To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.

The Sha

unread,
Jun 30, 2020, 4:04:11 PM6/30/20
to Django users
Thank you friend, it worked
Reply all
Reply to author
Forward
0 new messages