Аргумент TypeError в / Students / exam / 1 / int () должен быть строкой, байтовым объектом или числом, а не списком. - PullRequest
0 голосов
/ 19 марта 2019

Я пытаюсь отправить эту форму через эту модель и представления, но я получаю TypeError, которая показана на скриншоте.Хотя я хочу также представить его в базу данных.Я взял все свое значение только для того, чтобы поместить его в базу данных.

enter image description here

Я был бы очень рад, если бы вы помогли решить save() метод также.

student.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:

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:

<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>

1 Ответ

0 голосов
/ 20 марта 2019

Я вижу, что единственный вызов int () находится в

int(y) 

Попробуйте это print(type(y)).Может быть, у является списком объектов.Вот почему ошибка говорит:

Аргумент должен быть строкой или ... не списком

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...