Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/70.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
Python 如果窗体的值存在于db中,则显示窗体_Python_Html_Django_Django Models_Django Forms - Fatal编程技术网

Python 如果窗体的值存在于db中,则显示窗体

Python 如果窗体的值存在于db中,则显示窗体,python,html,django,django-models,django-forms,Python,Html,Django,Django Models,Django Forms,views.py @login_required() def Info_anlegen(request, id=None): item = get_object_or_404(Kunden, id=id) kontaktform_form = InfoForm(request.POST or None, instance=item) if WindowsHome.objects.filter(KN=item.KN).exists(): item1 = Wi

views.py

@login_required()
def Info_anlegen(request, id=None):
    item = get_object_or_404(Kunden, id=id)
    kontaktform_form = InfoForm(request.POST or None, instance=item)
    if WindowsHome.objects.filter(KN=item.KN).exists():
        item1 = WindowsHome.objects.get(KN=item.KN)
        winform_form = InfoWinForm(request.POST or None, instance=item1)
    if kontaktform_form.is_valid():
        return redirect('/Verwaltung/KontaktAnlegen')
    else:
        form = acroniform(instance=item)
        return render(request, 'blog/infokontakt.html',
                      {'kontaktform_form': kontaktform_form, 'winform_form': winform_form})
infokontakt.html

{% extends 'blog/base.html' %}
{% load bootstrap4 %}
{% block supertitle %} InfoPage {% endblock %}
{% block Content %}
{% load static %}
<html>
<div class="p-2 mb-1 bg-white text-black">
    <head>
        <div class="d-flex justify-content-center align-items-center container ">
            <img src="{% static 'blog/Gubler.jpeg' %}" alt="Gubler" height="300" width="700">
        </div>
    </head>
    <br>
    <body>
        <form class="form-row" action="" method="post">
            <div style="margin-left: 2.5em;">
                <font color="black">
                    <div class="col-sm-10 col-form-label">
                        {% csrf_token %}
                        {% bootstrap_form kontaktform_form %}
                    </div>
                </font>
            </div>
        </form>
        <form class="form-row" action="" method="post">
            <div style="margin-left: 2.5em;">
                <font color="black">
                    <div class="col-sm-10 col-form-label">
                        {% csrf_token %}
                        {% bootstrap_form winform_form %}
                    </div>
                </font>
            </div>
        </form>
我怎么说如果db条目不存在,它就不应该显示表单? 或
如果数据库条目不存在,只需显示一个空格“

您尝试将
winform\u form
发送到模板,但在
WindowsHome.objects.filter(KN=item.KN.)exists()为false时未设置它

您可能应该这样做:

@login\u required()
def信息分析(请求,id=None):
上下文={}
item=获取对象或404(Kunden,id=id)
kontaktform_form=InfoForm(request.POST或None,instance=item)
如果WindowsHome.objects.filter(KN=item.KN.)存在():
item1=WindowsHome.objects.get(KN=item.KN)
winform_form=InfoWinForm(request.POST或None,实例=item1)
上下文['winform\u form']=winform\u form
如果kontaktform_form.是有效的():
返回重定向('/Verwaltung/KontaktAnlegen')
其他:
表单=acroniform(实例=项)
上下文['kontaktform_form']=kontaktform_form
返回呈现(请求'blog/infokontakt.html',上下文)

您尝试将
winform\u表单
发送到模板,但当
WindowsHome.objects.filter(KN=item.KN).exists()为false时,未设置该表单

您可能应该这样做:

@login\u required()
def信息分析(请求,id=None):
上下文={}
item=获取对象或404(Kunden,id=id)
kontaktform_form=InfoForm(request.POST或None,instance=item)
如果WindowsHome.objects.filter(KN=item.KN.)存在():
item1=WindowsHome.objects.get(KN=item.KN)
winform_form=InfoWinForm(request.POST或None,实例=item1)
上下文['winform\u form']=winform\u form
如果kontaktform_form.是有效的():
返回重定向('/Verwaltung/KontaktAnlegen')
其他:
表单=acroniform(实例=项)
上下文['kontaktform_form']=kontaktform_form
返回呈现(请求'blog/infokontakt.html',上下文)

您可以在方法的开头将winform\u form初始化为None,这样它就不会抛出该错误。(即)

此外,在模板中,您还可以使用django模板标记{%if%}。。。{%endif%}

i、 e

{%if winform\u form%}
{%csrf_令牌%}
{%bootstrap_form winform_form%}
{%endif%}

您可以在方法的开头将winform\u form初始化为None,这样它就不会抛出该错误。(即)

此外,在模板中,您还可以使用django模板标记{%if%}。。。{%endif%}

i、 e

{%if winform\u form%}
{%csrf_令牌%}
{%bootstrap_form winform_form%}
{%endif%}

ty您的评论与Adaikalaraj的代码配合使用ty您的评论与Adaikalaraj的代码配合使用
UnboundLocalError at /Verwaltung/InfoKontakt/6
local variable 'winform_form' referenced before assignment
Request Method: GET
Request URL:    http://127.0.0.1:8000/Verwaltung/InfoKontakt/6
Django Version: 3.0.1
Exception Type: UnboundLocalError
Exception Value:    
local variable 'winform_form' referenced before assignment
def Info_anlegen(request, id=None):
    winform_form = None  # Do like this
    item = get_object_or_404(Kunden, id=id)
{% if winform_form %}
      <form class="form-row" action="" method="post">
            <div style="margin-left: 2.5em;">
                <font color="black">
                    <div class="col-sm-10 col-form-label">
                        {% csrf_token %}
                        {% bootstrap_form winform_form %}
                    </div>
                </font>
            </div>
        </form>
{% endif %}