简体   繁体   中英

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

This is Django problem. I am a newbie to this. This is my url.py file

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.

The following is the snippet from the html file

    <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 %}

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 }}

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