@AprilSnow

Как исправить обработчик форм, чтобы он работал?

Не получается реализовать форму постинга записи рядом с теми записями, которые уже есть.

Форма:
class CinemaForm(forms.Form):
    th_name = forms.CharField(label='Название кинотеатра')
    time = forms.DateTimeField(label='Сеансы')
    address = forms.CharField(label='Адрес кинотеатра')
    cn_name = forms.CharField(label='Название фильма')

Модель:

class Cinema(models.Model):
    theater_name = models.TextField(max_length = 35, blank=False)
    theater_address = models.TextField(max_length=35, blank=False)
    cinema_timetable = models.DateTimeField(blank=False)
    cinema_name = models.TextField(max_length=35, blank=False)

Представление:
def cinema(request):
    all_cinema = Cinema.objects.all()
    cinema_form = CinemaForm
    if request.POST:
        newpost_form = CinemaForm(request.POST)
        if newpost_form.is_valid():
            newpost_form.save()
            return redirect("/")
    return render_to_response("theatername.html", {"cinema": all_cinema, "form": cinema_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 action="/" method="post">
{% csrf_token %}
{{ form.as_ul }}
<input class="btn btn-success" type="submit" value="Добавить">
{% endblock %}

{% block seance %}
{% for theater in cinema %}
<table class="table table-bordered">
  <thead>
    <tr>
      <th>Название кинотеатра</th>
      <th>Расписание</th>
    </tr>
  </thead>
  <tbody>
    <tr>
        <td>{{ theater.theater_name }}</td>
        <td>{{ theater.cinema_timetable }} - {{ theater.cinema_name }}</td>
    </tr>
  </tbody>
</table>
<br>
{% endfor %}
<form action="/" method="post">
{{ form.as_ul }}
<input class="btn btn-success" type="submit" value="Добавить">
{% endblock %}


Таблицы в бд не создаются, что нужно исправить?
fa616b3b54b54e69a34547a07c7ad212.pngd84072fa52274bf1b84d710a4c1bf53b.png
  • Вопрос задан
  • 252 просмотра
Пригласить эксперта
Ответы на вопрос 1
@deliro
Нет такого метода (save) у формы. Вам нужно использовать ModelForm.

class CinemaForm(forms.ModelForm):
    class Meta:
        model = Cinema
        fields = ('theater_name', 'theater_address', 'cinema_timetable', 'cinema_name')
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы
26 апр. 2024, в 09:18
500 руб./в час
26 апр. 2024, в 06:46
1500 руб./в час
26 апр. 2024, в 05:31
1000 руб./за проект