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 ?
The django views need to return an
HttpResponseobject. You are doing that whilenum < 5, but then you return an int whennum == 5:If all you want for when
num==5is to return a plain text response of the number 1, then you can return an HttpResponse and set the content type: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
numvalue 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.
check()is your single view.adder()is the helper function that will add the value, check it, and return anHttpResponseobject based on that value. You must always return this from your view to the client.numUpdate 2
You said that you are actually passing in your
numthrough the url and not through the POST form. Small adjustment to the last example. You don’t even need anadder()anymore. You only need a single view.Set your url to have an optional
numpattern:urls.py
views.py
Your “checker” url now has an optional number. If it is not passed in the url, it will be a value of
0in the view. If you send it as a POST request, it will add and return a different template.