gpt4 book ai didi

python - 执行 form.is_valid() 时,“WSGIRequest”对象没有属性 'get'

转载 作者:太空宇宙 更新时间:2023-11-04 07:15:23 25 4
gpt4 key购买 nike

我正在使用 Django 2.0.1 和 Python 3.5.2

我在执行 form.is_valid() 时遇到此错误

AttributeError: 'WSGIRequest' 对象没有属性 'get'

View .py:

@method_decorator(login_required, name='dispatch')
class CourseSelectionView(View):

def get(self, request):
form = CourseSelectionForm(request)
selected_course = SelectedCourse.objects.filter(user=request.user)
return render(request, 'profile/CourseSelection.html', context={
'form': form,
'selected_course': selected_course,
})

def post(self, request):
form = CourseSelectionForm(request)
if form.is_valid():
if not request.user.profile.can_select_this(int(self.request.POST.get('course_id'))):
form.errors['course_id'] = "شما قادر به اضافه کردن درس دیگری نیستید."
return render(request, 'profile/CourseSelection.html', context={
'form': form
})
request.user.profile.remaining_units = \
request.user.profile.remaining_units - int(self.request.POST.get('course_id'))
return redirect(reverse_lazy('course_selection'))

表格.py: 类 CourseSelectionForm(ModelForm):

    course_id = forms.IntegerField(
max_value=10000000
)

class Meta:
model = SelectedCourse
fields = ('course_id',)

def clean_course_id(self):
course_id = self.cleaned_data.get('course_id')
try:
int(str(course_id))
except ValueError:
raise ValidationError("Please enter a valid course")
return course_id

回溯:

    Internal Server Error: /profile/course_selection
Traceback (most recent call last):
File "/home/heh/Projects/PycharmProjects/acsrv2/venv/lib/python3.5/site-packages/django/core/handlers/exception.py", line 35, in inner
response = get_response(request)
File "/home/heh/Projects/PycharmProjects/acsrv2/venv/lib/python3.5/site-packages/django/core/handlers/base.py", line 128, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/heh/Projects/PycharmProjects/acsrv2/venv/lib/python3.5/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/heh/Projects/PycharmProjects/acsrv2/venv/lib/python3.5/site-packages/django/views/generic/base.py", line 69, in view
return self.dispatch(request, *args, **kwargs)
File "/home/heh/Projects/PycharmProjects/acsrv2/venv/lib/python3.5/site-packages/django/utils/decorators.py", line 62, in _wrapper
return bound_func(*args, **kwargs)
File "/home/heh/Projects/PycharmProjects/acsrv2/venv/lib/python3.5/site-packages/django/contrib/auth/decorators.py", line 21, in _wrapped_view
return view_func(request, *args, **kwargs)
File "/home/heh/Projects/PycharmProjects/acsrv2/venv/lib/python3.5/site-packages/django/utils/decorators.py", line 58, in bound_func
return func.__get__(self, type(self))(*args2, **kwargs2)
File "/home/heh/Projects/PycharmProjects/acsrv2/venv/lib/python3.5/site-packages/django/views/generic/base.py", line 89, in dispatch
return handler(request, *args, **kwargs)
File "/home/heh/Projects/PycharmProjects/acsrv2/acsuser/views.py", line 97, in post
if form.is_valid():
File "/home/heh/Projects/PycharmProjects/acsrv2/venv/lib/python3.5/site-packages/django/forms/forms.py", line 179, in is_valid
return self.is_bound and not self.errors
File "/home/heh/Projects/PycharmProjects/acsrv2/venv/lib/python3.5/site-packages/django/forms/forms.py", line 174, in errors
self.full_clean()
File "/home/heh/Projects/PycharmProjects/acsrv2/venv/lib/python3.5/site-packages/django/forms/forms.py", line 376, in full_clean
self._clean_fields()
File "/home/heh/Projects/PycharmProjects/acsrv2/venv/lib/python3.5/site-packages/django/forms/forms.py", line 388, in _clean_fields
value = field.widget.value_from_datadict(self.data, self.files, self.add_prefix(name))
File "/home/heh/Projects/PycharmProjects/acsrv2/venv/lib/python3.5/site-packages/django/forms/widgets.py", line 254, in value_from_datadict
return data.get(name)
AttributeError: 'WSGIRequest' object has no attribute 'get'

有什么建议吗?

最佳答案

我认为你需要做

form = CourseSelectionForm(request.POST)

关于python - 执行 form.is_valid() 时,“WSGIRequest”对象没有属性 'get',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48742171/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com