在 django 管理面板中添加学生数据时出现 NoReverseMatch 错误

问题描述 投票:0回答:1

我是一名初学者,正在尝试创建一个基本的大学门户网站。

在 django-admin 面板中添加学生数据时,我在 /admin/api/student/add/ 处收到 NoReverseMatch 错误。

url.py

from django.contrib import admin
from django.urls import path,include

urlpatterns = [
    path('admin/',admin.site.urls),
    path('api/',include('api.urls')),
]

管理员.py

from django.contrib import admin
from .models import Student, Instructor, Course, Enrollment

@admin.register(Student)
class StudentAdmin(admin.ModelAdmin):
    list_display = ['std_id', 'firstname', 'lastname', 'date_of_birth', 'cgpa']

@admin.register(Course)
class CourseAdmin(admin.ModelAdmin):
    list_display = ['course_id', 'coursename', 'cred_hrs']

@admin.register(Instructor)
class InstructorAdmin(admin.ModelAdmin):
    list_display = ['instructor_id', 'instructor_name']

@admin.register(Enrollment)
class EnrollmentAdmin(admin.ModelAdmin):
    list_display = ['enrollment_id', 'student', 'course', 'instructor']

模型.py

from django.db import models
from django.core.validators import MinValueValidator, MaxValueValidator

# Create your models here.
class Student(models.Model):
    std_id =  models.CharField(max_length=6,primary_key = True, unique=True , editable=False)
    firstname = models.CharField(max_length=30)
    lastname = models.CharField(max_length=30)
    date_of_birth = models.DateField()
    cgpa = models.FloatField(
        validators=[MinValueValidator(0), MaxValueValidator(4)]
    )

class Course(models.Model):

    CREDIT_HOURS_CHOICES = [
        (1, '1 credit hour'),
        (2, '2 credit hours'),
        (3, '3 credit hours'),
    ]
    course_id = models.CharField(max_length=6,primary_key = True, unique=True , editable=False)
    coursename = models.CharField(max_length=100)
    cred_hrs = models.IntegerField(choices=CREDIT_HOURS_CHOICES)

class Instructor(models.Model):
    instructor_id = models.CharField(max_length=6,primary_key = True, unique=True , editable=False)
    instructor_name = models.CharField(max_length=100)

class Enrollment(models.Model):
    enrollment_id = models.AutoField(primary_key=True)
    student = models.ForeignKey(Student, on_delete=models.CASCADE)
    course = models.ForeignKey(Course, on_delete=models.CASCADE)
    instructor = models.ForeignKey(Instructor,on_delete = models.CASCADE)

我对这类东西很陌生,所以是的...

我试图查找我提供的代码中是否有任何错误,但一切看起来都很好...... 使用过 chatgpt 但没有运气

python python-3.x django django-rest-framework
1个回答
0
投票

有很多原因,你没有提供实际的错误消息或跟踪

也许您忘记将

django.contrib.admin
添加到设置中的
INSTALLED_APPS

© www.soinside.com 2019 - 2024. All rights reserved.