如何将多个表单放在同一个视图中.py函数-Django



我需要在一个views.py函数中放入四个Form,但我不知道如何做到这一点。

我需要每一张表格同时在网站上显示。

我会采纳任何关于如何解决这个问题的建议。

型号.py

# Hotel
class Hotel(models.Model):
customer = models.ForeignKey(Account, null=True, on_delete=models.CASCADE)
name = models.CharField(max_length=50)
short_description = models.TextField(max_length=250)
long_description = models.TextField(max_length=2000, null=True)
HOTEL_STAR = (
('1', '1'),
('2', '2'),
('3', '3'),
('4', '4'),
('5', '5')
)
star = models.TextField(max_length=1, default=5, choices=HOTEL_STAR, null=True)
picture = models.ImageField(upload_to='images/%Y/%m/%d/',default="images/default.jpg" , blank=True)
created = models.DateTimeField(auto_now_add=True, null=True)

def __str__(self):
return str(self.name)
# Room
class Room(models.Model):
hotel = models.ForeignKey(Hotel, null=True, on_delete=models.CASCADE)
def __str__(self):
return f'Room of {self.hotel}'
# Single type for Room
class Single(models.Model):
room = models.ForeignKey(Room, null=True, on_delete=models.CASCADE)
ROOM_NAME = (
('Budget_single_room', 'Budget Single Room'),
('Standard_single_room', 'Standard Single Room'),
('Superior_single_room', 'Superior Single Room'),
('Deluxe_single_room', 'Deluxe Single Room'),
)
name = models.TextField(max_length=30, choices=ROOM_NAME, null=True)
ROOM_SMOKE = (
('Smoking', 'Smoking'),
('Non-Smoking', 'Non-Smoking'),
)
smoke = models.TextField(max_length=15, choices=ROOM_SMOKE, null=True)
bed = models.IntegerField()
capacity = models.IntegerField()
room_size = models.IntegerField()
def __str__(self):
return f'{self.name}'
# Double type for Room
class Double(models.Model):
room = models.ForeignKey(Room, null=True, on_delete=models.CASCADE)
ROOM_NAME = (
('Budget_Double_room', 'Budget Double Room'),
('Standard_Double_room', 'Standard Double Room'),
('Superior_Double_room', 'Superior Double Room'),
('Deluxe_Double_room', 'Deluxe Double Room'),
)
name = models.TextField(max_length=30, choices=ROOM_NAME, null=True)
ROOM_SMOKE = (
('Smoking', 'Smoking'),
('Non-Smoking', 'Non-Smoking'),
)
smoke = models.TextField(max_length=15, choices=ROOM_SMOKE, null=True)
bed = models.IntegerField()
capacity = models.IntegerField()
BED_SIZE = (
('Single_bed_90_130', 'Single Bed / 90-130 cm wide'),
('Double_bed_131_150', 'Double Bed / 131-150 cm wide'),
('Large_bed_king_size_151_180', 'Large Bed (King Size) / 151-180 cm wide'),        
)
room_size = models.IntegerField()

def __str__(self):
return f'{self.name}'
# Bed Quantity for Single and Double types
class BedQuantity(models.Model):
type = models.ForeignKey(Double, null=True, on_delete=models.CASCADE)
number_of_bed = models.IntegerField(null=True)
def __str__(self):
return f'{self.type} with {self.number_of_bed} beds'

表单.py

from django.forms import ModelForm
from .models import Hotel, Apartment, Single, Double, BedQuantity
class HotelForm(ModelForm):
class Meta:
model = Hotel
exclude = ('customer',)
class SingleForm(ModelForm):
class Meta:
model = Single
exclude = ('room',)

class DoubleForm(ModelForm):
class Meta:
model = Double
exclude = ('room',)

class BedQuantityForm(ModelForm):
class Meta:
model = BedQuantity
exclude = ('type',)

class ApartmentForm(ModelForm):
class Meta:
model = Apartment
exclude = ('customer',)

视图.py

from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
from .models import Hotel, Room, Single, Double, BedQuantity
from .forms import HotelForm, ApartmentForm, SingleForm, DoubleForm, BedQuantityForm
@login_required
def add_hotel(request):
form = HotelForm()
if request.method == "POST":
form = HotelForm(request.POST, request.FILES)
if form.is_valid():
data = form.save(commit=False)
data.customer = request.user
data.save()
return redirect('/')
context = {'form':form,}
return render(request, 'add_hotel.html', context)

如果要从view中呈现多个表单,则必须首先在视图中实例化所有表单,然后将它们传递给模板。

沿着这些思路应该会奏效。

from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
from .models import Hotel, Room, Single, Double, BedQuantity
from .forms import HotelForm, ApartmentForm, SingleForm, DoubleForm, BedQuantityForm
@login_required
def add_hotel(request):
# instantiate the forms
hotel_form = HotelForm()
apartment_form = ApartMentForm()
single_form = SingleForm()
double_form = DoubleForm()
if request.method == "POST":
hotel_form = HotelForm(request.POST, request.FILES)
apartment_form = ApartMentForm(request.POST)
single_form = SingleForm(request.POST)
double_form = DoubleForm(request.POST)
if form.is_valid():
data = hotel_form.save(commit=False)
data.customer = request.user
data.save()
# do your thing with other forms and save them as well
return redirect('/')
context = {
'hotel_form': hotel_form,
'apartment_form': apartment_form,
...
}
return render(request, 'add_hotel.html', context)

另外,作为一条单独的建议,请尽量避免将pythonkeywords(如type(用于变量名。

最新更新