在Django表单中添加多个隐藏字段

时间:2019-02-27 16:31:49

标签: django

我正在使用Django。我需要一个表格来显示我创建的班级的3个字段,其想法是每次您想添加一个新的日期和时间开始和结束时,都显示一个新的新部分,以便可以找到每个老师在一天中更多的时间开始和结束(这三个字段在一起)

如果有人有任何想法,我将不胜感激。

Models.py

class Profesor(Person):
    legajo = models.IntegerField(blank=True, null=True)
    type = models.CharField(max_length=30)
    matter = models.CharField(max_length=100, blank=True, null=True)
    calendar = models.ForeignKey('calendar', on_delete=models.DO_NOTHING)
    user = models.CharField(max_length=20, blank=True, null=True)

class calendar(models.Model):
    day = models.DateTimeField(default=date.today().isoweekday())
    hs_init = models.DateTimeField(default=datetime.now().hour)
    hs_end = models.DateTimeField(default=datetime.now().hour)

Forms.py

class addProfesorForm(ModelForm):

    calendar = forms.ModelChoiceField(queryset=calendar.objects.all(), widget=forms.HiddenInput())
    class Meta:
        model = Profesor
        TYPES = (
            ('Motiv', 'Motiv'),
            ('Academic', 'Académic'),
            ('Otro', 'Otro')
        )
        help_texts = {
            'matter': 'message'
        }
        fields = ['id', 'type', 'matter']
        widgets = {
            'type': Select2Widget(choices=typeS)
        }

class calendarForm(ModelForm):
    class Meta:
        model = calendar
        fields = ['day','hs_init','hs_end']

Views.py

def agregarTutor(request):
    if request.method == 'POST':
        form = addProfesorForm(request.POST['calendar'])
        calendar=calendar.objects.all()[0]
        if form.is_valid():
            id = form.cleaned_data['id']
            try:
                person_Sysatem = SysatemPerson.objects.get(pk=id)
                alumn_Sysatem = SysatemAlumn.objects.get(pk=id)

            except SysatemPerson.DoesNotExist:
                return render(request, 'menu/new-Profesor.html',
                              {'new_manual': True, 'not_found': True, 'nbar': 'profesors', 'id': id})

            new_Profesor = Profesor(
                nombre=person_Sysatem.nombre.rstrip(),
                id=person_Sysatem.numerodocu,
                legajo=alumn_Sysatem.legajo,
                telefono=person_Sysatem.telefono.rstrip(),
                mail=person_Sysatem.mail.rstrip(),
                type=form.cleaned_data['type'],
                calendar=form.cleaned_data['calendar'],
                matter=form.cleaned_data['matter'],
                user=id,
            )
            Profesor.save(new_Profesor)
            contrasena = 'id'+str(id)[0:5]
            user = User.objects.create_user(id, person_Sysatem.mail.rstrip(), contrasena)
            user.first_name = person_Sysatem.nombre.rstrip()
            user.save()
            form = addProfesorForm(initial={'calendar':calendar})
            return render(request, 'menu/new-Profesor.html', {'form': form, 'Profesor': new_Profesor, 'success': True, 'nbar': 'profesors'})
        else:
            return render(request, 'menu/new-Profesor.html', {'form': form, 'error_form': True, 'nbar': 'profesors'})
    else:
        form = addProfesorForm()
        return render(request, 'menu/new-Profesor.html', {'form': form, 'nbar': 'profesors'})

Template.html

<h2>new Profesor</h2>
            <div class="row">
                <div class="col">
                    <form method="post">{% csrf_token %}
                        {% include 'common/form_template.html' with form=form %}
                        <button type="submit" style="margin-top: 10px; float: right;" class="btn btn-primary">Add</button>
                    </form>
                </div>
            </div>

0 个答案:

没有答案