简体   繁体   English

TypeError at /students/exam/1/ int() 参数必须是字符串、类似字节的对象或数字,而不是“列表”

[英]TypeError at /students/exam/1/ int() argument must be a string, a bytes-like object or a number, not 'list'

I'm trying to submit this form through this model and views, but I am getting the TypeError which is shown in the screenshot.我正在尝试通过此模型和视图提交此表单,但我收到了屏幕截图中显示的 TypeError。 Though I want to also submit it to the database.虽然我也想把它提交到数据库。 I have taking all my value just to drop it into database.我已经把我所有的价值都放到了数据库中。

在此处输入图片说明

I'd be very happy if you'd assist to solve the save() method as well.如果您也能协助解决save()方法,我会很高兴。

student.py:学生.py:

@login_required
@student_required
def take_exam(request, pk):
    course = get_object_or_404(Course, pk=pk)
    student = request.user.student
    question = course.questions.filter()  
    #correct_answers = student.course_answers.filter(answer__question__quiz=course, answer__is_correct=True).count()
    total_questions = course.questions.count()
    choice = Answer.objects.filter()
    marks_obtainable = Details.objects.get(course_id=course)

    if request.method == 'POST':

        question_pk = request.POST.getlist('question_pk')
        question_obj = Question.objects.filter(id=int(question_pk))

        choice_pk = [request.POST['choice_pk{}'.format(q)] for q in question_obj]

        #print(marks_obtainable.marks_obtained)
        zipped = zip(question_obj, choice_pk)

        for x, y in zipped:
            correct_answers = Answer.objects.filter(question_id=x,  is_correct=True).values("id").first()['id']

            print(x, y, correct_answers)
            if int(y) == int(correct_answers):
                #z = TakenQuiz(student=student, course=course, \
                    #question=x, selected_choice=y,  marks_obtained=marks_obtainable, is_correct=True)
                print("correct")
            else:
                print("Not Correct")

    return render(request, 'classroom/students/take_exam_form.html', {
        'course': course,
        'question': question,
        'course': course,
        'total_questions': total_questions,
        'choice': choice,
        'marks_obtainable': marks_obtainable

    })

models.py:模型.py:

class Question(models.Model):

    course = models.ForeignKey(Course, on_delete=models.CASCADE, related_name='questions')
    text = models.CharField('Question', max_length=500)

    def __str__(self):
        return self.text

class Answer(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE, related_name='answers')
    text = models.CharField('Answer', max_length=255)
    is_correct = models.BooleanField('Correct answer', default=False)

    def __str__(self):
        return self.text

take_exam_form.html: take_exam_form.html:

<h2 class="mb-3">{{ course.name }}</h2>
Course id <h2 class="mb-3">{{ course.id }}</h2>
Student id <h2 class="mb-3">{{ request.user.id }}</h2>
Total Question:  <h2 class="mb-3">{{ total_questions }}</h2>
Mark Obtainable <h2 class="mb-3">{{ marks_obtainable.maximum_marks }}</h2>

<form method="post" novalidate>
        {% csrf_token %}
        {% for questions in question %}
        <input type="hidden" name="question_pk" value="{{ questions.pk }}">
        <h3 class="text-info">{{ questions.text|safe }}</h3>
    {% for choices in questions.answers.all %}

        <input class="form-check-input" type="radio" name="choice_pk{{ questions.pk }}" id="choices-{{ forloop.counter }}" value="{{ choices.pk }}">
        <label class="form-check-label" for="choices-{{ forloop.counter }}">
            {{ choices.text|safe }}
        </label>

      {% endfor %}
      {% endfor %}
          <button type="submit" class="btn btn-primary">Submit Now →</button>
</form>

I can see that the only int() call is in我可以看到唯一的 int() 调用在

int(y) 

Try this print(type(y)) .试试这个print(type(y)) Maybe y is a list object.也许 y 是一个列表对象。 That's why the error said:这就是为什么错误说:

The arg must be string or ... not a list arg 必须是字符串或...不是列表

暂无
暂无

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

相关问题 int()参数必须是字符串,类似字节的对象或数字,而不是“列表” - int() argument must be a string, a bytes-like object or a number, not 'list' Python 2-TypeError:int()参数必须是字符串,类似字节的对象或数字,而不是“列表” - Python 2 - TypeError: int() argument must be a string, a bytes-like object or a number, not 'list' 类型错误:int() 参数必须是字符串、类似字节的对象或数字,而不是“列表”? - TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'? Django TypeError:int()参数必须是字符串,类似字节的对象或数字,而不是&#39;list&#39; - Django TypeError: int() argument must be a string, a bytes-like object or a number, not 'list' 类型错误:int() 参数必须是字符串、类似字节的对象或数字,而不是“列表” - TypeError: int() argument must be a string, a bytes-like object or a number, not 'list' TypeError: int() 参数必须是一个字符串,一个类似字节的 object 或一个数字,而不是“NoneType”。 但我写了 int() - TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'. But I wrote the int() 尝试替换列表中的项目时收到“TypeError: int() 参数必须是字符串、类似字节的对象或数字,而不是‘列表’” - Receiving "TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'" when attempting to replace item in a list 如何在python中将图像列表转换为数字列表? TypeError:int()参数必须是字符串,类似字节的对象或数字,而不是“ Image” - How to convert list of images into list of numbers in python? TypeError: int() argument must be a string, a bytes-like object or a number, not 'Image' int() 参数必须是字符串、类似字节的对象或数字,而不是“NoneType” - int() argument must be a string, a bytes-like object or a number, not 'NoneType' int()参数必须是字符串,类似字节的对象或数字,而不是“元组” - int() argument must be a string, a bytes-like object or a number, not 'tuple'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM