Zorgan
Zorgan

Reputation: 9173

Getting "This field is required" error even though I set null=True and blank=True

I have a Post model for users submitting posts. I've given the content field of Post an attribute of blank=True. But for some reason django tells me content is still required. form_post.errors prints this:

<ul class="errorlist"><li>content<ul class="errorlist"><li>This field is required.</li></ul></li></ul>

Here's my code:

models

class Post(models.Model):
    ...
    user = models.ForeignKey(User, blank=True, null=True)
    title = models.TextField(max_length=76)
    content = models.TextField(null=True, blank=True)
    category = models.CharField(max_length=20, choices=CATEGORY_CHOICES, default='1')

forms

class PostForm(forms.ModelForm):
    content = forms.CharField(widget=PagedownWidget)
    title = forms.TextInput(attrs={'placeholder': 'title'})

    class Meta:
        model = Post
        fields = [
            'title',
            'content',
            'category',
            'image',
            'id',
            'user'
        ]

views

def post(request):
    allauth_login = LoginForm(request.POST or None)
    allauth_signup = SignupForm(request.POST or None)
    if request.user.is_authenticated():
        form_post = PostForm(request.POST or None, request.FILES or None)
        if form_post.is_valid():
            print('valid')
            instance = form_post.save(commit=False)
            instance.user = request.user

            category = form_post.cleaned_data['category']
            for a, b in CATEGORY_CHOICES:
                if a == category:
                    category = b
                    form_post.save()

            return HttpResponseRedirect('/%s' % category)
        else:
            print(form_post.errors)
            form_post = PostForm()

        context = {
            'allauth_login': allauth_login,
            'allauth_signup': allauth_signup,
            'form_post': form_post
        }

        return render(request, 'post.html', context)
    else:
        return HttpResponseRedirect("/accounts/signup/")

html

...
<form method="post" action="" enctype="multipart/form-data">{% csrf_token %}
<div class="submitContainer">
    <div class="article_title_div">
     {{ form_post.title|add_class:"article_title" }}
     </div>
    <div>
    </div>
        {{ form_post.category }}
    </div>
    <div class="submitButton">
        <button class="submitArticleSubmit" type="submit">Post</button>
</div>
</form>
...

Any idea why I'm getting this error?

Upvotes: 1

Views: 2019

Answers (1)

nik_m
nik_m

Reputation: 12106

The reason for this, is because you're overriding the default model field. Both content and title.

Although, content can be nullable when stored in your database, it is required by your form (content = forms.CharField(widget=PagedownWidget)).

Change to content = forms.CharField(widget=PagedownWidget, required=False) to make it optional on form submission.

Upvotes: 2

Related Questions