Thursday, March 3, 2016

Re: mathematical function and django connect



On Thu, Mar 3, 2016 at 10:48 AM, Xristos Xristoou <saxri89@gmail.com> wrote:
update my code khow i feel so close but not work again hahaha to many problems

. I want to write a django code which takes a number and a list of numbers from the website as user input and and then export a list with results. but if i run my app don't show me results from the progress in html template, i can only input details if i push second button just reload the page and not show me results.

I have written following code but it doesn't show me the results:

views.py

def calc(request):          NList=[]          num=0          a=[]          y=0          c=0          total=0          num = request.POST.get ('num', False)          if request.method=='POST' and 'btnform1' in request.POST:              b='1'.zfill(int(num))              e=b[::-1]              c=int(e)              #num=int(num)          if request.method=='POST' and 'btnform2' in request.POST:           for i in range(int(num)):               total = request.POST.get ('total', False)               NList.append(int(total))           for k in NList:                  if k == 1:                      y=1                  elif (k > 1) and (k < 5):                      y = k+1000                  a.append(y)            return render_to_response ('blog/calc.html', {'a' :a,'num':num,'c':c,'y':y,'total':total,'NList':NList},context_instance=RequestContext(request))

html form

form id="form1" action="" method="POST">{% csrf_token %}  <div>    Number of number: <input type="text" name="num" value="Number"/>      <div>      <button type="submit" name="btnform1">count</button>          </div>  </div>      <p>      {{num}}      </p>  </form>  <form id="form1" action="" method="POST">{% csrf_token %}  <div>      {% for i in c|make_list %}    Enter a value: <input type="text" name="total" value="Total value">      {% endfor %}  <div>      <button type="submit" name="btnform2">final</button>      </div>  </div>  </form>  <p>   {{ a}}  or full info  {% for sm in a %}      {{ sm }}  {% endfor %}  </p>

urls.py

url(r'^$',views.calc, name='calc'),

this is a example if if the first input is num=4 then the script create 4 html inputs field(to user input second number list total) but if i click final button dont show me resust(a=output).


It appears to me that the problem is with the fact that you have two forms on your page, but your view function is expecting values from both forms in order to generate the results you want.

You can either a) only have a single form that is rebuilt per each step with the additional fields you need or b) refactor your view code. 

         for i in range(int(num)):               total = request.POST.get ('total', False)               NList.append(int(total))

Your problem in the view is that your loop relies on int(num), but the 'num' variable will be set to False when you click the 'final' button, because it is not sent as part of that form (ie request.POST.get('num') returns False), and so your loop will never run. In reality, you don't even need the 'num' variable, just query the 'total' fields:

for total in request.POST.getlist('total'):
    NList.append(int(total))

getlist() should return an empty list if 'total' is not available, so it should still work on initial page loads before the user specifies the number of fields they need, etc.

Also keep in mind that you are dealing with POST variables directly, with no validation that you would get from a true Django Form() object, and there is no error checking in your code either, so someone submitted 'four' instead of '4' would cause your code to throw an exception. Using a real Django form would help validate that case and provide error-handling to re-display the form in the users' browser.

-James

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2Be%2BciVQp2%2BfTMrCWWdQiqKMkRviy%2BXQ0DnKW%2BgYbjh6njCktQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment