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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:08:01+00:00 2026-05-28T08:08:01+00:00

I have a finite state machine (django-fsm) which allows an object to go from

  • 0

I have a finite state machine (django-fsm) which allows an object to go from a source state into one of several target states. I can add all the actions in a dictionary like:

ACTIONS { 'button_1': action1,
          'button_2': action2,
           ...
}

This translates in a form with a submit button for each state.

  {% for n,m in object.get_available_current_state_transitions %}
      <input type="submit" class="btn" value="{{ n|get_action|capfirst }}"
             name="button_{{n}}" />
  {%endfor%}

  <input type="submit" class="btn primary" value="Save">
  <a class="btn" onclick="javascript:history.go(-1)">Cancel</a>

This usually results in more than 3 buttons.

Clicking a button results in a specific action, defined in my case in the model class.

Now, I know I can get the clicked button in the request.POST dictionary, but this would result in a cascade if like:

  if 'button_1' in request.POST:
      action_1()
  elif 'button_2' in request.POST:
      ...

Is there any way to get the button pressed separately (ideally from the request object) in a variable so I can have something like

ACTIONS[clicked_button_name](...)

? In other words, is there any way to obtain the clicked button outside the POST dictionary?

PS: I’ve looked other replies on the “multiple buttons” question, but all offer request.POST as answer.

  • 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-28T08:08:02+00:00Added an answer on May 28, 2026 at 8:08 am

    If all of the actions and strings are already in your view, why don’t you just iterate over that actions dict?

    for key, value in ACTIONS.items():
        if key in request.POST:
            value()
    

    Just make your button names very unlikely to be used as a regular form field name.


    A few alternatives: use javascript to handle the submission and have it set a single form field such as “action”.

    Use more unique keys and filter through request.POST.keys() with a regex pattern or string comparison.

    action = [x for x in request.POST.keys() if 'FAIRLY_UNIQUE_BUTTON_PREFIX' in x]
    
    if action:
        ACTIONS[action]()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to model a kind of FSM(Finite State Machine). I have a sequence
I have a GEF editor which represent a finite state machine. Editor's input (and
I have a class representing a finite-state machine, which should run in a forever
Does anyone have a solution for a basic, compact Finite state machine/automata written in
I have a third-party library (the interface to Xerox's Finite State tools) which come
I have to draw a small finite state machine that has some reflexive transitions
How do you go about implementing FSM(EDIT:Finite State Machine) states? I usually think about
I have a moving tank: http://www.exeneva.com/html5/movingTankExample/ The tank uses a finite state machine for
I have i finite-state machine. My regular expression is: \+[0-9]+\+%\+[0-9]+ The problem is that
Basically I have a custom implemented finite state machine that mostly listens for hardware

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.