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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:16:41+00:00 2026-06-01T19:16:41+00:00

I have a form for which I would like to toggle a field’s required

  • 0

I have a form for which I would like to toggle a field’s required attribute on and off. I can successfully use the required attribute of the Form’s CharField but only on a page refresh or when I POST the form. When I try to do a GET callback the ‘sometimes’ field will have required toggled on and off but the html class “required” will not be refreshed.

The html that doesn’t get refreshed, showing the “required” class. This is auto-generated by Django:

<dt class="required">
    <label for="sometimes">Username</label>
</dt>

Simplified views.py:

form_validate(httpreq):
  form = SetupForm()
  if httpreq.GET.has_key('req'):
    # In this case, it is not the initial call
    if httpreq.GET['req'] == 'true':
      print 'required is True'
      form.fields['sometimes'].required = True
    else:
       print 'required is False'
       form.fields['sometimes'].required = True
  else:
    # Here, required = True/False behaves as expected
    # When the field is required, it has the class="required"
    form.fields['sometimes'].required = True
    # For forms.fields['sometimes'].required = False,
    # class="required" is absent in the html


class SetupForm(forms.Form):
  sometimes = forms.CharField(widget=forms.TextInput(attrs={'id':'sometimes','size':FIELD_LENGTHS['shorttext'],}), label=ugettext_lazy("Username"),)

My jQuery:

  $(document).ready(function(){
    $('#tog').click(function(){
        var params = {}
        if ($("#tog").is(":checked")) {
            params = {"auth":true};
        } else {
            params = {"auth":false};
        }
        var url = "/url_for_form/?fmt=json";
        $.getJSON(url, params, function(jsonrpc, xhrstatus, xhr) {
              console.log("JSON callback");
             });
    });
  });

So, how can I make the html refresh the class name?

  • 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-06-01T19:16:43+00:00Added an answer on June 1, 2026 at 7:16 pm

    I’d suspect the problem lies somewhere between the browser and the server. When you POST or explicitly reload a page, the browser will load the full page from the server, ignoring its cache. However, when you do a regular GET request, the browser may show the last cached version of the page.

    The solution is to add the following header to the HTTP response to this specific view:

    Cache-Control: no-cache
    

    One way to go about this might be to decorate your view with a decorator similar to this one: http://djangosnippets.org/snippets/275/

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

Sidebar

Related Questions

I have a form which includes a data sheet. I would like to make
I have a nested form in which I would like the form fields to
I have come across a dynamically written form which I would like to style
I have a block of form elements which I would like to clone and
We have a table form which we would like to select data in both
I have a custom form to which I would like to pass a parameter.
I have a form which I would like to store the values of in
In a customised form I would have a create Purchase Menubutton which opens a
I have a number of table rows that I would like to toggle the
I have a form which has a field which takes in a hex value

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.