Hey
I get this error, when i use a post method for register page.
The Error:
Forbidden (403)
CSRF verification failed. Request canceled.
Help
Reason given for failure:
Origin checking failed - https://subdomain.domain.com does not match any trusted origins.
In general, this can occur when there is a genuine Cross Site Request Forgery, or when Django's CSRF mechanism has not been used correctly. For POST forms, you need to ensure:
Your browser is accepting cookies.
The view function passes a request to the template’s render method.
In the template, there is a {% csrf_token%} template tag inside each POST form that targets an internal URL.
If you are not using CsrfViewMiddleware, then you must use csrf_protect on any views that use the csrf_token template tag, as well as those that accept the POST data.
The form has a valid CSRF token. After logging in in another browser tab or hitting the back button after a login, you may need to reload the page with the form, because the token is rotated after a login.
You're seeing the help section of this page because you have DEBUG = True in your Django settings file. Change that to False, and only the initial error message will be displayed.
You can customize this page using the CSRF_FAILURE_VIEW setting.
My views.py
def register_view(request):
if request.user.is_authenticated:
return redirect('home:index')
else:
form = CreateUserForm()
if request.method == 'POST':
form = CreateUserForm(request.POST)
if form.is_valid():
form.save()
messages.success(request, 'Account wurde erstellt')
return redirect('home:login')
context = {'form': form}
return render(request, 'home/page1.html', context)
My page 1.html
<form action="" method="POST" class="mbr-form form-with-styler mx-auto" data-form-title="Form Name"><input type="hidden" name="email" data-form-email="true" value="O91j1FC0MzSz5LgxPVjfuihgNRgPzecEgljPmrHNqd2EOosmu7rXdscbzJKvCwwCSp7ClbRQRz1j00dCh1UlW4Oqpx1rjtbvjjLokK+Bu0lokWBbduhLGtzBfsTcsRzH">
{% csrf_token %}
<p class="mbr-text mbr-fonts-style align-center mb-4 display-7">Erstelle dir dein Merkl Konto, um Zugriff zum Portal zu erhalten.<a href="#" class="text-primary"><br></a></p>
<div class="dragArea row">
<div class="col-lg-12 col-md-12 col-sm-12 form-group mb-3" data-for="name">
{% render_field form.username class="form-control" type="username" placeholder="Nutzername" id="phone-form7-b" data-form-field="username"%}
</div>
<div class="col-lg-12 col-md-12 col-sm-12 form-group mb-3" data-for="email">
{% render_field form.email class="form-control" type="email" placeholder="Email" id="phone-form7-b" data-form-field="email"%}
</div>
<div data-for="phone" class="col-lg-12 col-md-12 col-sm-12 form-group mb-3">
{% render_field form.password1 class="form-control" type="password" placeholder="Passwort" id="phone-form7-b" data-form-field="password"%}
</div>
<div data-for="phone" class="col-lg-12 col-md-12 col-sm-12 form-group mb-3">
{% render_field form.password2 class="form-control" type="password" placeholder="Passwort wiederholen" id="phone-form7-b" data-form-field="password"%}
</div>
<div class="col-auto mbr-section-btn align-center"><button type="submit" class="btn btn-success display-4">Registrieren</button></div>
<div class="form-errors">{{form.username.errors}}</div>
<div class="form-errors">{{form.email.errors}}</div>
<div class="form-errors">{{form.password1.errors}}</div>
<div class="form-errors">{{form.password2.errors}}</div>
</div>
</form>
Please help me, thanks!