Hello @all,
there are many questions regarding this topic and it feels like I have read all. But I still can’t solve this problem.
I can load my form and fill in the fields, but when I send the form I get the error that the csrf token is missing.
Template tag {% csrf_token %} is in the form
‘django.middleware.common.CommonMiddleware’, is used
Cookies are accepted
I can see the csrf token in the form when the site is loaded
Tried different browsers (also privat browsing)
Django version 5.0.7
template
{% block content %}
<div class="row mb-4">
<div class="col">
<h3>Registrierung</h3>
</div>
</div>
<div class="row mb-4">
<div class="col">
<img src="{% static 'app1/logo.png' %}"><img src="{% static 'logo.png' %}"><a href="/admin">Zum Admin-Login</a>
</div>
</div>
<div class="row mb-4">
<div class="col">
<form method="post" action="" enctype="text/plain">
{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="btn btn-success btn-sm">Speichern</button>
</form>
</div>
</div>
{% endblock content %}
forms.py
from django import forms
from .models import Person
class PersonForm(forms.ModelForm):
class Meta:
model = Person
fields = '__all__'
models.py
from django.db import models
class Person(models.Model):
vorname = models.CharField(max_length=50)
nachname = models.CharField(max_length=50)
email = models.EmailField()
phone = models.CharField(max_length=50)
def __str__(self):
return self.vorname +' '+ self.nachname
views.py
from django.http import HttpResponseRedirect
from django.shortcuts import render, HttpResponse
from .forms import PersonForm
from django.shortcuts import render, redirect
# Create your views here.
def home(request):
if request.method == 'POST':
form = PersonForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect('/success')
else:
form = PersonForm()
return render(request, 'app1/index.html', {'form': form})
def success(request):
return HttpResponse('Hat geklappt')
urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='home'),
path('success', views.success, name='success'),
]
What am I missing? What more can I do to check what’s wrong?
Thanks.
Suhel