I am a newbie. I am learning Django by developing a web application. There is use-case where I got stuck.
there are 3 buttons on a html page
Personal info
Education
Work Experience
It is desired when the user click any of the button, respective html form shall be displayed on right-half of web-page.
What I have tried is this, onclick function of buttons:
function myfun()
{
var xmlhttp;
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("rightpan").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","{% url "polls:demo_test" %}",true);
xmlhttp.send();
}
</script>
in views.py, demo test does this:
def demo_test(request):
return HttpResponse("<form name='reg' onsubmit='return ValidateForm(this)' action='{% url 'polls:signupexec' %}' method='post'><tr><td><div>Firstname:</div></td><td><input type='text' name='first_name' /></td></tr><tr><td><div>Lastname:</div></td><td><input type='text' name='last_name' /></td></tr><tr><td><input name='submit' type='submit' value='Submit' /></td></tr></form>")
- First of all, it works fine except that it does not embed CSRF_TOKEN in the form, which is important.
- Secondly, I feel that this approach is against the philosophy of django.
Please give some suggestions on how can I do this. Loading HTML Forms dynamically through AJAX.