gpt4 book ai didi

Python django 'QueryDict' 对象没有属性 'subject'

转载 作者:太空宇宙 更新时间:2023-11-03 20:06:29 24 4
gpt4 key购买 nike

我在这里该怎么写,因为我不知道如何解决我的问题,事实证明我正在创建一个在线测试门户,我需要知道我失败了,我解释了创建时的上下文问题应注明评估指标,并且该指标具有与所讨论的测试主题和类(class)一致的过滤器。好吧,过滤器可以向我显示特定类(class)和主题的指标,但只有当我输入手动 ID 时,而不是传递测验变量时,它才会抛出错误:“QueryDict”对象没有属性“主题”,我留下了形成代码和 View ,以便他们可以告诉我我失败了。

表单.py

class QuestionForm(forms.ModelForm):

class Meta:
model = Question
context_object_name = 'questions'
fields = ('number','planificacion','text','description', 'puntaje' ,'image','document' )
label = 'Pregunta'



def __init__(self ,quiz ,*args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['planificacion'].queryset = PlanificacionIndicador.objects.filter(planificacion__asignatura__id=quiz.subject.id, planificacion__curso__id=1)

正如你在过滤器中看到的,我做了 Planning__course__id = 1 有一个手动设置的 id,另一个有我想要的,我必须说过滤器有效,失败的是在想要保存时抛出错误.

查看.py

@login_required
@teacher_required
def question_add(request, pk):

quiz = get_object_or_404(Quiz, pk=pk, owner=request.user)

if request.method == 'POST':
form = QuestionForm(request.POST, request.FILES)


if form.is_valid():
question = form.save(commit=False)
question.quiz = quiz
question.save()
messages.success(request, 'Ahora puede agregar respuestas / opciones a la pregunta.')
return redirect('teachers:question_change', quiz.pk, question.pk)
else:
form = QuestionForm(quiz=quiz)
return render(request, 'classroom/teachers/question_add_form.html', {'quiz': quiz,'form': form})

这是错误

Internal Server Error: /teachers/quiz/79/question/add/
Traceback (most recent call last):
File "C:\Users\Oficina\AppData\Local\Programs\Python\Python37-32\lib\site-
packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Users\Oficina\AppData\Local\Programs\Python\Python37-32\lib\site-
packages\django\core\handlers\base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\Oficina\AppData\Local\Programs\Python\Python37-32\lib\site-
packages\django\core\handlers\base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Oficina\AppData\Local\Programs\Python\Python37-32\lib\site-
packages\django\contrib\auth\decorators.py", line 21, in _wrapped_view
return view_func(request, *args, **kwargs)
File "C:\Users\Oficina\AppData\Local\Programs\Python\Python37-32\lib\site-
packages\django\contrib\auth\decorators.py", line 21, in _wrapped_view
return view_func(request, *args, **kwargs)
File "C:\Proyectos\proyecto-educa-quiz\classroom\views\teachers.py", line
535, in question_add
form = QuestionForm(request.POST, request.FILES)
File "C:\Proyectos\proyecto-educa-quiz\classroom\forms.py", line 409, in
__init__self.fields['planificacion'].queryset =

PlanificacionIndicador.objects.filter(planificacion__asignatura__id=quiz.subject .id,planificacion__curso__id=1) AttributeError:“QueryDict”对象没有属性“subject”

最佳答案

您忘记将 quiz 参数放入 POST 案例中的 QuestionForm.__init__ 中。该行应为

    form = QuestionForm(quiz, request.POST, request.FILES)

关于Python django 'QueryDict' 对象没有属性 'subject',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58941395/

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