A
A
AprilSnow2015-06-06 19:32:37
Django
AprilSnow, 2015-06-06 19:32:37

How do I fix the form handler so that it works?

It is not possible to implement the entry posting form next to the entries that already exist.
The form:

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)

Performance:
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})

Sample:
{% 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 %}

Tables in the database are not created, what needs to be fixed?
fa616b3b54b54e69a34547a07c7ad212.pngd84072fa52274bf1b84d710a4c1bf53b.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Kitaev, 2015-06-06
@deliro

There is no such method (save) for the form. You need to use ModelForm.

class CinemaForm(forms.ModelForm):
    class Meta:
        model = Cinema
        fields = ('theater_name', 'theater_address', 'cinema_timetable', 'cinema_name')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question