Search code examples
pythondjangodjango-urls

How do i fix Reverse for 'viewStudents' with arguments '('',)' not found. 1 pattern(s) tried: ['schedule/(?P<year_id>[0-9]+)\\Z']?


So I am creating auto-generating school schedule app in django.

I try to make a list on a page where user can choose different years of classes (For example if someone want to check groups of 4th year) and now i am facing reverse link problem.

Here is the code:

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>

What should I do in order to fix this bug?


Solution

  • Here your value of year_id is empty. You need to properly add it to context of the view that is currently visible.

    <a href = "{% url 'scheduleapp:viewStudents' year_id %}">