On Sat, Feb 20, 2016 at 10:32 PM, Xristos Xristoou <saxr...@gmail.com>
wrote:

> i have a progress with help anyway i am newbie
>
> i convert my python script(remember the first script on the first page) to
> django script on the view
>
> views.py:
>
> from django.shortcuts import render_to_response
> def calc(request):
>         a = []
>         NList = []
>         y=0
>         nums = request.POST['num']
>         total_inputs = request.POST['total']
>         for i in range(total_inputs):
>             NList.append(int(num) for num in nums.split(','))
>             for k in NList:
>                 if k is 1:
>                     y = 1
>                 elif (k > 1) and (k < 5):
>                     y = (k - 1) + 2
>                 a.append(y)
>         return render_to_response('blog/calc.html', {
>         'a' :a,
>         })
>
>
> app/urls:
>
> urlpatterns = [
> url(r'^$',views.calc, name='calc'),
> ]
>
>
> html form:
>
> <form action="" method="POST">
>   Number of number: <input type="text" name="num" value="Number">
>   Enter a value: <input type="text" name="total" value="Total value">
>   <button type="submit" value="Submit">
> </form>
>
>
> and i take that error
>
>
> MultiValueDictKeyError at /
>
> "'num'"
>
>
>    1.
>
>     nums = request.POST['num']
>
>
>
You're likely getting this error because request.POST['num'] is empty
(wasn't submitted via the form). With the way that your view is written,
this error will occur if you are using that view to display the initial
blank form. As of now, you can only use that view as the action= target for
the form.

request.POST is a Django QueryDict with keys for each field in the form.

https://docs.djangoproject.com/en/1.9/ref/request-response/#django.http.HttpRequest.POST

When accessing fields in request.POST, usually you use something like
request.POST.get('num') so that an error isn't thrown, and instead returns
None. If a form field can contain multiple values, you would need to call
.get() multiple times in a loop.

https://docs.djangoproject.com/en/1.9/ref/request-response/#django.http.QueryDict.get

Otherwise it seems like you are getting closer.

-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+unsubscr...@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%2BciUhztnKh5o7wpUNbjov9pm7KNz2473QM_LQ8A05W%2B%3D%3D2Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to