通过url传递参数来查看django

时间:2016-08-22 11:35:37

标签: python django

我通过网址将值传递给我的观点时遇到了一些困难。

到目前为止,我已经成功运行了我的观点而没有任何问题。

view.py(绘制图表):

def draw(request)
    ....
    ....
    return HttpResponse (buffer.getvalue(), content_type="Image/png")

但我需要我的观点来接受用户的输入,所以我编辑了它并添加了一个额外的参数:

def draw(request, loan_amount)
     loanAmount = loan_amount
     .....

用户输入从表单传递到另一个视图:

def search_member(request):
     loanAmount = request.GET.get('desired_loan')
     return render(request, 'blog/search_member.html', {'loanAmount':loanAmount)

在我的模板中,我在绘图参数中插入用户输入:

<img src="http://test.com/graph/{{ loanAmount }}">

这是假设根据用户输入绘制图像,而我根本没有图像。

如果我删除参数,图像工作正常。我假设我在参数设置方面做错了,很可能是在模板或网址中:

url(r'^graph/(?P<desired_loan>\d+)/$', views.draw, name='draw'),

我测试了视图,表单和其他所有内容,它们都有效。如何缩小此问题以找到解决方案?

任何方向/帮助将不胜感激,

感谢,

<form method="GET" action="/search_member/" class="navbar-form pull-left">

            <input type="number" step = "any" id="searchBox" class="input-medium search-query" name="desired_loan" placeholder="Desired Loan"><br>
            <input type="number" step = "any" id="searchBox" class="input-medium search-query" name="repayment_time" placeholder="Payment Time"><br>

            <input type="submit" class="btn" value="Draw Graph" >

        </form><br>
        <img src="http://test.com/graph/{{ loanAmount }}">

屏幕截图:

The picture doesn't show

1 个答案:

答案 0 :(得分:2)

喜欢

url(r'^graph/(?P<desired_loan>\d+)/$', views.draw, name='draw'),

将您的观点更改为:

def draw(request, *args, **kwargs):
 loanAmount = kwargs['desired_loan']
 .....

然后更改模板:

<img src="{% url 'graph' loanAmount %}">