3

I am trying to pass two arguments to make a dynamic URL for each post of a blog app I'm working on. I get it to work if I pass the id, but don't know what's the syntax when using +1 arguments (nor if I'm doing things right).

I want to make urls be 'post/<int:pk>/<slug:slug>/' but can only make it work with the id: 'post/<int:pk>/

Here's how I have it now:

URLS.PY

    urlpatterns = [
    ...
    path('post/<int:pk>/<slug:slug>/', PostDetailView.as_view(), name='post-detail'),
    path('post/new/', PostCreateView.as_view(), name='post-create'),
    path('post/<int:pk>/<slug:slug>/update/', PostUpdateView.as_view(), name='post-update'),
    path('post/<int:pk>/<slug:slug>/delete/', PostDeleteView.as_view(), name='post-delete'),
    ...
    ]

VIEWS.PY

   class PostDetailView(DetailView):
        model = Post

I am calling this in the template:

   <ul class="secondary-menu menu-list">
        <li class="menu-item"><a href="{% url 'post-update' object.slug %}">Edit Post</a></li>
    <li class="red-button"><a href="{% url 'post-delete' object.slug %}">Delete</a></li>
    </ul>

And have this function to retrieve the path to any specific instance in MODELS.py

   def get_absolute_url(self):
        return reverse('post-detail', kwargs={'pk': self.pk, 'slug':slug})

I get the following error during template rendering

Reverse for 'post-detail' with arguments '(1,)' not found. 1 pattern(s) tried: ['post/(?P<pk>[0-9]+)/(?P<slug>[-a-zA-Z0-9_]+)/$']

3 Answers 3

3
    def get_absolute_url(self):
        return reverse('post-detail', kwargs={'pk': self.pk, 'slug':self.slug})

you missed the self in slug, else you need to pass slug as a parameter to the function if self is not needed.

Sign up to request clarification or add additional context in comments.

1 Comment

I made the change, but still get the same error. May it be something about how I'm passing the url in the template? Because when I use only the id and call the url as <a href="{% url 'post-update' object.id %}">Edit Post</a> it works. But when I add the second argument (slug), it breaks <a href="{% url 'post-update' object.id object.slug %}">Edit Post</a>
0

You need to pass slug as self.slug and also if slug is empty or null your reverse url will not work.

get_absolute_url(self):
        return reverse('post-detail', kwargs={'pk': self.pk, 'slug':self.slug})

Comments

0

I found the error.

The settings for the files URLS.PY, VIEWS.PY were ok.

As both @Exprator and @Mohammed Aadil mentioned, I was missing the self in slug in the MODELS.PY file.

But I kept having the same error because I didn't update the urls from all the html templates. By the way, if you're not sure about the syntax on passing multiple arguments to a url, here's how it worked for me: {% url 'post-detail' post.id post.slug %}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.