Это мой код:
models.py >
Код: Выделить всё
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Booking(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="user_name")
booking_date = models.DateField(auto_now=True)
status = (
('Requested', 'Requested'),
('Confirmed', 'Confirmed'),
('Cancelled', 'Cancelled'),
)
booking_status = models.CharField(choices=status, blank=True, null=True, default='Requested')
first_name = models.CharField(max_length=100, unique=True)
last_name = models.CharField(max_length=100, unique=True)
birth_date = models.DateField()
email = models.EmailField(max_length=100, unique=True)
phone_number = models.BigIntegerField()
address = models.CharField(max_length=200)
zip_code = models.CharField(max_length=100)
city = models.CharField(max_length=100)
country = models.CharField(max_length=100)
booking_object = (
('Upper Apartment', 'Upper Apartment'),
('Lower Apartment', 'Lower Apartment'),
('Whole House', 'Whole House'),
)
booking_item = models.CharField(choices=booking_object)
arrival_date = models.DateField()
departure_date = models.DateField()
guest_nationality = (
('German', 'German'),
('Other', 'Other'),
)
nationality = models.CharField(choices=guest_nationality)
passport_number = models.CharField(max_length=100, blank=True, null=True)
animals = models.BooleanField(blank=True, null=True)
message = models.TextField(max_length=1000)
class Meta:
ordering=['last_name','first_name']
def __str__(self):
return f'Booking: {self.last_name}, {self.first_name}'
Код: Выделить всё
from django.forms import forms, ModelForm
from .models import Booking
class BookingForm(ModelForm):
class Meta:
model = Booking
fields = ('first_name', 'last_name', 'birth_date', 'email', 'phone_number', 'address', 'zip_code', 'city', 'country', 'booking_item', 'arrival_date', 'departure_date', 'nationality', 'passport_number', 'animals', 'message')
Код: Выделить всё
from django.contrib import messages
from django.http import HttpResponse, HttpRequest, HttpResponseRedirect
from django.shortcuts import render, get_object_or_404, redirect, reverse
from .models import Booking
from .forms import BookingForm
# Booking Form
def book_apartment(request):
if request.method == "POST":
form = BookingForm(request.POST)
if form.is_valid():
form.save()
messages.add_message(request, messages.SUCCESS, "Thank you for booking with us. We will be in touch with you soon.")
return redirect(main_page)
else:
messages.add_message(request, messages.ERROR, "Please fill out all form fields.")
form = BookingForm()
return render(
request,
"{% url 'main-page' %}",
)
Код: Выделить всё
{% extends "base.html" %}
{% load static %}
{% load crispy_forms_tags %}
{% block content %}
Booking
{{ form | crispy }}
{% csrf_token %}
Submit
{% endblock %}
Код: Выделить всё
Booking
First Name
Last Name
Birth Date
E-Mail
Telephone Number
Address
ZIP Code
City
Country
You would like to book...
Upper Apartment
Lower Apartment
Whole House
Arrival Date
Departure Date
Your Nationality
German
Other
Passport Number
Pets?
Your Message
Send
Мой другой подход нужно было создать форму через form.py, но на странице booking.html тоже ничего не отображается.
Подробнее здесь: https://stackoverflow.com/questions/785 ... m-to-datab