Multiple submit for same form refresh the page

104 views
Skip to first unread message

Nirantar Kulkarni

unread,
May 9, 2020, 9:56:54 AM5/9/20
to Django users
html template
<form method="post" enctype="multipart/form-data" id="sub_form" name="sub_form">
        {% csrf_token %}
          <textarea name="name">{{name}}</textarea>
          <button type="submit" class = "b1" name = "wordcloud">WordCloud</button>
          <button type="submit" class = "b2" name = "bar">Text_Analysis</button>
          <button type="submit" class = "b3" name = "sa">Sentiment Analysis</button>
 </form>

I have multiple submit buttons for same form and each button refreshes the page 
I don't want it to refresh as it removes all changes made by one button to perform changes made by another please help me

Kasper Laudrup

unread,
May 9, 2020, 10:21:53 AM5/9/20
to django...@googlegroups.com
Hi Nirantar,

On 09/05/2020 15.15, Nirantar Kulkarni wrote:
>
> I have multiple submit buttons for same form and each button refreshes
> the page
> I don't want it to refresh as it removes all changes made by one button
> to perform changes made by another please help me
>

As far as I know, you can only have one action with a plain HTML form.
It might be possible to have more than one submit button, but I don't
think that's part of the standard and might be handled differently
depending on the browser.

If you don't mind the refreshing as long as the data is still there, you
can prefill the form with the contents submitted. That is all easily
handled with standard Django.

You probably need some javascript magic to handle multiple submits though.

I would try something like hiding the actual submit button from the form
and then adding different buttons outside of the form and write some
javascript handler that would submit the form with an extra field
depending on which button was pressed. You could also make the same
javascript action send the data directly to avoid refreshing of the page.

In short, I'm fairly certain you cannot achieve what you want without
writing some kind of client-side javascript :-(

Kind regards,

Kasper Laudrup

Jim Illback

unread,
May 9, 2020, 7:02:01 PM5/9/20
to Django users
Nirantar, I think you could do this in your class (or even in your function if you use that instead):
  - Override (or use) the get() method
  - Add a conditional such as this in that get() method:
if request.GET.get(wordcloud):
<do wordcloud things>
elif request.GET.get(‘bar’):
<do bar things>

Good luck!
Jim Illback


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/14a8c9f5-c949-4caa-a3dd-87af0d4d39c4%40googlegroups.com.

Motaz Hejaze

unread,
May 9, 2020, 7:15:22 PM5/9/20
to Django users
try this :

<form onsubmit="return false">
</form>

Reply all
Reply to author
Forward
0 new messages