Working with forms

Hi, I try to follow the documentation, but I can’t see a formula on my site. Any ideas where I do my mistake?


from django import forms
import datetime

from django.core.exceptions import ValidationError
from django.utils.translation import gettext_lazy as _

class BookingForm(forms.Form):
    belegdatum = forms.DateField(label='Belegdatum')
    buchungstext = forms.CharField(label='Buchungstext')
    betrag = forms.CharField(label='Betrag')

    def clean_date(self):
        if data <
            raise ValidationError(_('Ungültig - Datum in der Vergangenheit'))
        if data >
            raise ValidationError(_('Ungültig - Datum in der Zukunft'))

        return data
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse
from django.http import HttpResponseRedirect
from .models import Cashier, Booking
from .forms import BookingForm

def get_booking(request):
    if request.method == 'POST':
        form = BookingForm(request.POST)
        if form.is_valid():
            return HttpResponseRedirect('/thanks/')
        form = BookingForm()

    return render(request, 'booking.html', {'form': form})

def index(request):
    context = {'cashiers': Cashier.objects.all(), 'titel': 'Kassierer'}
    return render(request=request, template_name='../templates/index.html', context=context)

def booking(request, slug):
    context = {'bookings': Booking.objects.all(), 'titel': 'Buchungsmaske'}
    return render(request=request, template_name='../templates/booking.html', context=context)

{% extends 'base.html' %}
{% block content %}
<h1>{{ titel }}</h1>
<form method="post">
    {% csrf_token %}
    {{ form }}
    <input type="submit" value="Buchen">

{% endblock %}

What is happening that you’re not expecting to have happen? (Or what is not happening that you are expecting to see?)

Are you getting some kind of error message in the browser?

Are you getting some kind of error message in the console where you’re runnning runserver?

Hi Ken,
I am working in PyCharm. Looking on to

The File is shown correctly with no error. But for the. {{ form }} part there is just showing nothing.

I’m assuming that the url /booking/ is a reference to the view named booking. (Or, phrasing this another way, which view is booking/ referring to?)

If so, where are you creating the form in the view to be rendered?

Review the section The view on the Working with forms page.