how can I skip some text which is between quotes("text") or brackets(<div>text<div>) or anything else
here is an example where I have a Django template and I want to find the tags, where I have intentionally injected tags inside a string.
{% load humanize %}
{% url "core:p%}os{%t" post.pk %} {% url "index" %}
and this is the regex currently I have
\{%\s*\b(url|load)\b.*?\s*%\}
What this regex matches is
{% url "core:p%}
But obviously that is not what I want. I want to match the whole tag like this
{% url "core:p%}os{%t" post.pk %}
Here is the regexer link, where you can test it.
Related
i am retrieving information from the database and using {%if%} and {%endif%} condition so if that field is blank it does not cause any problem but when that field does not have any value it creates a new blank line in place of that looks bad in styling I saw an answer use this {%if -%} {%- endif %}it doesn't work and throws the error all I want if field is blank do not create any new blank line and render second line below that without creating any whitespace
any suggestion will be helpfull
You could format like this:
My first line{% if second_line %}<br>
{{second_line}}{% endif %}<br>
My third line
The idea being if second_line doesn't exist it won't render that <br>
You can use the built in templatetag
{% spaceless %}
Space less content here
{% endspaceless %}
As per the documentation that can be found here:
https://docs.djangoproject.com/en/4.1/ref/templates/builtins/#spaceless
Thespaceless tag:
Removes whitespace between HTML tags. This includes tab characters and newlines.
{% spaceless %}
<p>
Foo
</p>
{% endspaceless %}
Would result in:
<p>Foo</p>
I was writing my first django program, following instructions from a book. The book wrote url tags like {% url 'learning_logs:index' %} (learning_logs is the app name) but when I tried to emulate that, I got an error until I went with just {% url 'index' %} after reading a post here. Later in my program, after I created another app on the same project called 'users', I was getting the error Reverse for '' not found. '' is not a valid view function or pattern name until I reverted to the initial method used in the book, adding "learning_logs:" before the page names {% url 'learning_logs:index' %}. I need some help on how to recognise when to add the 'name_of_the_app:' and when not to add it in a url tag.
Here is some code sample with the 'learning_logs:' included:
<h1>
Learning Log -
Topics -
{% if user.is_authenticated %}
Hello, {{ user.username }}
{% else %}
log in
{% endif %}
</h1>
{% block content %}{% endblock content %}
And here is how I wrote it with just the url name, which also worked sometimes (This was before I created the new app 'users':
<h1>
Learning Log -
Topics -
</h1>
{% block content %}{% endblock content %}
in the urls.py the function include(that you use to include a new urls.py for an app) has the kwarg namespace which define the namespace of the url for the app, that is what you are looking for. The main reason for this is to not collide too commons urls like "list" or "create" between apps. So if you don't define namespace you can just use the name of the url without namespace: before.
docs here: https://docs.djangoproject.com/en/3.0/topics/http/urls/#url-namespaces
hope you understand.
I'm reading an entire page's content from database and render them in a HTML file. Here are some examples.
In database:
<a href="{% url 'home_view' %}></a>
In order to avoid escape of HTML tags, I use {{ page.content|safe }}
But I couldn't render {% url 'home_view' %}
You can control auto-escaping behavior using autoescape template tag. In your case, you can use the below snippet.
{% autoescape off %}{{ page.content }}{% endautoescape %}
You can get more information in autoescape Documentation
Just wondering what the correct syntax is for checking if the current path is equal to some url:
{% if request.path == url "app_namespace:route_name" %}
Above doesn't work - but hoping someone knows a way or method for doing this lookup...
You can use this syntax to save the url path in a template variable:
{% url 'app_namespace:route_name' as url_path %}
which you can later use within your if condition
{% if request.path == url_path %}...{% endif %}
Note that you may also find this syntax useful when you need to use the output of a url function within a blocktrans block:
{% blocktrans %}
text to translate
{% endblocktrans %}
I am currently facing a serious problem.
I use the standard django admin interface incl. change list to display one of my models.
The model has got a field, which includes a link (e.g. in database: http://localhost:8000/data/somefile.pdf'>link).
What I want now is that this string is rendered unescaped and displayed as link. I already tried the following in "change_list_results.html":
{% for result in results %}
<tr id="{{ result.1|adminfilter }}" class="{% cycle 'row1' 'row2' %}">
{% for item in result %}
{{ item|safe }}
{% endfor %}</tr>
{% endfor %}
I used "|safe" on the actual item that is output. Furthermore i tried "{% autoescape off %}". Same result, the String got escaped.
Do you see any other way to get the String displayed unescaped?
You want to set allow_tags=True on your method. It's a bit hidden, but it is described in the documentation - about a screen or so down from where this link takes you.