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 432449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T20:04:33+00:00 2026-05-12T20:04:33+00:00

At some point I need to display a disabled (greyed out by disabled=disabled attribute)

  • 0

At some point I need to display a "disabled" (greyed out by disabled="disabled" attribute) input of type "select". As specified in the standard (xhtml and html4), inputs of type "select" can not have the "readonly" attribute. Note that this is for presentation purposes only, the actual value must end up in the POST. So here is what I do (quoting a part of the form declaration in django):

from django import forms

_choices = ['to be', 'not to be']
class SomeForm(forms.Form):
    field = forms.ChoiceField(choices=[(item, item) for item in _choices],
                   widget=forms.HiddenInput()) # the real field

    mock_field = forms.ChoiceField(required=False, # doesn't get submitted
                        choices=[(item, item) for item in _choices],
                        label="The question",
                        widget=forms.Select(attrs={'disabled':'disabled'}))

Then it is initialized like this:

initial_val = 'to be'
form = SomeForm(ititial={'field':initial_val,
                         'mock_field':initial_val})

And all is well. Well, until the form gets validated and one of the other fields fails the validation. When this happens, the form is reloaded and the values are preserved, but not the one of the “mock_field” – it never got submitted (it is disabled). So it is not preserved. While this doesn’t affect the data integrity, it is still not so good presentation-wise.

Is there any way to preserve that field, with as little hackery as possible? The form is a part of a django.contrib.formtools.FormWizard and the initial values (and some fields) are generated dynamically. Basically, there is a lot of stuff going on already, it’d be great if it was possible not to overcomplicate things.

  • 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-12T20:04:33+00:00Added an answer on May 12, 2026 at 8:04 pm

    Well, this will be the first time I answer my question, but I’ve found a solution and (while it cerainly is a hack) it works.

    Instead of getting the initial value from the form instance, – self.fields['whatever'].initial seems to be None inside the constructor, I am getting the value from keyword argument “initial”. And then I set it as the only choice for the “mock” field. Like this:

    from django import forms
    
    _choices = ['to be', 'not to be']
    class SomeForm(forms.Form):
        field = forms.ChoiceField(choices=[(item, item) for item in _choices],
                       widget=forms.HiddenInput()) # the real field
    
        mock_field = forms.ChoiceField(required=False, # doesn't get submitted
                            choices=[(item, item) for item in _choices],
                            label="The question",
                            widget=forms.Select(attrs={'disabled':'disabled'}))
    
        def __init__(self, *args, **kwargs):
            super(SomeForm, self).__init__(*args, **kwargs)
            mock_initial = kwargs['initial']['field']
            self.fields['mock_field'].choices = [(mock_initial, mock_initial),]
    

    This probably needs some error handling. Obviously, this will not work if the initial value is not provided for the actual field.

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

Sidebar

Related Questions

I am working on a webpage and at some point I need to disable
I need to display some HTML content as it will be printed. Basically, I
i have a input tag which is non editable, but some times i need
I have a need to convert Pixels to Points in C#. I've seen some
At some point in time I turned on a setting in Visual Studio 2005
Ok, so at some point in time, somebody checked in some files with names
I am designing a system which will at some point require to send email
I have recently reinstalled MacOSX, and at some point (without realizing it) I made
I have a lot of constants that are somehow related, at some point I
Can anyone point to some code that deals with the security of files access

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.