简体   繁体   English

有没有一种方法可以使用常量替换在Django模板的html文件中传递URL值?

[英]Is there way a to pass URL values in html files in django templates using constant substitution?

This is Django problem. 这是Django问题。 I am a newbie to this. 我是这个新手。 This is my url.py file 这是我的url.py文件

from django.urls import path
from splunkAutomation.logic.Constants import Constants
from . import views

app_name = 'splunkAutomation'
urlpatterns = [
 path(Constants.URL_FOR_SEEING_ALL_SPLUNK_QUERIES,views.viewForSeeingAllSplunkQueries, name=Constants.NAME_OF_URL_FOR_SEEING_ALL_SPLUNK_QUERIES),
]

I want to be able to reference this name " Constants.NAME_OF_URL_FOR_SEEING_ALL_SPLUNK_QUERIES " in html template. 我希望能够在html模板中引用此名称“ Constants.NAME_OF_URL_FOR_SEEING_ALL_SPLUNK_QUERIES ”。

The following is the snippet from the html file 以下是html文件中的代码段

    <ul class="actions">
    <li><a href="{% url 'splunkAutomation:splunkQuerySpecific' query.id  %}" class="button special">Check</a></li>
    </ul>

Basically I want to use the constant substitution instead of {% url 'splunkAutomation:splunkQuerySpecific' query.id %} 基本上,我想使用常量替换而不是{% url 'splunkAutomation:splunkQuerySpecific' query.id %}

Is there a way to use this ? 有办法使用吗?

I you add a name to the path: 我在路径中添加一个名称:

path(Constants.URL_FOR_SEEING_ALL_SPLUNK_QUERIES,views.viewForSeeingAllSplunkQueries, name="url_link"),

You can use it like this: 您可以像这样使用它:

<a href="{% url 'url_link' query.id  %}" class="button special">Check</a>

If you want to have a dynamic url, you can do this (example for class based views): 如果要使用动态网址,可以执行此操作(基于类的视图的示例):

urlpatterns = patterns('',
    url(
        regex=r'^(?P<webaddress>\w+)/$',
        view=Mysite.as_view(),
    ),
)

And in your view you grap the parameter: 在您看来,您可以抓取参数:

def dispatch(self, request, *args, **kwargs):
    self.webaddress= kwargs.get('webaddress', '')

In your template you can then access the value with 然后,您可以在模板中使用

{{ view.webaddress }}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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