replace url in Django template - django

{% if shelf.script or shelf.log_url %}
{% if shelf.log_url %}
Log URL: {{ shelf.log_url }}<br />
{% endif %}
{% if shelf.log_url %}
<b>Another URL:</b> {{ shelf.log_url.replace("/tmp/BOOTLOG/", "http://www.sxp.com") }}<br />
{% endif %}
{% endif %}
I have already got a log URL, I would like to update this URL with a domain address and adding /index.html to at the end.
I would like to replace this URL
shelf.log_url output = /tmp/BOOTLOG/darwin_12345.tgz
to something like this.
www.sxp.com/darwin_12345/index.html
How can I do in Django template ?

What you are trying to do can be achieved by a custom template tag. You can find more about custom template tags at:
https://docs.djangoproject.com/en/1.11/howto/custom-template-tags/

you should try using custom filter for this. and you will get better idea from Django custom filter documentation
for example
{{shelf.log_url|replace:"url you need"}}
add filter in your any file for example filters.py
#register.filter(name='replace')
def replace(url, new_url):
line = url.replace('/tmp/BOOTLOG/', new_url)
return line

Related

construct url dynamically (name view as argument) with name space url tag in Django template

In a Django template, I want to construct dynamically urls
I try to concatenate (see below) but got an error :
Could not parse the remainder: '+key+'_index'' from ''ecrf:'+key+'_index''
{% for key, values in forms.items %}
<!-- for exemple a key would be 'inclusion' -->
{{ key|capfirst }}
{% endfor %}
urls
app_name='ecrf'
urlpatterns = [
path('inclusion/create/', views.InclusionCreate.as_view(), name='inclusion_create'),
expected result:
Inclusion
You can use the add template tag[Django docs] to add strings together:
{% for key, values in forms.items %}
<!-- for example a key would be 'inclusion' -->
{{ key|capfirst }}
{% endfor %}
But this feels a bit hacky to me, perhaps your forms dictionary can be restructured so that such logic is done in the view itself? For example the dictionary can be:
{'inclusion': {'value': <some_value>, 'create_url': 'ecrf:inclusion_create'}}
And in your template your loop would be:
{% for key, values in forms.items %}
<!-- for example a key would be 'inclusion' -->
{{ key|capfirst }}
{% endfor %}

How to set href value in html using python

I have been trying to solve this problem: i want to set url to html a tag using loop. I tried this way. But it gives me error which is "Reverse for 'i.menuResolve' not found. 'i.menuResolve' is not a valid view function or pattern name".
In case, "i.menuResolve" returns url which is '/sales/profile' etc.
{% for i in userMenus %}
<li>
<a href="{% url 'i.menuResolve' %}" ></a>
</li>
{% endfor %}
Please help if anybody knows this error?
If you have a method or property that returns a URL, you don't need to use Django's {% url %} template tag. That template tag passes the arguments to Django's reverse() function, but you don't need to do that if you've already got the URL.
Give this a try:
{% for i in userMenus %}
<li>
<a href="{{ i.menuResolve }}" ></a>
</li>
{% endfor %}

flask url conflicting with href

In my HTML code i have the following jinja template code:
{% for site in sites %}
{{ site.site_name }}
{% endfor %}
The issue is: let's assume that {{ site.site_link }} = www.domainname.com, so whenever the link is clicked it supposed to direct the user to the domainname page. but, it leads to :
http://127.0.0.1:5000/www.domainname.com
which is a 404..Any idea why is this happening and how to fix it?
I don't know if this is relevant, but, I am using flask Blueprints.
You need to put http:// in front of it, like href="http://........"
{% for site in sites %}
{{ site.site_name }}
{% endfor %}

Combine built-in tags in templates with variables

I want to combine the built-in tag: {% url %} with a dynamic url which I parse with {{ url_value }}
I tried doing: {% url 'urlname' url_value %}, but it didn't work
This is the url:
url(r'^(?P<slug>[^/]+)/$', 'reviews.views.single_product', name='product_detail'),
{{url_value }} just represents the slug
I think it should be like:
{% url product_detail slug=url_value %}

How do I do display content in Django based on URL and all recurisive urls

So I have this in a Django template.
{% if request.get_full_path = "/blog/" %}
do something
{% endif %}
Which does exactly what I want when I go to http://somesite.com/blog/
What I want to do is also include all subdirectories.
So something like
{% if request.get_full_path = "/blog/*" %}
do something
{% endif %}
Unfortunately as far as I can tell Django doesn't do wildcards in templates. So how do I do this?
Try with this:
{% if request.get_full_path|slice:'0:6' == '/blog/' %}