Home > Software engineering >  how to clear the django variables on html page after refresh
how to clear the django variables on html page after refresh

Time:08-31

index.html

{% csrf_token %}{% if text %}{{text}}{% endif %}

views.py def basic_work(request): global summary text = request.POST.get('text')

if text:
    # word_count logic (text format)

    text_count = len(text.split(' '))

    if text_count > 1000:
        print(1000)
        messages.info(request, 'Please try with word count less than 1000')
        return render(request, 'index.html', {'len': text_count, 'text': text})'''

After refreshing the html page I can still see text inputted previously to clear a text i have manually select all text from text area and clear it. how can i clear it on refresh ?

CodePudding user response:

The way to this is to first access the variable text then assign it to a temporary variable and delete it

if text:
    # word_count logic (text format)

    text_count = len(text.split(' '))

    if text_count > 1000:
        print(1000)
        messages.info(request, 'Please try with word count less than 1000')
        tmp_var = text
        del text # delete text after assign it to temp variable 
        # you can also set text="" if you don't want to delete it
        return render(request, 'index.html', {'len': text_count, 'tmp_var': tmp_var})'''

This can help you

CodePudding user response:

There is no way to do this automatically. You could add a button that says "Clear" and when clicked, it would clear the textarea.

  • Related