简体   繁体   English

Python Django下拉列表和触发器python脚本

[英]Python Django dropdown form and trigger python script

Newbie with Django and Python. Django和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. 创建一个带有下拉列表的导航栏(通过form.py填充)用户选择该项目后,将显示另一个下拉列表。 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 用户从列表中选择项目并单击提交后,喜欢触发python脚本以获取数据并以表格式填充粘滞执行python脚本

Following code: views.py: 以下代码: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 这是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. 也可以使用onclick按钮,将数据传递到python脚本以通过数据运行并以表格式输出回去。

expected output: FrontPage: NAVBAR: HOME | 预期输出:FrontPage:NAVBAR:HOME | LOG | 日志| SELECT APP ['FIND','DELETE'] USER Selected FIND PAGELOADED: w/ NAVBAR HOME | SELECT APP ['FIND','DELETE']用户选择的FIND页面已加载: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. 查找['查找','删除']另一个下拉列表:选择位置['DC1','DC2','DC3']按钮: SUBMIT用户单击按钮后,它将运行python脚本。

A few things to clear up. 需要清除的几件事。 Your Python only runs on the server. 您的Python仅在服务器上运行。 Therefore your form that Python generated is not actual Python anymore. 因此,Python生成的表单不再是实际的Python。

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. 您可以使用普通的表单提交将数据发布到服务器,然后需要在服务器上通过表单POST获取值并采取某种措施。

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. 如果所有内容都以Django形式包装,并且页面具有提交,则将在按下Enter键或仅使用JavaScript POST页面数据时发布。 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. 请记住,这将刷新整个应用程序,因此,在这种情况下,您需要仅使用JavaScript对服务器进行异步调用。 You than get the response to those calls (via JavaScript) and use JavaScript to update the HTML without requiring a page refresh. 然后,您将获得对这些调用的响应(通过JavaScript),并使用JavaScript更新HTML,而无需刷新页面。 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. 否则,您需要研究会话管理,以避免在每次刷新或提交页面时浪费所有用户更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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