Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 730385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:54:20+00:00 2026-05-14T06:54:20+00:00

I’m trying to build a better/more powerful form class for Django. It’s working well,

  • 0

I’m trying to build a better/more powerful form class for Django. It’s working well, except for these sub-forms. Actually, it works perfectly right after I re-start apache, but after I refresh the page a few times, my HTML output starts to look like this:

<input class="text" type="text" id="pickup_addr-pickup_addr-pickup_addr-id-pickup_addr-venue" value="" name="pickup_addr-pickup_addr-pickup_addr-pickup_addr-venue" />

The pickup_addr- part starts repeating many times. I was looking for loops around the prefix code that might have cause this to happen, but the output isn’t even consistent when I refresh the page, so I think something is getting cached somewhere, but I can’t even imagine how that’s possible. The prefix var should be reset when the class is initialized, no? Unless it’s somehow not initializing something?

class Form(object):
    count = 0
    def __init__(self, data={}, prefix='', action='', id=None, multiple=False):
        self.fields = {}
        self.subforms = {}
        self.data = {}
        self.action = action
        self.id = fnn(id, 'form%d' % Form.count)
        self.errors = []
        self.valid = True
        if not empty(prefix) and prefix[-1:] not in ('-','_'): prefix += '-'

        for name, field in inspect.getmembers(self, lambda m: isinstance(m, Field)):
            if name[:2] == '__': continue
            field_name = fnn(field.name, name)
            field.label = fnn(field.label, humanize(field_name))
            field.name = field.widget.name = prefix + field_name + ife(multiple, '[]')
            field.id = field.auto_id = field.widget.id = ife(field.id==None, 'id-') + prefix + fnn(field.id, field_name) + ife(multiple, Form.count)
            field.errors = []

            val = fnn(field.widget.get_value(data), field.default)

            if isinstance(val, basestring):
                try:
                    val = field.coerce(field.format(val))
                except Exception, err:
                    self.valid = False
                    field.errors.append(escape_html(err))

            field.val = self.data[name] = field.widget.val = val

            for rule in field.rules:
                rule.fields = self.fields
                rule.val = field.val
                rule.name = field.name

            self.fields[name] = field

        for name, form in inspect.getmembers(self, lambda m: ispropersubclass(m, Form)):
            if name[:2] == '__': continue
            self.subforms[name] = self.__dict__[name] = form(data=data, prefix='%s%s-' % (prefix, name))

        Form.count += 1  

Let me know if you need more code… I know it’s a lot, but I just can’t figure out what’s causing this! I’m not even using any cache middleware.


Copying/cloning the fields first gives me this output instead:

<label for="None">None</label>
<input class="text" type="text" id="id-pickup_address-venue" value="" name="pickup_address-venue" />

field.name and field.label are set in exactly the same way… in fact, field.id is correctly displayed on the <input> but that same value is suddenly gone when I try to print the label…. the difference is that the <input> bit is printed by the Widget class, whereas the label is printed directly from my template… which I guess is…. oh I get it, that one is still referring to the unset class-level/static field, rather than the instance field…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T06:54:21+00:00Added an answer on May 14, 2026 at 6:54 am

    You’re probably declaring your forms like this:

    class SomeForm(Form):
        someField = Field(....)
        ...
    

    Now, this means that one instance of someField will actually be shared among all your SomeForm instances. In your __init__ you’re changing the attributes of the field, which will affect all forms, not just the current one, including ones created in the future.

    To fix this, you can make a copy of the field for each instance:

    field = copy(field)  #maybe you need deepcopy instead
    setattr(self, name, field)   
    

    And then change the attributes of the copy.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.