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

  • Home
  • SEARCH
  • 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 3480550
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:21:53+00:00 2026-05-18T10:21:53+00:00

I am trying to use a textarea form entry which I have some javascript

  • 0

I am trying to use a textarea form entry which I have some javascript code to count and limit the character count, whose max_length and size are dynamic (ie these attributes are alterable after model creation).

In order to include this functionality in the django form textarea widget I suppose I would have to create a custom widget, although im not sure how to tie in the javascript code in the template really. For changing the textarea size, i suppose I would pass a variable into the form creation from the view to generate the textarea widget instance.

Otherwise for now I am simply creating the inputs in the template in html/js and then submitting the POST data to the django view, and retrieving it using getitem, then saving the data directly to my models. Which works of course, but is not very django’y, and perhaps is somehow vulnerable without the built-in django validation and clean data functions.

So two questions I guess: (a) If I create a custom django form widget, how do I apply my javascript function to the textarea fields in the template (includes “onkeypress=jstextcounter…”) and (b) If I continue just retrieving the post data in the view without a django form, is there a vulnerability and what should I do to ensure the data is well-validated? (page already requires user login, not a public comment box)

Or maybe someone has created such a custom form widget before and knows a good snippet somewhere?

Cheers…

EDIT
So thanks to MovieYoda’s help, I got this working and some reading over dynamic forms in django. And with a %d substitute I can dynamically change the maxChars attribute for my js function.

In forms.py I have:

text=forms.CharField(max_length = 1000, widget=forms.widgets.Textarea())

def __init__(self, *args, **kwargs):
     size = kwargs.pop('size')
     maxChars = kwargs.pop('maxChars') 
     super(MyForm, self).__init__(*args, **kwargs)
     self.fields['text'].widget.attrs['onkeypress'] = 'return textCounter(this, this.form.counter, %d);' % maxChars
     self.fields['text'].widget.attrs['rows'] = size
     self.fields['text'].widget.attrs['cols'] = '40'

Where the js function ‘textCounter’ in the template is limiting the number of characters, using the maxChars variable.

<script type="text/javascript">

function textCounter( field, countfield, maxLimit) {
  if ( field.value.length > maxLimit )
  {
    field.value = field.value.substring( 0, maxLimit );
    return false;
  }
  else
  { 
    countfield.value = maxLimit - field.value.length;
  }
}
</script>

And in the view create the form with the kwargs inputs:

FormInstance = MyForm(size=1, maxChars=5)
  • 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-18T10:21:53+00:00Added an answer on May 18, 2026 at 10:21 am

    (a) If I create a custom django form widget, how do I apply my javascript
    function to the textarea fields in the
    template (includes
    “onkeypress=jstextcounter…”) and

    From what I was able to understand you want to define javascript handlers on your form elements? Here’s how one can approach this –

    In forms.py

    class Search(forms.Form):
     se=forms.CharField(widget=forms.TextInput(attrs={'onClick':'return jscode();'}))
    

    Now render your form as usual. In your js file, write this handler function jscode() & make it do what you want.

    (b) If I continue just retrieving the post data in the view without a
    django form, is there a vulnerability
    and what should I do to ensure the
    data is well-validated?

    The second part can also be easily handled. In your forms.py file define these member functions for which you want to do some data-validation. You can do it like this –

    class Search(forms.Form):
     se=forms.CharField(widget=forms.TextInput(attrs={'onClick':'return jscode();'}))
     def clean_se(self):
        #do your validaton here. In case of error raise ValidationError.
        raise forms.ValidationError('Passwords do not match.')
    

    Note that the clean fn. should be in this format – clean_<field_to_be_cleaned>. Here it is se. so…

    Apart from Making sure @login_required – which makes sure your view is accessed by a logged in user, you need to use csrf_token in your form templates & urlencode your data (if user submits some data through the form). Now you are good to go…

    • 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.