每次我成功执行发布请求后,都会收到此消息。我知道它与实现重定向功能有关,因此您不会收到重复的帖子,但是我无法使其正常工作。这是我的代码。感谢您的帮助。
@login_required
def K8_Points_Test(request):
if request.method == 'POST':
form = K8PointsTestForm(request.POST)
if form.is_valid():
form.save(commit=False)
class_name = form.cleaned_data.get('class_name')
getstudents = Student.objects.filter(class_name = class_name)
students = getstudents.all()
context = {'form': form ,'students' : students , 'class_name': class_name}
return render(request, 'points/k8_points_test.html', {'form': form} )
else:
return HttpResponseBadRequest
else:
form = K8PointsTestForm()
return render(request, 'points/k8_points_test.html', {'form': form} )
追踪
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/points/k8_points_test
Django Version: 2.2.6
Python Version: 3.7.3
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'forms.apps.FormsConfig',
'crispy_forms',
'users.apps.UsersConfig',
'points.apps.PointsConfig']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "C:\Users\rzuchowski\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\exception.py" in inner
34. response = get_response(request)
File "C:\Users\rzuchowski\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\utils\deprecation.py" in __call__
96. response = self.process_response(request, response)
File "C:\Users\rzuchowski\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\middleware\clickjacking.py" in process_response
26. if response.get('X-Frame-Options') is not None:
Exception Type: TypeError at /points/k8_points_test
Exception Value: get() missing 1 required positional argument: 'header'
答案 0 :(得分:2)
此行:
return HttpResponseBadRequest
导致错误。您需要返回响应类的实例,而您要返回该类本身。
这应该解决它:
return HttpResponseBadRequest()
# OR you can also pass a custom message
return HttpResponseBadRequest("Bad Request")