简体   繁体   中英

How to pass Dynamic Value in Html anchor tag using Python Django

I am working on Cuckoo Sandbox and I hope that everyone will understand my question as It is going difficult for me to explain the question. I am working on different files and I am facing some issues. The issue is that I want to call dynamic variable in html anchor tag in Django, but, when I pass the dynamic variable the sidebar disappears automatically. I need your help guys:

urls.py file

url(r"^(?P<task_id>\d+)/$", AnalysisRoutes.redirect_default, name="analysis/redirect_default"),

routes.py file:

@staticmethod
    def redirect_default(request, task_id):
        if not isinstance(task_id, (unicode, str)):
            task_id = str(task_id)

        return redirect(reverse(
            "analysis",
            args=(re.sub(r"\^d+", "", task_id), "summary")),
            permanent=False
        )

include.html file:

<li>
                <a href="{% url 'analysis/redirect_default'  45 %}">
                    <div class="parent-icon"><i class='bx bx-home'></i>
                    </div>
                    <div class="menu-title">Summary</div>
                </a>
            </li>

In HTML file you can see that there is a int number 45. Just need to parse task_id dynamic value in html anchor tag. When I pass task_id variable in replace of 45 the sidebar disappears automatically. Kindly help me to resolve this issue. Thank you

Try get_absolute_url() instead of hard coding.

from django.urls import reverse
def get_absolute_url(self):
    return reverse('analysis', kwargs={'id' : self.id})

<a href="{{ analysis.get_absolute_url }}">{{ analysis.data}}</a>

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