I am trying to set up a school journal based on Django and Python, but I can’t do it because of a 404 page with a problem in the title.then Python part is backend and Node + Yarn + HTML is the consumer (client) part.
my views.py
from itertools import chain
from rest_framework import viewsets, mixins
from rest_framework.authentication import TokenAuthentication
from rest_framework.permissions import IsAuthenticated
from core.models import (
StudyYear,
Subject,
Mark,
StudentClass,
Student,
School,
)
from journal import serializers
class BaseJournalAttrViewSet(viewsets.GenericViewSet,
mixins.ListModelMixin,
mixins.RetrieveModelMixin,):
"""Base viewset for journal attributes"""
authentication_classes = (TokenAuthentication,)
permission_classes = (IsAuthenticated,)
class StudyYearViewSet(BaseJournalAttrViewSet):
"""Manage study year in the database"""
queryset = StudyYear.objects.all()
serializer_class = serializers.StudyYearSerializer
class SubjectViewSet(BaseJournalAttrViewSet):
"""Manage subjects in the database"""
queryset = Subject.objects.all()
serializer_class = serializers.SubjectSerializer
class MarkViewSet(BaseJournalAttrViewSet,
mixins.CreateModelMixin,
mixins.UpdateModelMixin):
"""Manage marks in the database"""
queryset = Mark.objects.all()
serializer_class = serializers.MarkSerializer
def get_queryset(self):
"""Retrieve all marks by student and subject"""
queryset = self.queryset
student = self.request.query_params.get('student')
subject = self.request.query_params.get('subject')
if student:
queryset = queryset.filter(student__id=student)
if subject:
queryset = queryset.filter(subject__id=subject)
return queryset
class StudentClassViewSet(BaseJournalAttrViewSet):
"""Manage classes in the database"""
queryset = StudentClass.objects.all()
serializer_class = serializers.StudentClassSerializer
class StudentViewSet(BaseJournalAttrViewSet):
"""Manage students in the database"""
queryset = Student.objects.all()
serializer_class = serializers.StudentSerializer
def get_queryset(self):
"""Retrieve students for authenticated user by class"""
student_class = self.request.query_params.get('student_class', None)
queryset = self.queryset
if student_class:
queryset = queryset.filter(student_class__name=student_class)
return queryset
class JournalAPIView(viewsets.ModelViewSet):
"""Combine data from other serializer to form journal"""
authentication_classes = (TokenAuthentication,)
permission_classes = (IsAuthenticated,)
serializer_class = serializers.MarkSerializer
queryset = Mark.objects.all()
def get_queryset(self):
"""Retrieve journal data from multiple serializers"""
student_class = self.request.query_params.get('student_class', None)
subject = self.request.query_params.get('subject', None)
students_queryset = Student.objects.filter(
student_class__name=student_class)
mark_querysets = []
for student in students_queryset:
mark_querysets.append(student.marks.filter(subject__name=subject))
queryset = list(chain(*mark_querysets))
return queryset
class SchoolViewSet(BaseJournalAttrViewSet):
"""Manage school in the database"""
queryset = School.objects.all()
serializer_class = serializers.SchoolSerializer
urls.py
from django.urls import path, include
from rest_framework.routers import DefaultRouter
from journal import views
router = DefaultRouter()
router.register('study_years', views.StudyYearViewSet, basename='studyyrs')
router.register('subjects', views.SubjectViewSet, basename='subj')
router.register('marks', views.MarkViewSet, basename='markpage')
router.register('student_classes', views.StudentClassViewSet, basename='studcl')
router.register('students', views.StudentViewSet, basename='studentsview')
router.register('journals', views.JournalAPIView, basename='journaling')
router.register('school', views.SchoolViewSet, basename='allschool')
app_name = 'journal'
urlpatterns = [
path('', include(router.urls)),
]
Code link:
https://drive.google.com/file/d/1D_gP1GpL8MOJhLwWuw7UgQ_-ATHgLk_q/view?usp=sharing
$ python3 manage.py runserver
(runs successfully)
and then
Not Found: /
[20/Aug/2024 11:00:07] "GET / HTTP/1.1" 404 2771
Client part:
$ yarn start
(success)
(And a blank React page.)
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/
Using the URLconf defined in app.urls, Django tried these URL patterns, in this order:
admin/
api/user/
api/event/
api/timetable/
api/journal/
The empty path didn’t match any of these.
2
Answers
if you check your urlpatterns, you can see:
it means, on any empty path will be searched list in router.urls
But in router.urls you never register view for empty path.
Probably you mean:
More here:
about url dispatcher: https://docs.djangoproject.com/en/5.1/topics/http/urls/ , there you can see examples with empty url.
about redirect: https://docs.djangoproject.com/en/5.1/ref/class-based-views/base/#redirectview
In DRF documentation:
As you can see, you should use router.urls instead of urlspatterns.
So here is the solution:
in journal app urls.py
and then in your app/urls.py