简体   繁体   English

Python django 'QueryDict' object 没有属性 'subject'

[英]Python django 'QueryDict' object has no attribute 'subject'

how do I write here because I do not know how to solve my problem, it turns out that I am creating an online test portal and I need to know that I am failing, I explain the context at the time of creating a question an evaluation index should be indicated, and this has a filter that agrees the subject and course of the test in question.我怎么写在这里,因为我不知道如何解决我的问题,事实证明我正在创建一个在线测试门户,我需要知道我失败了,我在创建问题时解释上下文评估索引应该被指出,并且它有一个过滤器,它同意所讨论的测试的主题和过程。 Well the filter works shows me the indicators that are of a specific course and subject, but only when I enter a manual id instead when I pass the quiz variables it throws the error: 'QueryDict' object has no attribute 'subject 'I leave the form code and the view so they can tell me that I am failing please.过滤器的工作原理是向我显示特定课程和主题的指标,但只有当我输入手动 ID 而不是当我传递测验变量时它才会抛出错误:'QueryDict' object 没有属性'主题'我离开了表单代码和视图,以便他们可以告诉我我失败了。

forms.py forms.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)

As you can see in the filter I did the planning__course__id = 1 has an id set manually and the other has as I want, I must say that the filter works what fails is that at the time of wanting to save throws the error.正如您在过滤器中看到的那样,我在 Planning__course__id = 1 中手动设置了一个 id,而另一个设置了我想要的,我必须说过滤器工作失败是在想要保存时抛出错误。

view.py视图.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})

this is the error这是错误

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' object has no attribute 'subject' PlanificacionIndicador.objects.filter(planificacion__asignatura__id=quiz.subject.id, planificacion__curso__id=1) AttributeError: 'QueryDict' object 没有属性 'subject'

You forgot to put in the quiz arg to QuestionForm.__init__ in the POST case.POST案例中,您忘记将quiz arg 放入QuestionForm.__init__中。 That line should read那行应该是

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM