0

I need to provide a list of objects, that if clicked on, bring you to the object detail page in Django Admin. The URL link needs to have the id, and the actual clickable is the name of the product.

Originally, I hardcoded the URL path as such, but was able to get the ID and Name in the link:

return "<a href='/admin/content/product/%s'>%s</a>" % (str(obj.id), obj.product_name)

Displays in CMS: MyProductName as a clickable link, which will go to product ID x.

Since hardcoding URLs will break the app in production, I am trying to use reverse. I'm trying to replicate the above:

return reverse("admin:content_product_change", args=[str(obj.id), obj.product_name])

But get error:

NoReverseMatch: Reverse for 'content_product_change' with arguments '('1', u'ProductName')' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'admin/content/product/(.+)/$']

4
  • 2
    The reverse function won't return the whole <a href=""></a> tag, it will only return paths. You should be using it like return "<a href='%s'>%s</a>" % (reverse("admin:content_product_change", args=[str(obj.id)]), obj.product_name) Commented Oct 13, 2015 at 16:43
  • @CésarBustíos That doesn't seem intuitive, but thank you! Post as answer and I'll accept Commented Oct 13, 2015 at 16:48
  • You should be rendering html in the template not in the views Commented Oct 13, 2015 at 16:51
  • @CésarBustíos can you please elaborate? Commented Oct 13, 2015 at 16:53

1 Answer 1

1

The reverse function returns paths (i.e. /admin/content/product/1), try changing your code to:

product_change_url = reverse("admin:content_product_change", args=[str(obj.id)])
return "<a href='%s'>%s</a>" % (product_change_url, obj.product_name)
Sign up to request clarification or add additional context in comments.

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.