【问题标题】:how can i create Quiz app using django framework?如何使用 django 框架创建测验应用程序?
【发布时间】:2020-08-29 18:50:26
【问题描述】:

我正在尝试制作一个随机询问 5 个问题的测验应用程序。在此之前,我在模型中使用了外键。我浏览了一个 django 文档。现在我想在同一页面上呈现所有问题及其 4 个选项。而通过文档,它为每个问题创建一个链接,该链接仅使用其 ID 呈现在一个问题上。现在,如果我在同一页面上呈现,它不会为每个问题分开无线电按钮。参考链接:https://docs.djangoproject.com/en/3.0/intro/tutorial01/ 我的代码如下:

models.py

class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

    def __str__(self):
        return self.question_text

    def was_published_recently(self):
        return self.pub_date >= timezone.now() - datetime.timedelta(days=1)


class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)

    def __str__(self):
        return self.choice_text

views.py

def index(request):
    latest_question_list = Question.objects.all()
    context = {'latest_question_list': latest_question_list}
    return render(request, 'index.html', context)

def detail(request, question_id):
    question = get_object_or_404(Question, pk=question_id)
    return render(request, 'detail.html', {'question': question})

def results(request, question_id):
    question = get_object_or_404(Question, pk=question_id)
    return render(request, 'results.html', {'question': question})


def vote(request, question_id):
    question = get_object_or_404(Question, pk=question_id)
    try:
        selected_choice = question.choice_set.get(pk=request.POST['choice'])
    except (KeyError, Choice.DoesNotExist):
        # Redisplay the question voting form.
        return render(request, 'detail.html', {
            'question': question,
            'error_message': "You didn't select a choice.",
        })
    else:
        selected_choice.votes += 1
        selected_choice.save()
        # Always return an HttpResponseRedirect after successfully dealing
        # with POST data. This prevents data from being posted twice if a
        # user hits the Back button.
        return HttpResponseRedirect(reverse('app1:results', args=(question.id,)))

index.html在这里创建问题链接

{% if latest_question_list %}
    <ul>
    {% for question in latest_question_list %}

        <li><a href="{% url 'app1:detail' question.id %}">{{ question.question_text }}</a></li>

    {% endfor %}

    </ul>
{% else %}
    <p>No polls are available.</p>
{% endif %}

detail.html 点击问题后,它会出现在这个页面上,其中显示了这个带有 ans 的signle quetion

<h1>{{ question.question_text }}</h1>
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}

<form action="{% url 'app1:vote' question.id %}" method="post">
{% csrf_token %}
{% for choice in question.choice_set.all %}
    <input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}">
    <label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br>
{% endfor %}
<input type="submit" value="Vote">
</form>

【问题讨论】:

  • 您可以将所有单选放在同一个表单中,但只放一个单选按钮将名称更改为选项 1 选择 2 等等,然后检查服务器中所有问题的答案
  • 我是个大人物..你能帮我看看我该怎么做

标签: python html django web foreign-keys


【解决方案1】:

当您按下提交时,它会将所有输入数据作为参数发送,因此如果您将所有输入放在一个表单中,它将发送所有输入。并且您可以通过名称来计算数据引用,您可以调用第一个输入 1 和第二个 2 依此类推,在服务器端您应该检查所有输入,如果答案正常,您也可以使用 js 发送你去了什么

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-20
    • 2021-06-25
    相关资源
    最近更新 更多