Louis
Louis

Reputation: 370

NoReverseMatch at /plat/salade-nicoise/

I hope you're well.

It's probably a very simple question but:

Reverse for 'post_catego' with arguments '('salade nicoise',)' not found. 1 pattern(s) tried: ['plat/(?P[-a-zA-Z0-9_]+)/$']

So if the name of the slug and the catego are the same it works, but if not it does not works.

Could someone explain to me why that works like?

views.py

def catego(request, slug):
    catego = Catego.objects.get(slug=slug)
    
    context = {
        'catego': catego
    }

models.py

class Catego(models.Model):
    name = models.CharField(max_length=20)
    slug = models.SlugField(default='nocategory')
    status = models.IntegerField(choices=STATUS, default=0)
    
    def __str__(self):
        return self.name

class Post(models.Model):
    ...
    categories = models.ManyToManyField('Catego', related_name='posts')

urls.py

path('plat/<slug:slug>/', catego, name="post_catego"),

post_catego.html

{% for post in catego.posts.all %}
...
{% for catego in post.categories.all %} <a href="{% url 'post_catego' catego.name %}">&#128154; {{ catego.name }}</a>{% endfor %}
...
{% enfor %}

Upvotes: 0

Views: 22

Answers (1)

Kishan Parmar
Kishan Parmar

Reputation: 820

I hope you're well too.

It has probably a very simple answer in your post_catego.html when passing the url value put catego.slug instead of catego.name.

post_catego.html

{% for post in catego.posts.all %}
...
{% for catego in post.categories.all %} <a href="{% url 'post_catego' catego.slug %}">&#128154; {{ catego.name }}</a>{% endfor %}
...
{% enfor %}

Upvotes: 1

Related Questions