Hai
I think you have to return jsonresponse in views Inorder to do it turn it
data into json using json package and it has a method like dumps or loads
and return data

On Sat 11 May, 2019, 7:58 AM Samaresh Singh, <ssam3...@gmail.com> wrote:

> I can print the prod_name inside the views.py but cannot pass that
> prod_name to the form page of selected product text filed. Main goal is to
> throw the dropdown value received from form page to views.py and send the
> same back to the form page from views.py. Hence I will be able to see the
> changing product name when I select different value from the dropdown.
>
> I am new to Django and would appreciate if you can help me with the
> working code.
>
> Thanks,
> Samaresh
>
>
> On Fri, May 10, 2019 at 6:57 AM Najmath Ummer <najmat...@gmail.com> wrote:
>
>> Hey,
>> Try to print the prod_name in viewsand if it is undefined or null try
>> using post instead of get.
>> Thanks and Regards,
>>
>> Najmath Ummer
>> Perleybrook Labs LLC
>> Mob:8075314478
>>
>>
>> On Fri, 10 May 2019 at 02:04, Samaresh Singh <ssam3...@gmail.com> wrote:
>>
>>> Hi All,
>>>
>>> I am working on internal django project where, i select the option from
>>> dropdown in the form page and pass that value to the views.py using AJAX.
>>> in views.py i use the text from dropdown and want to throw the text into
>>> same form pages text field but that does not work. When i try to pass some
>>> constant string from views.py to template form page that works. Below is
>>> the code snippet
>>>
>>> form.html
>>> ---------------
>>>
>>> <h1>New Hot Fix Request</h1>
>>>   <!--<p>This session is of a &ndash;&gt;  {{request.session.requestor}} 
>>> <&#45;&#45; type user</p>-->
>>>     <label>Product * </label>
>>>     <select class="dropdown" id="dropdown" name="product">
>>>         <option value="" selected="selected"></option>
>>>         <option value="Product1">Product1</option>
>>>         <option value="Product2">Product2</option>
>>>     </select><br>
>>>     <script>
>>>         $(document).ready(function(){
>>>
>>>             $('#dropdown').on('change',function(e){
>>>                 var e = document.getElementById("dropdown");
>>>                 var productName = e.options[e.selectedIndex].value;
>>>                 console.log("Dropdown Product Name: "+productName);
>>>                 $.ajax({
>>>                     url:"",
>>>                     method:'GET',
>>>                     // send selected data to the newHPReq method which is 
>>> in views.py
>>>                     data : {'product' : $(this).val()}, // 'product' will 
>>> be used in request.GET.get('product') which is in views.py, $(this).val() 
>>> is selected item,
>>>                     success:function(data){
>>>                         //console.log(data);
>>>                     }
>>>                 });
>>>             });
>>>         });
>>>     </script>
>>>
>>>
>>> views.py
>>> ---------------
>>>
>>> def newHPReq(request):
>>>     # name = request.session['requestor']
>>>
>>>     if request.method == 'GET':
>>>         prod_name = request.GET.get('product')  # we are getting selected 
>>> item with this line. we defined 'product' in ajax file
>>>         print("Product name from dropdown: %s " % prod_name)
>>>
>>>         context_dict = {'product_name': prod_name}
>>>
>>>         print("Product Name is:  %s " % context_dict['product_name'])
>>>
>>>
>>>         return render(request, 'FormPage/form.html', (context_dict)) # 
>>> Expecting product1 to be sent to form.html but doesnt.
>>>
>>>
>>> I want to capture 'Product1' from dropdown to below text field of
>>> fom.html
>>>
>>> <label for="productName">Selected Product</label>
>>> <input type="text" name="productName" id="productName" size="38" 
>>> value=""><br>
>>>     <script>
>>>         $(document).change(function() {
>>>             console.log('I am here');  // This gets printed every time when 
>>> I change the dropdown value
>>>             getSelectValue();
>>>             });
>>>     </script>
>>>     <script type='text/javascript'>
>>>         function getSelectValue() {
>>>             field = document.querySelector('#productName');
>>>             var name = "{{product_name}}";               // This always 
>>> gives None when select product from dropdown
>>>             console.log("Received name from view: " + name);
>>>             field.value = name;
>>>         }
>>>     </script>
>>>
>>>
>>>
>>> I need some help, how can I propagate the dynamic change text from
>>> views.py to template form.html page.
>>>
>>> Thank you in advance
>>>
>>
>>> --
>>> 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/be482944-edeb-4fa6-a414-a1f8d2c4b290%40googlegroups.com
>>> <https://groups.google.com/d/msgid/django-users/be482944-edeb-4fa6-a414-a1f8d2c4b290%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>> --
>> 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/CAP0JBPvTh%2Bxg0NozR9bHTGfM6b4q6B3bVZzgJJoN9e_31MZv6Q%40mail.gmail.com
>> <https://groups.google.com/d/msgid/django-users/CAP0JBPvTh%2Bxg0NozR9bHTGfM6b4q6B3bVZzgJJoN9e_31MZv6Q%40mail.gmail.com?utm_medium=email&utm_source=footer>
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
> --
> 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/CAOf%2B%2Bsz4-gow%2BJaO8tVL-XTkZs2Z5A2oG38E%2BaPMD4tOss-Zrg%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CAOf%2B%2Bsz4-gow%2BJaO8tVL-XTkZs2Z5A2oG38E%2BaPMD4tOss-Zrg%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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/CAANT8EG5ajS33GORB2BE_FRRWFoqq8mAfR%2B9TQ7Lfn%2B_Nmu_Pw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to