Answer the question
In order to leave comments, you need to log in
How to implement automatic updating of the block with comments without reloading the page?
There is a base.html template :
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>{% block title %}{% endblock %}</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
setTimeout(F1, 2000);
function F1(){
$.get('/', function(data){
$('#comment').html(data);
});
setTimeout(F1, 2000);
}
</script>
</head>
<body>
{% block content %}
{% endblock %}
</body>
</html>
{% extends 'task/base.html' %}
{% block title %} Тестовая задача | Все комментарии {% endblock %}
{% block content %}
<div id="comment">
<h2>Все комментарии</h2>
{% for comment in comments %}
<hr>
Имя: {{ comment.name }}<br>
Email: {{ comment.email }}<br>
Комментарий: <b>{{ comment.text }}</b><br>
Дата: {{ comment.create }}<br>
<a href="{% url 'comments:comment_delete' pk=comment.pk %}" onclick="return confirm('Точно удалить?')">Удалить</a>
<hr>
{% endfor %}
</div>
<div class="row">
<div id="form">
<h2>Добавить новый комментарий</h2>
<form method="POST" action="#" id="form">{% csrf_token %}
{{ form.as_p }}
<button type="submit">Отправить</button>
</form>
<hr>
</div>
<div class="pagination">
<span class="step-links">
{% if comments.has_previous %}
<a href="?page={{ comments.previous_page_number }}">Назад</a>
{% endif %}
<span class="current">
Страница {{ comments.number }} из {{ comments.paginator.num_pages }}.
</span>
{% if comments.has_next %}
<a href="?page={{ comments.next_page_number }}">Вперёд</a>
{% endif %}
</span>
</div>
</div>
{% endblock %}
Answer the question
In order to leave comments, you need to log in
1. Split the form and the list into 2 separate files.
2. Update - only the list. (form - do not touch)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question