简体   繁体   English

如何在Django的python脚本中接收表单输入?

[英]How to receive a form input in a python script in django?

what i nee is: On a html form when user click the submit button the value of that button to be received as a python variable so that i can use it in my script. 我要说的是:在html表单上,当用户单击“提交”按钮时,该按钮的值将作为python变量接收,以便我可以在脚本中使用它。

  • My button value is a path to the csv file "CSV/sample_submission.csv" uploaded by user in my app and stored in a folder in my django app. 我的按钮值是用户在我的应用程序中上载并存储在django应用程序的文件夹中的csv文件“ CSV / sample_submission.csv”的路径。

What i want to do is plot some plotly plots by using the file uploaded by the user. 我想要做的是通过使用用户上传的文件来绘制一些绘图。

I've tried to send the data through the post request and receive it in my view function and render it on html page but what i want is that data to be saved as a global or any type of variable that can be used in any python script in my app. 我试图通过发布请求发送数据,并在我的view函数中接收它,并在html页面上呈现它,但是我想要的是将数据保存为全局变量或可以在任何python中使用的任何类型的变量我的应用中的脚本。

URL.py URL.py

path("event/",views.event,name="event"),

Views.py: Views.py:

def event(request):
    context={}
    file= request.POST.get('file',None)
    context['file']=file
    return render(request,'main/event.html',context)

Following is my code of plot.py 以下是我的plot.py代码

from plotly.offline import plot
import plotly.graph_objs as go
import pandas as pd

def get_simple_candlestick():

    df = pd.read_csv("FILE PATH RECEIVED FROM FORM")
    df.head()

    data = [
        go.Bar(
            x=df['x'], # assign x as the dataframe column 'x'
            y=df['y']
        )
    ]
    layout = go.Layout(
        title='Plot Title',
     xaxis=dict(
        autorange=True
        ),
     yaxis=dict(
        autorange=True
        )
     )
    plot_data = data
    figure = go.Figure(data=plot_data, layout=layout)
    plot_div = plot(figure, output_type='div', include_plotlyjs=False)
    return plot_div

what i expect is that the data to be read by .read_csv() function should be the the one that user uploaded and selected from many of the files he uploaded. 我期望的是,要由.read_csv()函数读取的数据应该是用户上传的数据,并应从他上传的许多文件中选择。

我只是通过使全局变量来实现的。

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

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