Melissa Stewart
Melissa Stewart

Reputation: 3605

Customizing django admin

This is my Quiz model,

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

This is a Question,

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)

And this is an answer,

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)

Instead of the regular django admin display I want to display the entire quiz creation form in one page. How can I customize django admin to do this.

Upvotes: 0

Views: 66

Answers (1)

Usama Nadeem
Usama Nadeem

Reputation: 128

Firstly open your urls.py file and write the path as or as you desire:

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

Open your views.py file and write the function about your quiz:

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 )

Now, create the quiz.html insite your templates as:

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

Upvotes: 1

Related Questions