Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
未从表单上下文传递Django模板_Django_Python 3.x_Django Forms_Django Templates_Django Views - Fatal编程技术网

未从表单上下文传递Django模板

未从表单上下文传递Django模板,django,python-3.x,django-forms,django-templates,django-views,Django,Python 3.x,Django Forms,Django Templates,Django Views,在我的站点中,我试图创建一个模板,自动显示模型中的表单字段。我正在使用渲染在视图中传递表单的实例,但模板无法识别表单,也无法显示任何输入字段 views.py: def create_booking(request): if request.method == 'POST': form = BookingsForm(request.POST) form2 = BookingsFormRecurring(request.POST) if form.is_valid() an

在我的站点中,我试图创建一个模板,自动显示模型中的表单字段。我正在使用渲染在视图中传递表单的实例,但模板无法识别表单,也无法显示任何输入字段

views.py:

def create_booking(request):
if request.method == 'POST':
    form = BookingsForm(request.POST)
    form2 = BookingsFormRecurring(request.POST)
    if form.is_valid() and form2.is_valid():
        booking = form.save()
        bookingRecurring = form2.save()
        return redirect("confirm")
else:
    form = BookingsForm()
    form2 = BookingsFormRecurring()

return render(request, 'makebooking.html', { 'form': form, 'form2': form2})
forms.py

class BookingsForm(forms.ModelForm):

    studentUsername = 'test'
    startingDate = forms.DateInput()
    teacherID = forms.CharField(label = 'Select teacher', widget=forms.Select(choices=TeacherCHOICES))
    startingTime = forms.CharField(label = 'Available starting times', widget=forms.Select(choices=TimeCHOICES))
    lessonDuration = forms.CharField(label = 'For how long?', widget=forms.Select(choices=DURATION_CHOICES))
    instrumentFocus = forms.CharField(label = 'Which instrument?', widget=forms.Select(choices=INSTRUMENTS_CHOICES))

    class Meta:
        model = bookingsModel
        fields = ('teacherID', 'startingDate', 'startingTime', 'lessonDuration', 'instrumentFocus')
        widgets = {
        'startingDate': DateInput()
    }

class BookingsFormRecurring(forms.ModelForm):

    lessonRepeat = forms.CharField(label = "How often?", widget=forms.Select(choices=REPEATS_CHOICES), required=False)
    secondaryLessonDay = forms.CharField(label = "Secondary lesson day", widget=forms.Select(choices=LESSON_DAY_CHOICES), required=False)
    secondaryLessonTime = forms.CharField(label = "Secondary lesson time", widget=forms.Select(choices=TimeCHOICES), required=False)
    tertiaryLessonDay = forms.CharField(label = "Third lesson day", widget=forms.Select(choices=LESSON_DAY_CHOICES), required=False)
    tertiaryLessonTime = forms.CharField(label = "Third lesson time", widget=forms.Select(choices=TimeCHOICES), required=False)


    class Meta:
        model = bookingsModelRecurring
        fields = ('lessonRepeat', 'secondaryLessonDay', 'secondaryLessonTime', 'tertiaryLessonDay', 'tertiaryLessonTime')
makebooking.html(模板)

。。。。
在这里预订课程!

{%if user.u经过身份验证%}
{%csrf_令牌%}


{%形式的字段为%} {{field.label_tag}}
{{field}} {%if field.help_text%} {{field.help_text} {%endif%} {%字段中有错误。错误%}

{{error}

{%endfor%}


{%endfor%}
....
我不认为模板出于某种原因识别了
{{form}
,因为当我只使用
{{form}}
时,什么都没有显示

编辑:
form2
稍后也将在模板页面中使用,使用与表单相同的语法

如何知道上下文变量未传递给模板?尝试将
{{form}}
添加到模板中,查看它是否打印任何内容。将{{form}添加到模板中也不会在模板上显示任何内容当前用户是否经过身份验证?是,如果用户未经身份验证,我有它的显示文本,告诉他们在预订前登录我不知道你在这里发布时是否把格式弄乱了,但是
def
下没有缩进任何内容。除此之外,如果user.u经过身份验证,则模板具有
。视图中是否有显示身份验证的内容?
....

<div class="container">
<div class="card-panel blue-grey darken-1 white-text">
    <h3>Make a lesson booking here!</h3>
{% if user.is_authenticated %}


<div class="row">
    <form class="booking" id="booking" action="" method="post">
    {% csrf_token %}
    <div class="col s4">

    <br><br>

        {% for field in form %}
            <p>
                {{ field.label_tag }}<br>
                {{ field }}
                {% if field.help_text %}
                    <small style="color: grey">{{ field.help_text }}</small>
                {% endif %}
                {% for error in field.errors %}
                    <p style="color: red">{{ error }}</p>
                {% endfor %}
            </p>
            <br>
        {% endfor %}

        <br>
....