0

I'm trying to link to a blog post but it has a slug in it, and i'm not sure how to pass it in. I've looked online but I didn't apparently understand the answers since I haven't found a solution yet.

I'm trying to do like the following in my template href="{% url 'blog_detail' post.categories.5 slug=blender-task-name post.14 %}", but it gives me the following exception: Could not parse the remainder: '-task-name' from 'blender-task-name'

post.categories.5 because i'm trying to access category with id 5 and post.14 because it's the post with the id 14

This is how the object i'm trying to link to looks like Blog object

URLS.py:

urlpatterns = [
    path("", views.blog_index, name="blog_index"),
    path("<category>/<slug>/<int:pk>/", views.blog_detail, name="blog_detail"),
    path('<category>/', views.blog_category, name='blog_category'),
]

Models.py:

class Category(models.Model):
    name = models.CharField(max_length=30)
    def __str__(self): 
       return self.name 

class Post(models.Model):
    slug = models.SlugField(max_length = 250, null = True, blank = True)
    title = models.CharField(max_length = 250, default="Blog Post")
    body = models.TextField()
    created_on = models.DateTimeField(null=True)
    last_modified = models.DateTimeField(null=True)
    categories = models.ForeignKey('Category', related_name='posts', default="2", unique=False, on_delete=models.CASCADE)

class Comment(models.Model):
    author = models.CharField(max_length=60)
    body = models.TextField()
    created_on = models.DateTimeField(auto_now_add=True)
    post = models.ForeignKey('Post', on_delete=models.CASCADE)
Lesnie Sncheider
  • 365
  • 3
  • 16

0 Answers0