2

I start with def start method, it calls go_adder to add the num value 5 times in the adder.html until num equals 5. After that, the adder method should return ready=1

In views.py

def start(request):
    num=0
    ready_or_not=go_adder(num)
    return HttpResponse("Ready: %s "%str(ready_or_not))

def go_adder(num):
    ready=0
    if num<5:
        return render_to_response('adder.html',{'num':num})
    elif num==5:
        ready=1
        return ready

def check_post(request,num):
    if request.method == 'POST':
        num+=1
        return adder(num)

When I try to run this snippet code, it works until my "num=5", Then I get that error :

'int' object has no attribute 'status_code' 
Exception Location: C:\Python27\lib\site-packages\django\middleware\common.py in process_response, line 94

and Traceback says:

C:\Python27\lib\site-packages\django\core\handlers\base.py in get_response
                response = middleware_method(request, response) ...
▶ Local vars
C:\Python27\lib\site-packages\django\middleware\common.py in process_response
        if response.status_code == 404: ...
▶ Local vars 

How can I fix that error ? Could you please help me ?

1 Answer 1

8

The django views need to return an HttpResponse object. You are doing that while num < 5, but then you return an int when num == 5:

def adder(num):
    ready=0
    if num<5:
        num+=1
        # This renders the template and returns and HttpResponse; good
        return render_to_response('adder.html',{'num':num})
    elif num==5:
        ready=1
        # DONT RETURN AN INT HERE. RETURN AN HttpResponse
        return ready

If all you want for when num==5 is to return a plain text response of the number 1, then you can return an HttpResponse and set the content type:

    elif num==5:
        ready=1
        return HttpResponse(str(ready), content_type="text/plain")

Update 1

Based on our conversation, you have suggested that you want the view to constantly pass along the count value no matter what, and that you are POST-ing the num value in the actual form. If the number is less than 5 it should return one kind of template, otherwise it should return another kind of template.

You can combine your two different views into one that will handle both the original GET request when the page is first loaded, and the POST request submitted by your form. Just make sure to point your form at that same page.

def check(request):
    num = 0

    if request.method == 'POST':
        num = int(request.POST.get('num'))

    return adder(num)

def adder(num):
    if num < 5:
        num += 1
        tpl_name = 'adder.html'
    else:
        tpl_name = 'print.html'

    return render_to_response(tpl_name, {'num':num})
  • check() is your single view.
  • adder() is the helper function that will add the value, check it, and return an HttpResponse object based on that value. You must always return this from your view to the client.
  • Both templates will be passed the context containing the value of num

Update 2

You said that you are actually passing in your num through the url and not through the POST form. Small adjustment to the last example. You don't even need an adder() anymore. You only need a single view.

Set your url to have an optional num pattern:

urls.py

(r'^checker/(?P<num>\d+)$', 'myapp.views.check')

views.py

def check(request, num=0):
    num = int(num)
    if num < 5:
        num += 1
        tpl_name = 'adder.html'
    else:
        tpl_name = 'print.html'

    return render_to_response(tpl_name, {'num':num})

Your "checker" url now has an optional number. If it is not passed in the url, it will be a value of 0 in the view. If you send it as a POST request, it will add and return a different template.

Sign up to request clarification or add additional context in comments.

13 Comments

Your examples are not clear. Why is go_adder returning different types of objects? Which of those 2 views are you calling?
Cause, I want to repeat that process until I reach the value of 5. Before that, I call adder.html, once it is called 5 times, return a ready bit that is 1. Is there another way to do that ?
Your logic here should work, but your return value does not. What should happen if num reaches 5? A different template? And int is not an appropriate return value. Do you simply want it to return a plain/text response with the number 1?
Ok, I see your point. How can I get that value from my start method? , because I need to process that ready value over there.
It seems that your page never returns to the start view beyond the first GET, because you are sending it to another view.
|

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.