所以我在django中创建了一个自动生成学校时间表的应用程序。
我试图在一个页面上列出一个列表,用户可以选择不同年份的课程(例如,如果有人想检查第四年组),现在我面临着反向链接的问题。
下面是代码:views.py
#adding students
def addStudent(request):
if request.method == "POST":
year = request.POST["year"]
faculty = request.POST["faculty"]
number = request.POST["group_number"]
f = Students(group_year = year, group_title = faculty, group_number = number)
f.save()
return render(request, "app/AddingForms/addStudents.html")
#viewing
def viewStudents(request, year_id):
arr = Students.objects.filter(group_year = year_id)
context = {"year_id":year_id, 'students':arr}
return render(request, "app/AddingForms/Studentslist.html", context)
models.py
class Students(models.Model):
group_year = models.IntegerField()
group_title = models.CharField(max_length= 10)
group_number = models.IntegerField()
lessons = models.ManyToManyField(Lesson)
def __str__(self):
return self.group_title
urls.py
:
#FOR CORE APP
app_name = "scheduleapp"
urlpatterns = [
path("", views.index, name = 'index'),
path("profile/", views.index, name = "profile"),
path("about/", views.about, name = "about"),
path("add/", views.addingMenu, name = "adding"),
path("add-student/", views.addStudent, name = "addStudent"),
path("add-lesson/", views.addLesson, name = "addLesson"),
path("add-teacher/", views.addTeacher, name = "addTeacher"),
path("add-student/<int:year_id>", views.viewStudents, name = "viewStudents")
]
html link
<ul class="list-group">
<li class="list-group-item active" aria-current="true">Currently Active Groups:</li>
<a href = "{% url 'scheduleapp:viewStudents' year_id %}"><li class="list-group-item">1st Year</li></a>
</ul>
我应该怎么做才能修复这个错误?
1条答案
按热度按时间omhiaaxx1#
这里
year_id
的值是空的。你需要正确地将它添加到当前可见的视图的上下文中。