在Django中尝试构建测验格式时,问题正确列出,但无法列出选项

2024-03-29 09:21:16 发布

您现在位置:Python中文网/ 问答频道 /正文

嗨,我对django很陌生,python试图构建一些测试模式 我无法将选项映射到问题并列出它们。你知道吗

型号.py

    from __future__ import unicode_literals

    from django.db import models

    # Create your models here.

    class Date_list(models.Model):
        date_gk = models.CharField(max_length=20)
        CA_BGimage = models.CharField(max_length=20)


        def __str__(self):
         return 'Daily Current Affair and GK quiz: ' + self.date_gk


    class Questions(models.Model):
        date_list = models.ForeignKey(Date_list, on_delete=models.CASCADE, default=None)
        question = models.CharField(max_length=500)

        def __str__(self):
         return self.question

    class Options(models.Model):
        questions = models.ForeignKey(Questions, on_delete=models.CASCADE, default=None)
        option = models.CharField(max_length=50)
        correct_answer =models.CharField(max_length=100)


        def __str__(self):
         return self.option

视图.py

        from django.shortcuts import render
        from django.http import HttpResponse
        from .models import Date_list, Questions
        from django.template import loader
        from django.shortcuts import render, get_object_or_404
        # Create your views here.


        def index(request):
            all_dates = Date_list.objects.all()
            context = {'all_dates': all_dates,}
            return render(request, 'Current_Affair/index.html', context)

        def detail(request, date_list_id, questions_id):
            date_list = get_object_or_404(Date_list, pk=date_list_id)
            questions = get_object_or_404(Questions, pk=questions_id)
            return render(request, 'Current_Affair/detail.html', {'date_list':date_list}, {'questions':questions})

网址.py

    from django.conf.urls import url
    from . import views

    app_name = 'Current_Affair'

    urlpatterns = [
            #Current_Affair
            url(r'^$', views.index, name='index'),


            #/Current_Affair/32
        url(r'^(?P<date_list_id>[0-9]+)/$', views.detail, name='detail'),
    ]

索引.html

<!-- Loads the path to your static files -->
{% load staticfiles %}


<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"/>
<link rel="stylesheet" type="text/css" href="{% static 'Current_Affair/style.css' %}" />




<h1> <strong> Daily Current Affairs & GK Quiz </strong></h1>
<ul>
    {% for datess in all_dates %}
    <li><a href="/Current_Affair/{{ datess.id }}">{{ datess.date_gk }}</a></li>
    {% endfor %}
</ul>

你知道吗详细信息.html你知道吗

<h1> QUIZ </h1>
{% for q in date_list.questions_set.all %}
    <div> {{ q.question }} </div>
    {% for o in questions.options_set.all %}
          {{ o.option }}
    {% endfor %}
{% endfor %}


style.css

body {
    background-image: url("images/b2.jpeg");
}

我在犯错误。 有人能帮忙吗…谢谢

TypeError at /Current_Affair/1/ detail() takes exactly 3 arguments (2 given) Request Method: GET Request URL: http://127.0.0.1:8000/Current_Affair/1/ Django Version: 1.9 Exception Type: TypeError Exception Value: detail() takes exactly 3 arguments (2 given) Exception Location: C:\Python27\lib\site-packages\django-1.9-py2.7.egg\django\core\handlers\base.py in get_response, line 147 Python Executable: C:\Python27\python.exe Python Version: 2.7.13 Python Path: ['C:\Users\vijay\Desktop\Knowledge_Basket', 'C:\Windows\SYSTEM32\python27.zip', 'C:\Python27\DLLs', 'C:\Python27\lib', 'C:\Python27\lib\plat-win', 'C:\Python27\lib\lib-tk', 'C:\Python27', 'C:\Python27\lib\site-packages', 'C:\Python27\lib\site-packages\django-1.9-py2.7.egg'] Server time: Mon, 28 Aug 2017 20:51:54 +0530


Tags: djangofromimportselfiddatemodelslib
1条回答
网友
1楼 · 发布于 2024-03-29 09:21:16

在您的url中:

 url(r'^(?P<date_list_id>[0-9]+)/$', views.detail, name='detail')

您只定义了一个参数date_list_id

你得在你的字典里加上questions_idurl.py=>

 url(r'^(?P<date_list_id>[0-9]+)/(?P<questions_id>[0-9]+)$', views.detail, name='detail')

在模板中=>

  <li><a href="/Current_Affair/{{ datess.id }}/{{ question.id }}">{{ datess.date_gk }}</a></li>

相关问题 更多 >