Search code examples
djangopython-3.xdjango-formtools

Django form prefix separator hyphen to underscore


The naming process for django-formtools seems to be breaking the ability to access variables in session data.

The form I have named patient_details but it's fields are being separated by - for example patient_details-upper_arch. I'm trying to access the session data to manipulate a form further along in the wizard based on a selection in the first form. However when trying to access {{ ...patient_details-upper_arch }} it throws a TemplateSyntaxError Could not parse the remainder: '-upper_arch' from ...patient_details-upper_arch

Is there a way to set the separator to an underscore to make this variable accessible in the template, or another way around this?


Solution

  • I got around this with a simple_tag custom tag.

    from django import template
    
    register = template.Library()
    
    @register.simple_tag
    def get_session_formwizard_variable(session, key):
        return session.get(key, '')