Django如何根据从模板选择下拉列表中选择的选项更改视图

2024-05-28 19:39:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图显示一个图形,该图形响应用户从选择表单中选择的选项。然而,我发现,当我选择“BTC”之外的其他选项时,图形并没有做出响应

相关main.HTML:

     <div class = "container">
        <form method = "POST" action = "">
          <select name = "coin-select">
            {% for coin in coin_lst %}
            <option value="{{ coin }}">{{ coin }}</option>
            {% endfor %}
          </select>
        </form>
     </div>
     <div class = "container">
        {% autoescape off %}
        {{ graph }}
        {% endautoescape %}
      </div>

relevent view.py:

def initial_data(request):
    coin_lst = []
    for x in focused_df.coin:
        if x not in coin_lst:
            coin_lst.append(x)

    def close_vs_redditComments():
        if request.method == 'POST':
            coin =  str(request.POST.get("coin-select"))
        else: 
            coin = 'BTC'
        closeVSredditComment = make_subplots(specs=[[{"secondary_y": True}]])
        closeVSredditComment.add_trace(go.Scatter(x = df[df.coin == coin]['time'], y = df[df.coin == coin]['close'], name = "closing price"), secondary_y = False)
        closeVSredditComment.add_trace(go.Scatter(x = df[df.coin == coin]['time'], y = df[df.coin == coin]['reddit_comments'], name = "reddit comment volume"), secondary_y = True)
        return(plot(closeVSredditComment, output_type = 'div'))

    context = {
        'coin_lst': coin_lst,
        'graph': close_vs_redditComments()
    }
    return render(request, 'main.html', context)

我错过了什么


Tags: nameindiv图形dfcloserequest选项

热门问题