简体   繁体   中英

Python Django dropdown form and trigger python script

Newbie with Django and Python. Creating a Nav bar with dropdown list (which being populate via form.py) Once the user select the item, Another dropdown list will display. After user selected the item from the list and hit submit, Like to trigger python script to fetch data and populate in table format Stuck executing python script

Following code: views.py:

class StatusLayoutPageView(FormView):
    template_name = "status_layout.html"
    form_class = SelectLocationForm

    def form_valid(self, form):
        # This method is called when valid form data has been POSTed.
        # It should return an HttpResponse.
        return super().form_valid(form)

class DataView(FormView):
    ## This will contain the output 
    template_name = "data.html"
    form_class = SelectLocationForm

Here is the models.py

LOCATIONS = (
    ('DC1', 'DC1'),
    ('DC2', 'DC2'),
)
class SelectLocationForm(forms.Form):
    error_css_class = 'error'
    location = forms.ChoiceField(choices=LOCATIONS, required=True)
    class Meta:
        model = SelectLocation

Here is the template:

<form method="post">{% csrf_token %}
    <select name="location">
      <option selected>Select Location</option>
      {% for val in form.location %}
      <option value="{{ val }}"></option>
      {% endfor %}
    </select>
  <p>
  <button align="center" class="button execute" name="submit" value="submit">GO TO</button>
</form>

Issue running into is how to tell which value user selected base on that load the page. Also with the button onclick, like to pass the data to python script to run thru the data and output back in table format.

expected output: FrontPage: NAVBAR: HOME | LOG | SELECT APP ['FIND','DELETE'] USER Selected FIND PAGELOADED: w/ NAVBAR HOME | LOG | FIND ['FIND','DELETE'] ANOTHER DROPDOWN: SELECT LOCATIONS ['DC1', 'DC2', 'DC3'] BUTTON: SUBMIT Once the user click button, it will run a python script.

A few things to clear up. Your Python only runs on the server. Therefore your form that Python generated is not actual Python anymore.

You could use a normal form submit to post your data to the server and there you'll need to look into obtaining the values from your form POST on the server and take some sort of action.

If everything is wrapped in a Django form and the page has a submit, this will post upon hitting the enter button or just using JavaScript to POST the page data. Keep in mind this will refresh your entire app, so in your case, you need to look to making Async calls to your server using just JavaScript. You than get the response to those calls (via JavaScript) and use JavaScript to update the HTML without requiring a page refresh. Otherwise, you'll need to look into session management to avoid blowing away all your users changes every time they refresh or submit the page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM