簡體   English   中英

有沒有一種方法可以使用常量替換在Django模板的html文件中傳遞URL值?

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

這是Django問題。 我是這個新手。 這是我的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),
]

我希望能夠在html模板中引用此名稱“ Constants.NAME_OF_URL_FOR_SEEING_ALL_SPLUNK_QUERIES ”。

以下是html文件中的代碼段

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

基本上,我想使用常量替換而不是{% url 'splunkAutomation:splunkQuerySpecific' query.id %}

有辦法使用嗎?

我在路徑中添加一個名稱:

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

您可以像這樣使用它:

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

如果要使用動態網址,可以執行此操作(基於類的視圖的示例):

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

在您看來,您可以抓取參數:

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

然后,您可以在模板中使用

{{ view.webaddress }}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM