【问题标题】:Views function not getting triggered视图功能没有被触发
【发布时间】:2021-09-14 06:48:56
【问题描述】:

目前,我没有看到任何错误 - 但我的图像没有被创建。这意味着没有触发视图函数,因为图像生成函数在视图函数之外完美地工作。

Views.py

def views_function(request):
print('checkpoint') #This doesn't seem to be getting triggered (can't see the print statement output anywhere)
    def make_image(Na, Fe, Gb, Ty, Fs, Fl):
        #Lots of chart plotting code here
        try:
            fig1 = plt.gcf()
            plt.draw()
            print('saving file')
            fig1.savefig(f'../media/fig{User.profile.chart.url}.png', dpi=100)
        except:
            print('save failed')
        
        return render(request, 'AppName/plot.html', {'title': 'plot'})

    if request.method == 'POST':
        print('checkpoint')
        return make_image(100, 1, 16, 1, 1000, 600) and render(request, 'AppName/plot.html', {'title': 'plot'})

HTML 按钮:

<button type="submit" action="{% url 'AppName:views_function' %}" class="btn btn-light" style="width: 515px;">Run</button>

网址格式:

path('home/plot/', views.views_function, name='views_function'),

谁能看出问题所在?任何想法都值得赞赏,我现在无处可去。

【问题讨论】:

  • 如果您使用runserver cmd 在本地运行服务器,您会在控制台中看到它。单击按钮时,您实际上在控制台中看到了什么?
  • 发布请求@ToniSredanović
  • 你能展示一下吗?它返回 404 还是什么?
  • post 请求发送没有任何问题,输出为 '[02/Jul/2021 12:17:56] "POST /home/plot/ HTTP/1.1" 200 6564'。这是我在控制台中得到的唯一输出,页面基本上只是刷新。

标签: django django-models django-rest-framework django-views django-forms


【解决方案1】:

视图函数views_function 第一行的打印看起来没有正确缩进。尝试给一个 Tab。

【讨论】:

  • 那是我的错,原来的代码不是这样的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-04
  • 2022-10-04
  • 1970-01-01
  • 2012-12-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多