Can not render dynamic changing string from views.py to template form page

17 views
Skip to first unread message

Samaresh Singh

unread,
May 9, 2019, 4:35:12 PM5/9/19
to Django users
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

Najmath Ummer

unread,
May 10, 2019, 7:57:53 AM5/10/19
to django...@googlegroups.com
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


--
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...@googlegroups.com.
To post to this group, send email to django...@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.
For more options, visit https://groups.google.com/d/optout.

Samaresh Singh

unread,
May 10, 2019, 10:28:44 PM5/10/19
to django...@googlegroups.com
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 


Najmath Ummer

unread,
May 11, 2019, 5:58:57 AM5/11/19
to django...@googlegroups.com
In views return as Jsonresponse
return JsonResponse(context_dict,safe=False)
And in template
You can log into success of ajax. And if it is still not working I will show you an example code.


For more options, visit https://groups.google.com/d/optout.
--

Ravi Kumar

unread,
May 11, 2019, 8:35:19 AM5/11/19
to django...@googlegroups.com
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 

Reply all
Reply to author
Forward
0 new messages