自定义Django管理员

时间:2018-12-08 16:50:44

标签: python django django-admin

这是我的测验模型,

class Quiz(Base):
    name = models.CharField(max_length=512, null=True, blank=True)
    genre = models.ForeignKey(Genre, on_delete=models.CASCADE, default="", )

这是一个问题,

class Question(Base):
    text = models.TextField()
    quiz = models.ForeignKey(Quiz, on_delete=models.CASCADE, null=True)
    image = models.ImageField(upload_to=question_image_path, null=True)

这是一个答案,

class Answer(Base):
    text = models.TextField(null=True)
    question = models.ForeignKey(Question, on_delete=models.CASCADE, null=True, blank=True)
    correct = models.BooleanField(default=False)

我想在一页中显示整个测验创建表单,而不是常规的django管理员显示。我该如何自定义django管理员来做到这一点。

1 个答案:

答案 0 :(得分:1)

首先打开您的urls.py文件,然后根据需要编写路径:

from django.urls import path
from . import views
path('', views.quiz, name = 'quiz' ),

打开您的views.py文件并编写有关测验的功能:

from django.shortcuts import render
from .models import Quiz

def quiz(request):
    allQuiz = Quiz.objects.all()
    context = {
        'quizes': allQuiz
    }
    return render(request, '<appNameInsideYourTemplatesDirectry>/quiz.html', context )

现在,以以下方式在模板中创建quiz.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<ul>
{% for quiz in quizes %}
    <li>{{quiz}}</li>
{% endfor %}    
</ul>

</body>
</html>