Aayanam Anirudh
Aayanam Anirudh

Reputation: 47

crispy forms adding is-invalid class to my fields before form submission

I am using Django. I have created my forms as below

forms.py

class PostForm(forms.Form):
    title = forms.CharField(max_length=50, required=True)
    body = forms.CharField(max_length=10000, required=True,widget=forms.Textarea(attrs={"rows":"5"}))
    from_location = forms.CharField(max_length=50, required=True)
    to_location = forms.CharField(max_length=50, required=True)

views.py


def create_post(request):
    if request.user.is_authenticated:
        post_form = PostForm(request.POST)
        print('printing title..')
        if request.method == 'POST':

            if post_form.is_valid():
                post = Post.objects.create(title = request.POST['title'], body=request.POST['body'], from_location=request.POST['from_location'], to_location=request.POST['to_location'], author = request.user, uuid = uuid.uuid4())
                message = messages.success(request, f'Your post has been created!')
                return redirect(reverse_lazy('posts:post'))

    else:
        post_form = PostForm()
        return redirect(reverse_lazy('posts:post'))
    context = {
        'post_form':post_form
    }

    return render(request, 'posts/create_post.html', context)


I am getting a red border on all the form fields. I realized that crispy-forms has added the class is-invalid to all the fields making this. If I make required=False in forms, the error is gone.

Current output

Screenshot of output

expected output is the image above without the red borders and warnings

I tried removing the class from dev tools and it worked. I asked chatGPT and tried its methods, but it did not help

Upvotes: 1

Views: 144

Answers (1)

Mohammad Golam Dostogir
Mohammad Golam Dostogir

Reputation: 1884

You are having this issue because in your views.py file post_form = PostForm(request.POST) is outside of the if request.method == 'POST' POST request check.

Update your views.py file to this:

def create_post(request):
    if request.user.is_authenticated:
        if request.method == 'POST':
            post_form = PostForm(request.POST)

            if post_form.is_valid():
                post = Post.objects.create(title=request.POST['title'], body=request.POST['body'], from_location=request.POST['from_location'], to_location=request.POST['to_location'], author=request.user, uuid=uuid.uuid4())
                messages.success(request, f'Your post has been created!')
                return redirect(reverse_lazy('posts:post'))
        else:
            post_form = PostForm()
    else:
        return redirect(reverse_lazy('posts:post'))

    context = {
        'post_form': post_form
    }

    return render(request, 'posts/create_post.html', context)

This will solve the form validity issue on render on the page. You should always check the form method request first before rendering the form on the page or passing the request.POST form data.

Upvotes: 1

Related Questions