F
F
freiqq2020-12-06 17:44:52
Django
freiqq, 2020-12-06 17:44:52

Why doesn't Django find the slug?

When you try to navigate through the category, an error occurs:

If there is not enough data to answer, then write what needs to be added to the question. Thanks in advance

NoReverseMatch at /1928/december_rush/
Reverse for 'category_list' with keyword arguments '{'card': , 'category': 'backend'}' not found. 1 pattern(s) tried: ['1928/(?P[-a-zA-Z0-9_]+)/(?P[-a-zA-Z0-9_]+)/$']

models.py

class Card(models.Model):
    """ Карточка для выбора блога о сайте """
    STATUS_CHOICES = (
        ('draft', 'Draft'),
        ('published', 'Published')
    )

    title = models.CharField('Заголовок', max_length=100)
    text = models.TextField('Текст')
    slug = models.SlugField('Url', max_length=150)
    status = models.CharField('Стаутс публикации', choices=STATUS_CHOICES, default='draft', max_length=15)
    published = PublishedManager()
    objects = models.Manager()

    def get_absolute_url(self):
        return reverse('blog:post_list', kwargs={'card':self.slug})
    
    def __str__(self):
        return self.title
    
    class Meta:
        db_table = 'Cards'
        verbose_name = 'Карточка'    
        verbose_name_plural = 'Карточки'




class Category(models.Model):
    """ Категории постов """
    STATUS_CHOICES = (
        ('draft', 'Draft'),
        ('published', 'Published')
    )

    title = models.CharField('Заголовок', max_length=100)
    slug = models.SlugField('url')
    status = models.CharField('Стаутс публикации', choices=STATUS_CHOICES,default='draft', max_length=15)
    published = PublishedManager()
    objects = models.Manager()

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('blog:category_list', kwargs={'card':Card.slug,
                                                     'category':self.slug})
    
    class Meta:
        verbose_name = 'Категория'
        verbose_name_plural = 'Категории'



class Post(models.Model):
    """ Страница поста """
    STATUS_CHOICES = (
        ('draft', 'Draft'),
        ('published', 'Published')
    )
    title = models.CharField('Заголовок', max_length=150)
    slug = models.SlugField('Url', max_length=100)
    author = models.ForeignKey(User, on_delete=models.CASCADE, blank=False, related_name='blog_posts', verbose_name='Автор')
    text = models.TextField('Текст')
    created = models.DateTimeField('Дата создания', auto_now=True)
    updated = models.DateTimeField('Дата редактирования', auto_now_add=True)
    publish = models.DateTimeField('Дата публикации', default=timezone.now) 
    card = models.ForeignKey(Card, verbose_name='Карточка', on_delete=models.SET_NULL, null=True, blank=True)
    category = models.ManyToManyField(Category, verbose_name='Категория', blank=True,)
    status = models.CharField('Стаутс публикации', choices=STATUS_CHOICES, default='draft', max_length=15)
    objects = models.Manager()
    published = PublishedManager()

    def get_absolute_url(self):
        return reverse('blog:post_detail', args=[self.card.slug,
                                                 self.slug,
                                                 self.publish.day,
                                                 self.publish.month,
                                                 self.publish.year,])
    
    def __str__(self):
        return self.title

    class Meta:
        ordering = ('-publish',)
        db_table='Post'
        verbose_name = 'Пост'
        verbose_name_plural = 'Посты'


urls.py
path('<slug:card>/<slug:category>/', views.category_list, name='category_list'),

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2020-12-06
@freiqq

well, they obviously write to you that you yourself described two slugs in urls.py - card and category, where you have them in /1928/december_rush/ not counting 1928 about which you are silent ( ['1928/(?P[-a -zA-Z0-9_]+)/(?P[-a-zA-Z0-9_]+)/$'])?
PS 'card':Card.slug, what kind of trash is this, tell me

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question