7๐
โ
Add csrf_exempt to your views.py after importing it like this:
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse
from .models import Text, TextForm
from django.views.decorators.csrf import csrf_exempt,csrf_protect #Add this
.
.
.
@csrf_exempt #This skips csrf validation. Use csrf_protect to have validation
def text_new(request):
if request.method == 'POST':
form = TextForm(request.POST)
if form.is_valid():
return HttpResponse('Test')
else:
form = TextForm()
return render(request, 'projectname/new.html', {'form': form})
๐คcold_coder
0๐
In imports add
from django.shortcuts import redirect
Replace
return HttpResponse('Test')
with
return redirect('/') # or some other url of your URLconf
Also, add 'django.middleware.csrf.CsrfViewMiddleware'
to MIDDLEWARE_CLASSES
in settings.py
.
๐คf43d65
- [Django]-How can I disable a third party API when executing Django unit tests?
- [Django]-How to return most popular items in a table, but where each item is unique?
Source:stackexchange.com