python – Display form if its value exists in db-ThrowExceptions

Exception or error:

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>

My Problem is:

if WindowsHome.KN exists it gets displayed

but if it does not exist i get the error

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

How do i say that if the db entry does not exist it should not display the form ?
OR
if the db entry does not exist just display a spacing ” ”

How to solve:

You try to send winform_form to your template but it is not set when WindowsHome.objects.filter(KN=item.KN).exists() is false.

You should probably do something like this:

@login_required()
def Info_anlegen(request, id=None):
    context = {}
    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)
        context['winform_form'] = winform_form
    if kontaktform_form.is_valid():
        return redirect('/Verwaltung/KontaktAnlegen')
    else:
        form = acroniform(instance=item)
        context['kontaktform_form'] = kontaktform_form
        return render(request, 'blog/infokontakt.html', context)

###

You can initialize winform_form as None in the begining of your method, so that it will not throw that error. (i.e)

def Info_anlegen(request, id=None):
    winform_form = None  # Do like this
    item = get_object_or_404(Kunden, id=id)

and also in your template you can use django template tags {% if %} … {% endif %}

i.e

{% 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 %}

Leave a Reply

Your email address will not be published. Required fields are marked *