0

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> 
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
Q.Nguyen
  • 171
  • 1
  • 8
  • If you want them to display in p tags and not be able to edit them, there's no form here at all. What are you asking? – Daniel Roseman Oct 16 '18 at 12:36
  • It still has the field end, can I just simple turn the prefilled values of form to

    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
  • You can show the non editable data as we normally do in detailview and add a form in the same template by using `get_context_data`. In the form add those fields you want to edit. You can refer this question https://stackoverflow.com/questions/16931901/django-combine-detailview-and-formview – Bidhan Majhi Oct 17 '18 at 05:40

0 Answers0