Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Formset ID's don't seem to work in a formset. #2

Open
benjohnson opened this issue Aug 8, 2010 · 3 comments
Open

Formset ID's don't seem to work in a formset. #2

benjohnson opened this issue Aug 8, 2010 · 3 comments

Comments

@benjohnson
Copy link

Hi!

I'm calling this: {% field f.ends class="datepicker" %} inside a formset. It outputs the multiple fields, but the ID's are all identical to the first form element. That said, the name= attribute is outputting correctly, so something fishy is going on.

Any ideas?

Absolutely love your plugin. A designer's dream.

Ben.

@skyl
Copy link

skyl commented Oct 8, 2011

Hi, I noticed the same thing. I don't see this same behavior when using the field tag alone though, only when I use
the macros tag from http://djangosnippets.org/snippets/363/

I have found that I can fix the problem in the Field.render method by using .copy() on the attrs. I don't know why the extra_attrs are getting cached with the different Field instances or how that is happening. I think the real fix might be in the macros tag which I'm looking into now.

def render(self, context):
    field = template.resolve_variable(self.field_var, context)
    # Caling bound_field.as_widget() returns the HTML, but we need to 
    # intercept this to manipulate the attributes - so we have to 
    # duplicate the logic from as_widget here.
    widget = field.field.widget
    attrs = self.extra_attrs or {}
    attrs = attrs.copy() # <--------------------------- HERE
    auto_id = field.auto_id
    if auto_id and 'id' not in attrs and 'id' not in widget.attrs:
        attrs['id'] = auto_id
    if not field.form.is_bound:
        data = field.form.initial.get(field.name, field.field.initial)
        if callable(data):
            data = data()
    else:
        data = field.data
    html = widget.render(field.html_name, data, attrs=attrs)
    # Finally, if we're NOT in xhtml mode ensure no '/>'
    doctype = getattr(context, '_doctype', 'xhtml1')
    if doctype in html_doctypes:
        html = xhtml_end_re.sub('>', html)

    return html

@skyl
Copy link

skyl commented Oct 8, 2011

Although, maybe this is a problem with formsets as well, not just the macro tag:

mila@51ccaca

@skyl
Copy link

skyl commented Oct 8, 2011

And loops?

rob-b@fe20d0f

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants