I tried to implement the solution here , but I cannot get it to work correctly.
I have a div that is populated using a loop inside a Django template. Right below that, I have an input field where I can type the text and click "Submit." The Submit action should trigger a jQuery script that receives the model object from the server. Then this model object should be passed to the div, and the div should be “updated”. The goal is that after the div is “updated”, the variable that the for loop accesses would be updated, thereby displaying additional new results.
My template code is:
<h1> This is a Test Ajax Page</h1>
<div id="refresh-this-div">
{% for comment in question.comment_set.all %}
<p class="">{{ comment.body }}</p>
{% endfor %}
<input id="my-text-input-id" type="text" />
<button type="submit" class="add-comment-button">Add</button>
</div>
</div>
My jQuery:
<script type="text/javascript">
$(document).ready(function() {
$("button.add-comment-button").click(function() {
var com_body = $('#my-text-input-id').val();
$.ajax({
url: '/test_login_url',
success: function(data) {
$('#refresh-this-div').html(data);
}
});
});
});
</script>
My opinion:
def test_login_url(request):
question = Question.objects.get(id=1)
com = Comment(question=question, body='This is a new Comment!')
question.comment_set.add(com)
return render_to_response('application/ajax_test_template.html', { 'question': question })
"", div , div, , h1. " ", h1- .
, : before_clicking_submit
: after_clicking_submit
, , , , , , , - . div ?