Home > OS >  Django form throwing errors on display
Django form throwing errors on display

Time:10-24

I have created a POST Django form with several input fields. Many of them are required and they throw the error that they are required on the website render. Also they throw the error above them, even though I specified they threw it in the block underneath. Please help.

FORM DEFINITION

class Install_Version_Form(forms.Form):
    project = forms.CharField(widget=forms.TextInput(
        attrs={'class': 'textInputAddMach', 'placeholder': "project"}), max_length=100, required=True)
    privacy = forms.ChoiceField(choices=privacy, required=True)
    iteration = forms.CharField(widget=forms.TextInput(
        attrs={'class': 'textInputAddMach', 'placeholder': "iteration"}), max_length=100, required=True)
    machine_name = forms.CharField(widget=forms.TextInput(
        attrs={'class': 'textInputAddMach', 'placeholder': "machine name"}), max_length=100, required=True)
    version = forms.CharField(widget=forms.TextInput(
        attrs={'class': 'textInputAddMach', 'placeholder': "e.g. 0.1"}), max_length=100, required=True)

    def __init__(self, *args, **kwargs):
        super(Install_Version_Form, self).__init__(*args, **kwargs)

    def clean(self):
        cleaned_data = super().clean()

FORM VIEW CTX PACKING

def installation(request):
    ctx = {
        "installVersionForm": None
    }
    ctx["installVersionForm"] = Install_Version_Form(request.POST)
    if request.method == 'POST':
        form = Install_Version_Form(request.POST)
        if form.is_valid():
          (contact cloud)
    else:
        ctx["installVersionForm"] = form
    
    return render(request, "installation.html", ctx)

HTML TEMPLATE

{% block extend_content %}
    <form action="" method="post">
        {% csrf_token %}
        {{installVersionForm.as_p}}
        <input type="submit" value="add new version">
    </form>
    <br/>
    <br/>
    <br/>
    {% if installVersionForm.errors %}
        {% for error in installVersionForm.errors %}
            <small>{{ error }}</small>
        {% endfor %}
    {% endif %}
    {% if installVersionForm.messages %}
        {% for message in installVersionForm.messages %}
            <small>{{ message }}</small>
        {% endfor %}
    {% endif %}
{% endblock %}

CodePudding user response:

When you first create a form instance you don't have to give request.POST. If you do, it will handle things as if it was filled by your user even if it wasn't.

def installation(request):
    ctx = {
        "installVersionForm": None
    }
    ctx["installVersionForm"] = Install_Version_Form() <=====
    if request.method == 'POST':
        form = Install_Version_Form(request.POST)
        if form.is_valid():
          (contact cloud)
    else:
        ctx["installVersionForm"] = form

    return render(request, "installation.html", ctx)

And to have something clean I would do it this way :

def installation(request):
    if request.method == 'GET':
        ctx = {
            "installVersionForm": Install_Version_Form()
        }
    if request.method == 'POST':
        form = Install_Version_Form(request.POST)
        if form.is_valid():
          (contact cloud)
        else:
            ctx = {
            "installVersionForm": Install_Version_Form(request.POST)
        }


    return render(request, "installation.html", ctx)
  • Related