'CSRF verification failed. Request aborted. After deployment to cloud

I wrote a Django application and after I developed it to put it into a Docker container and deploy it to Google Cloud Run. The server is OK and running but when I want to type something in the forms and submit it I got this error:

Forbidden (403) CSRF verification failed. Request aborted.

What's the problem with it? Should I add something besides the csrf_token?

Here's my views.py:

from django.shortcuts import render, redirect
from . models import Action
##from django.core.mail import send_email

def index (request):
    actions = Action.objects.all()
    return render(request, 'index.html', {"actions": actions})

def uj_szemely (request):
    
    if request.method == "GET":
        return render(request, 'uj_szemely.html')
    elif request.method == "POST":
        surname = request.POST.get('surname')
        lastname = request.POST.get('lastname')
        whatdo = request.POST.get('whatdo')
        done = request.POST.get('done')
        created = request.POST.get('created')
        deadline = request.POST.get('deadline')
        
        if done == 'on':
            done = True
        else:
            done = False
            
        formaction = Action(surname=surname, lastname=lastname, whatdo=whatdo, done=done, created=created, deadline=deadline)
        formaction.save()
        
        return redirect('fooldal')
    

def fooldal (request):
    return render(request, 'fooldal.html')

My models.py:

from pyexpat import model
from django.db import models

class Action (models.Model):
    surname = models.CharField(max_length=100)
    lastname = models.CharField(max_length=100)
    whatdo = models.TextField()
    done = models.BooleanField(default=False)
    created = models.DateField(auto_now_add=True)
    deadline = models.DateTimeField()
    

I also have a contact page on the website and the contact form is giving the same error. Here is my contact forms module:

from logging import PlaceHolder
from django import forms

temak = [('egyéb', 'Egyéb'), ('reklamáció', 'Reklamáció'),('megkeresés', 'Megkeresés'), ('tájékoztatás', 'Tájékoztatás')]

class ContactForm(forms.Form):
    nev = forms.CharField(max_length=50, label="Név", widget=forms.TextInput(attrs={'placeholder': 'Írja be a nevét!'}))
    email = forms.EmailField(widget=forms.TextInput(attrs={'placeholder': 'Írja be az e-mail címét!'}))
    tema = forms.ChoiceField(choices=temak, label="Téma")
    uzenet = forms.CharField(label="Üzenet", widget=forms.Textarea(attrs={'placeholder': 'Írjon nekünk...'}))

And my contact's views.py:

from django.shortcuts import render
from .forms import ContactForm
from django.core.mail import send_mail
from django.core.mail import EmailMessage

def kapcsolat (request):
    form = ContactForm()
    
    if request.method == "POST":
        form = ContactForm(request.POST)
        if form.is_valid():
            nev = form.cleaned_data['nev']
            email = form.cleaned_data['email']
            tema = form.cleaned_data['tema']
            uzenet = form.cleaned_data['uzenet']
            
            email_to_send = EmailMessage (
                subject=tema,
                body=uzenet,
                from_email=email,
                to=['[email protected]'],
                reply_to=[email],
                headers={'Content-Type': 'text/plain'}
            )
            
            email_to_send.send()
            
            return render(request, 'kapcsolat_valasz.html', {'nev':nev, 'tema':tema})
        
        
    return render(request, 'kapcsolat.html', {'form':form})



Solution 1:[1]

Did you add {% csrf_token %} inside your form? please share your form code.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 abdulalim