3

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

6
  • 1
    path to image is not correct - look into loading static Commented Aug 22, 2016 at 12:02
  • @NabeelAhmed Hi Nabeel, why am I able to load <img src="test.com/graph"> but not <img src="test.com/graph{{ loan_amount }}"> ?? Meanwhile I will read about loading static. Commented Aug 22, 2016 at 12:11
  • What is the imgurl you see in your browser Commented Aug 22, 2016 at 12:35
  • @Aison test.com/graph/10000 (if I input 10000) Commented Aug 22, 2016 at 12:39
  • Have you set your STATIC_URL in settings? Commented Aug 22, 2016 at 12:41

1 Answer 1

3

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 %}">
Sign up to request clarification or add additional context in comments.

4 Comments

If you use {% url 'graph' loanAmount %} what error will you have? And change def draw(request, loan_amount) to def draw(request, *args, **kwargs)
In your latest update, I get 2016-08-22 12:29:43,515 :Not Found: /static/path/to/user image/10000/img.png. I have made the necessary changes. Can you please change your code to before your update.
I get the following error, just read the error log: ymin = (-desiredLoan - (repaymentTime * 25)) TypeError: bad operand type for unary -: 'str'
loanAmount = int(kwargs['desired_loan'])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.