通过Django Create View中的GET提供表单数据

时间:2018-10-16 12:33:56

标签: django django-forms django-templates django-views django-class-based-views

我想创建一个CreateView,它允许用户获得一个表单,他们可以在其中单击某些内容并使用无法更改的预填充字段来响应表单。 顺便说一句,我想在<p></p>中而不是<input>中显示这些预填充和不可编辑的字段。下面的代码包含我希望CreateView创建的模型,以及我仍然不知道下一步的表单。

#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

例如,我要在渲染时预填充房间,日期,结束字段。我正在考虑GET,但我想知道使用CreateView类的正确方法。 我想要类似的东西:

<form>
<p>{room field value}</p>
<p>{date field value} </p>
<p>{start field value}</p>
<input id='id_end' type='text'></input>
</form> 

0 个答案:

没有答案