almost a beginner almost a beginner - 3 months ago 7
Python Question

passing parameters through url to view django

I'm having some difficulty passing values to my view through url.

So far, I've managed to run my view without any issues.

view.py (draws a graph):

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


but I need my view to take input from users, so I edited it and added an extra parameter:

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


The user input is passed from a form, to another view:

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


In my template, I insert the user input in the draw's parameter:

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


This is suppose to draw an image base on the user input, instead I get no image at all.

If I remove the parameter, the image works fine. I'm assuming I'm doing something wrong with the setup of the parameter, most likely in the template, or url:

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


I have tested the view, form and everything else, they all work. How can I narrow down this problem to find the solution?

Any direction/help would be appreciated,

thanks,

<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 }}">


screen shot:

The picture doesn't show

Answer

Fo url like

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

Change your view to:

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

Then change in template:

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