Hi there
I am working on a project where I have pretty much set up the website using HTML, CSS and Django as a backend. I am trying to navigate from one link to another using dynamic routing in Django. I can dynamically move between different sections of the first layer but when I try to navigate to the second layer it won’t work. Let me give details below for better understanding of my issue.
Navigatation bar [ Home Aboutus Quiz Login ]
If quiz button is pressed the first layer of navigation buttons appear as follows:
[Number Algebra Geometry etc.... ]
I can retrieve all sub topics under the above topics on the left pane of the page and leave the right pane until one of the options below is selected.
e.g. Number 1
Number 2
Number 3
etc..
My issue now is when Number 1 is pressed, for example, I want to display its contents on the right pane.
This is how I am trying to achieve it:
========================================================================
view.py
from django.shortcuts import render
from django.http import HttpResponse
from .models import Sub_Topic, Topic, Quiz
# Create your views here.
def index(request):
return render(request, 'quiz_app/index.html')
def aboutus(request):
#return HttpResponse("About us under construction")
return render(request, 'quiz_app/aboutus.html')
def quiz_sub_topics(request,pk):
sub_topics = Sub_Topic.objects.filter(topic_id=pk)
context = {
'sub_top':sub_topics,
}
return render(request, 'quiz_app/quiz.html', context)
def quiz_sub_topic_questions(request,slug):
sub_topic_questions = Quiz.objects.filter(sub_topic_name=str(slug))
context = {
'sub_top_que':sub_topic_questions,
}
return render(request, 'quiz_app/quiz.html', context)
def login(request):
return render(request, 'login.html')
=================================================
urls.py (under my_app)
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name = 'index'),
path('aboutus/', views.aboutus, name='aboutus'),
path('quiz/<int:pk>/', views.quiz_sub_topics, name='quiz_sub_topics'),
path('quiz/<int:pk>/<slug:slug>/',views.quiz_sub_topic_questions, name='quiz_sub_topic_questions'),
]
=========================================================
models.py
from django.db import models
import json
# Create your models here.
class Topic(models.Model):
topic_id = models.BigAutoField(primary_key=True)
topic_name = models.CharField(max_length=100)
def __str__(self):
return str(self.topic_name)
class Sub_Topic(models.Model):
sub_topic_id = models.BigAutoField(primary_key=True)
sub_topic_name = models.CharField(max_length=100)
topic = models.ForeignKey(Topic , on_delete = models.CASCADE)
def __str__(self):
return str(self.sub_topic_name)
class Quiz(models.Model):
quiz_id = models.BigAutoField(primary_key=True)
question = models.CharField(max_length=200)
options = models.CharField(max_length=200)
answer = models.CharField(max_length = 20)
sub_topic_name = models.ForeignKey(Sub_Topic, on_delete = models.CASCADE)
def __str__(self):
return str(self.question)
class Student(models.Model):
student_id = models.BigAutoField(primary_key=True)
email = models.CharField(max_length=100)
password = models.CharField(max_length=100)
quiz = models.ManyToManyField(Quiz)
class Result(models.Model):
result_id = models.BigAutoField(primary_key=True)
percentage = models.DecimalField(max_digits=10, decimal_places=2)
result_date = models.DateTimeField(max_length=50)
student = models.ForeignKey(Student, on_delete = models.CASCADE)
=============================================================
admin.py
from django.contrib import admin
from .models import Quiz, Topic, Sub_Topic, Student, Result
# Register your models here.
admin.site.register(Quiz)
admin.site.register(Topic)
admin.site.register(Sub_Topic)
admin.site.register(Result)
admin.site.register(Student)
===================
I have tried everything to no avail. Please I need your help. What I am doing wrong?
Thanks.