简体   繁体   中英

URL reversion in Django template when pattern name is variable

I am passing a variable in django template named "link". This is my code snippet in views.py

 link = "msdetail/{{item.post_id}}"
 return render(request, "mart/all-products.html", {'t': ms, 'cat':category, 'link':link})

and on html file :

<a href={% url '{{link}}' %} class="btn btn-primary">View</a>

it is giving this error:

"NoReverseMatch at /mart/menshirt Reverse for '{{link}}' not found. '{{link}}' is not a valid view function or pattern name.".....

the error happens when i click the button. Can I not pass url like this or is it some other issue?

Solution

<a href='{{link}}' class="btn btn-primary">View</a>


Why is that?

Typing {% url 'X' %} is saying, Get me the URL which has name='X' .

This is not the case, Your case has a variable that you want to include it in the context, That's all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM