Answer the question
In order to leave comments, you need to log in
How to attach ajax to this form?
You need to glue ajax so that the page is not updated when a post request is sent to the server.
Модель и форма:
from django.db import models
from django import forms
class Cinema(models.Model):
theater_name = models.TextField(max_length = 35, blank=False)
theater_address = models.TextField(max_length=35, blank=False)
cinema_name = models.TextField(max_length=35, blank=False)
class CinemaForm(forms.ModelForm):
class Meta:
model = Cinema
fields = ('theater_name', 'theater_address', 'cinema_name')
Представление:
def cinema(request):
form = CinemaForm(request.POST or {})
if request.POST and form.is_valid():
form.save()
return redirect("/")
return render(request, "theatername.html", {"cinema": Cinema.objects.all(), "form": form})
Шаблон:
{% block theater %}
{% for theater in cinema %}
<table class="table table-bordered">
<thead>
<tr>
<th>Адрес</th>
<th>Название кинотеатра</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{ theater.theater_address }}</td>
<td>{{ theater.theater_name }}</td>
</tr>
</tbody>
</table>
<br>
{% endfor %}
<form method="post">
{% csrf_token %}
{{ form.as_ul }}
<input class="btn btn-success" type="submit" value="Добавить">
</form>
{% endblock %}
Answer the question
In order to leave comments, you need to log in
very little mana for django + ajax
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question