I
I
Ilidiance2020-09-10 09:49:46
Django
Ilidiance, 2020-09-10 09:49:46

I can not compose the correct URl and the output of model records on the form. How to fix the code so that groups with links are displayed correctly?

When trying to display a model record with a generating link, it gives an error:

NoReverseMatch at /sampo/group_lists/
Reverse for 'group_lists' with arguments '('1',)' not found. 1 pattern(s) tried: ['sampo/group_lists/']


The same will continue if you display a list of dates and cadets. Help someone fix it.
I attach the code:

Models.py
from django.db import models
from django.contrib.auth.models import User
from django.urls import reverse

# Create your models here.


class CourseCommander(models.Model):

    name = models.CharField(max_length=200, help_text="ФИО начальника курса", verbose_name="Начальник курса")
    course = models.CharField(max_length=10, help_text="Номер курса", verbose_name="Номер курса")

    class Meta:
        ordering = ['course']
        verbose_name = "Начальник курса"
        verbose_name_plural = "Начальники курсов"

    def __str__(self):
        return self.name + ", Курс №" + self.course


class CourseOfficer(models.Model):
    name = models.CharField(max_length=200, help_text="ФИО курсового офицера", verbose_name="Курсовой Офицер")
    course = models.CharField(max_length=10, help_text="Номер курса", verbose_name="Номер курса")

    class Meta:
        ordering = ['course']
        verbose_name = "Курсовой офицер"
        verbose_name_plural = "Курсовые офицеры"

    def __str__(self):
        return self.name + ", Курс №" + self.course


class DutyInstructor(models.Model):
    name = models.CharField(max_length=200, help_text="Дежурный преподаватель")

    class Meta:
        verbose_name = "Дежурный преподаватель"
        verbose_name_plural = "Дежурный преподаватель"

    def __str__(self):
        return self.name


class Group(models.Model):
    number = models.CharField(max_length=10, help_text="Номер группы", verbose_name="Номер Группы")
    courseOfficer = models.ForeignKey(CourseOfficer, on_delete=models.CASCADE, verbose_name="Курсовой Офицер")
    courseCommander = models.ForeignKey(CourseCommander, on_delete=models.CASCADE, verbose_name="Начальник курса")

    class Meta:
        ordering = ['number']
        verbose_name = "Группа"
        verbose_name_plural = "Группы"

    def __str__(self):
        return self.number

    def get_absolute_url(self):
        return reverse('group_lists', args=[self.number])


class Cadet(models.Model):
    rank = models.CharField(max_length=100, help_text="Звание курсанта", verbose_name="Звание")
    name = models.CharField(max_length=200, help_text="ФИО курсанта", verbose_name="Фамилия, Имя, Отчество")
    group = models.ForeignKey(Group, on_delete=models.CASCADE, verbose_name="Номер группы")
    courseOfficer = models.ForeignKey(CourseOfficer, on_delete=models.CASCADE, verbose_name="Курсовой Офицер")
    courseCommander = models.ForeignKey(CourseCommander, on_delete=models.CASCADE, verbose_name="Начальник курса")

    class Meta:
        ordering = ['group']
        verbose_name = "Курсант"
        verbose_name_plural = "Курсанты"

    def __str__(self):
        return self.name + ", Группа №" + str(self.group)


class DateOfSampo(models.Model):
    WAS_POINT = [
        ('AS', "Присутствовал"),
        ('NS', "Отсутствовал"),
    ]
    #
    REASON_OF_OUT = [
        ('NA', "Наряд"),
        ('NR', "Научная работа"),
        ('TR', "Тренировка"),
        ('KM', "Командировка"),
        ('DP', "Другие причины"),
        ('PR', "Присутствовал"),
    ]
    # Не забыть поменять отношения
    date = models.DateField(max_length=20, help_text="Дата самоподготовки", verbose_name="Дата самоподготовки")
    cadet = models.ForeignKey(Cadet, on_delete=models.CASCADE, verbose_name="Курсант")
    point = models.CharField(max_length=100, choices=WAS_POINT, verbose_name="Присутствовал\\Отсутствовал")
    reason = models.CharField(max_length=200, choices=REASON_OF_OUT, verbose_name="Причина пропуска")

    class Meta:
        ordering = ['date']
        verbose_name = "Дата самоподготовки"
        verbose_name_plural = "Даты самоподготовки"

    def get_absolute_url(self):
        return reverse('date_lists', args=[str(self.pk)])

    def __str__(self):
        return str(self.date)


Urls.py
from django.urls import path
from . import views
from django.conf.urls import url


urlpatterns = [
    path('', views.index, name='index'),
    path('about', views.about, name='about'),
    url(r'group_lists/', views.group_lists, name='group_lists'),
    url(r'group_list/?P<number>/', views.date_list, name='date_list'),
   
]


Views.py
from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse
from django.template import Context, loader
from django.conf import settings
from .models import Cadet, DateOfSampo, Group, CourseOfficer, CourseCommander, DutyInstructor
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
from django.views import generic
# Create your views here.


def index(request):
    template = loader.get_template('index.html')
    return HttpResponse(template.render())


def about(request):
    template = loader.get_template('about.html')
    return HttpResponse(template.render())


def group_lists(request):
    groups = Group.objects.all()
    context = {
        'groups': groups,
    }
    template = loader.get_template('group_lists.html')
    return HttpResponse(template.render(context))


def date_list(request, group_number):
    dates = DateOfSampo.objects.get(gr_numb=group_number)
    context = {
        'dates': dates,
    }
    template = loader.get_template('date_lists.html')
    return HttpResponse(template.render(context))


def cadet_list(request):
    pass

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2020-09-10
@soremix

Because when you do , you refer to this pattern
reverse('group_lists', args=[self.number])

url(r'group_lists/', views.group_lists, name='group_lists'),

Which has no arguments.
Maybe you wanted to refer to this pattern?
url(r'group_list/?P<number>/', views.date_list, name='date_list')

Then, respectively
reverse('date_list', args=[self.number])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question