I want to create a CreateView which allow user get a form where they click somethings and response a form with pre-filled fields which cannot not be change.
By the way, I want to display these prefilled and uneditable fields in <p></p>
instead of <input>
. The code below contains the model which I want CreateView to create, and the form which I still have no idea how to do next.
#models.py
class Booking(Occurrence):
TYPE_CHOICES = (
('web', 'Web Application'),
('emb', 'Embedded Application'),
)
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='bookings')
room = models.ForeignKey(Room, on_delete=models.CASCADE, related_name='bookings')
type = models.CharField(max_length=50, choices=TYPE_CHOICES, default='web')
date = models.DateField()
start= models.TimeField()
end = models.TimeField()
check_in = models.BooleanField(default=False)
check_out = models.BooleanField(default=False)
#views.py
@method_decorator(login_required, name='dispatch')
class BookingAddView(CreateView):
model = Booking
form_class = BookingForm
template_name = 'booking/add_booking.html'
def get_initial(self):
pass
def form_valid(self, form):
form.instance.user = self.request.user
super().form_valid(form)
def form_invalid(self, form):
pass
#forms.py
class BookingForm(forms.Model):
class Meta:
model = Booking
fields = ['user', 'room', 'date', 'start', 'end']
def __init__(self):
pass
For example, I want room, date, end fields prefilled when rendering. I am thinking of GET but I want know the right way to do it with class CreateView. I want to something like:
<form>
<p>{room field value}</p>
<p>{date field value} </p>
<p>{start field value}</p>
<input id='id_end' type='text'></input>
</form>
and render them in inside form tags?. And I have asked one more question about providing value to form via GET in Create View class.
– Q.Nguyen Oct 16 '18 at 12:58