Théo Lavaux
Théo Lavaux

Reputation: 1454

Django form prints no error and is not valid

To create a form in my website, I created some models corresponding to my fields. Then I created ModelForms from them, some views and templates. My problem is that I never see my forms errors first, and second, this particular field always has his form not valid, even with one attribute in it. Can you explain me what I'm doing wrong ?

models.py

class Grapheme(models.Model):
    lexeme = models.ForeignKey(Lexeme, on_delete=models.CASCADE)

    value = models.CharField(max_length=256)

    class Meta:
        verbose_name = "grapheme"
        ordering = ["value"]

    def __str__(self):
        return self.value

forms.py

class GraphemeForm(forms.ModelForm):
    class Meta:
        model = Grapheme
        fields = ['value']

views.py

@login_required
def lexeme_edit_view(request, lexicon_id):
    [...]

    if request.method == 'POST':
        lexeme_form = LexemeForm(request.POST)
        grapheme_form = GraphemeForm(request.POST)
        [...]

        if grapheme_form.is_valid(): # This line fails
        [...]

template.html

{% if grapheme_form.non_field_errors %}
    <div class="alert alert-danger" role="alert">
        {% for error in grapheme_form.non_field_errors %}
            {{ error }}
        {% endfor %}
    </div>
{% endif %}

[...]

<div class="form-group row">
    <label for="graphemeInput" class="control-label col-lg-2">{{ grapheme_form.value.label }}</label>
    <div class="col-lg-6">
        {% if grapheme_form.is_bound %}
            {% if grapheme_form.value.errors %}
                {% for error in grapheme_form.value.errors %}
                    <div class="invalid-feedback">
                        {{ error }}
                    </div>
                {% endfor %}
            {% endif %}

            {% if grapheme_form.value.help_text %}
                <small class="form-text text-muted">{{ grapheme_form.value.help_text }}</small>
            {% endif %}
        {% endif %}

        {% render_field grapheme_form.value type="text" class+="form-control" id="graphemeInput" %}
    </div>
</div>

Upvotes: 1

Views: 578

Answers (1)

matyas
matyas

Reputation: 2796

The problem is your lexeme Foreign Key on your Grapheme Model.

Since you are using django Modelforms if you do not set blank=True, null=True to a Foreign Key relationship it automatically becomes a mandatory field.

in your Form you declared that you do not want to show the lexeme foreign key selection therefore it does not appear on your form:

fields = ['value']

this is why you are getting the required field missing error on your form.

You have two possible solutions:
Solution 1
add blank=True, null=True to your foreign key relationship:

lexeme = models.ForeignKey(Lexeme, blank=True,null=True on_delete=models.CASCADE)


Solution 2: set the lexeme value when you intialize your form:

class GraphemeForm(Form):
def __init__(self, *args, **kwargs):
    super().__init__(*args, **kwargs)
    lexeme = Lexeme.objects.get(id=1)
    self.fields['lexeme'].initial = lexeme

class Meta:
    model = Grapheme
    fields = ['value']

Upvotes: 1

Related Questions