Using a variable in a {% url %} when `app_name` has been defined - django

I am having issues trying to use a Django template variable to form a URL in combination with the app_name attribute.
Currently, I am using a template variable to form a URL
On our page about {{ variable_name }}.
Though I am aware that it is seen as good practice to make a URL more accurate by defining app_name in URLs. e.g.
app_name = "name_of_app"
Meaning that URLs are then written like:
About
Is it possible for me to combine my current code with this approach? To use a variable and the app_name attribute?
I have experimented with this {% url 'NameOfApp:'this_is_a_variable %}, by putting the quote marks in different places, but no success yet.

You need to use the "add" filter as such:
{% url "name_of_app:"|add:variable %}

Django convert all the template tags and variables in to plain text while rendering. We can't directly do that. Try using template filter or with tag.

Related

Django template nested include passing variables

I use django template index.html to render the frontpage. It includes another template to create a link icon. This template url_icon.html includes another template icon.html. When passing the arguments down the way, I face with an error. How to fix it?
index.html
.
.
.
{% include "url_icon.html" with name="return" url="/" %}
.
.
.
url_icon.html
{% include "icon.html" with icon={{ name }} %}
icon.html
<img src="/static/images/{{ name }}.png" />
Causing an error:
Could not parse the remainder: '{{' from '{{'
it looks like there are a few things you can do to improve/fix this. Addressing #1 and #2 should fix your issue. I've also added suggestions for best practices that would probably require refactoring (#3, #4).
It looks like you need to remove the curly-braces from name inside the {% include %} tag. Context variables can be used inside tags without extra syntax.
url_icon.html:
{% include "icon.html" with icon=name %}
icon.html will have access to name since you're not using the only keyword when updating its context, so your code might appear to work at first ({% include %} documentation). However, it looks like your intention is to refer to it as icon.
Use the variable icon in instead of name
icon.html:
<img src="/static/images/{{ icon }}.png" />
Optional suggestion: Use Django's staticfiles system
Try using the {% static %} tag for your icon. This will help make deployment easier, especially if you use a separate CDN from your webserver. There's lots of literature on how to set up staticfiles for Django projects in production, it's a large topic, but you'll be able to approach it more easily if you use the {% static %} tag from the beginning.
Optional suggestion: Django's URL routing system
Your route in index.html is hard-coded to be "/". Django has a powerful URL referencing system to leverage. If you've defined the root URL / using Django too, you can refer to it by name. Docs: {% url %}, and for the back-end, reverse().

djangocms-snippet which contains {% %} doesnot show up as placeholder content

I am trying to make my footer a editable from frontend.. using placeholder and inserting footer snippet.
yet, my footer snippet contains django template language
e.g.
Terms and Conditions
as a result, the placeholder content is not showing up, if i remove the django specific things
Terms and Conditions
it is working.
how can I make it work with django reverse url?
I could have given hard coded path but i want the path translatable so i need to reverse by url's name.
If I've got you right, just create custom tag and store templates in database:
from django.template import RequestContext, Template
#register.simple_tag
def footer(request):
snippet = Snippet.object.get(name='footer')
template = Template(snippet.html)
return template.render(RequestContext(request))
{% footer request %}
use this syntax:
{% url 'terms_conditions' as the_url %}
Terms and Conditions
use smartsnippets
These has the capability of rendering django tags

New url syntax from django 1.3/dev onwards

Why did the django core developers allow the url templatetag to point directly to a django view function? (reference - https://docs.djangoproject.com/en/dev/ref/templates/builtins/#url)
{% load url from future %}
{# 1st method: pointing to a view function #}
{% url 'app_views.client' %}
{# 2nd method: pointing to a named url #}
{% url 'myapp:view-name' %}
One can already name the url in urls.py and hence use the 2nd method to point to a specific url. It doesn't feel right to allow developers to actually reference a view function directly from the template.
Does anyone know why this decision was made?
Passing a dotted view function name to the {% url %} template tag is simply the form the template tag took in the earlier days of Django, before you could name URLs. It's still supported, though as you point out, you probably wouldn't use it in a modern application.
URLs in Django are just mappings to views. Therefore, in the template, using a named URL is just indirectly referencing the view anyway.
The exception is where a single view is mapped to by multiple URLs.
Also note that they are planning to change the syntax of the url tag in 1.5. It will take a context variable as the parameter, rather than a string. It will still take views or named URLs though.

Django templates, pass template language through a template variable

I was thinking about putting my static pages in the database (simple model of url, title and content) and then having a basic base_static.html template - making them super easy to edit (from admin interface) if necessary.
I know you can easily escape html like so:
{{ content|safe }}
or
{% autoescape off %}
{{ content }}
{% endautoescape %}
But I need to be able to use some of the template language in the static pages, e.g. a simple for loop to go over variables passed to it from the view.
Is this possible, or would you recommend simply using templates to serve the static pages?(They won't change that much ever)
Thank you for your help.
If you want to store templates in db (and that is what you want if you want to use a template language) you can use this app : django-dbtemplates.
Check this question: Extending Django Flatpages to accept template tags
You may get some ideas from the solution exposed there

Using string literals as parameters to template tags in Django templates

One of the things I find myself doing often is passing string literals as parameters to template tags or functions; for instance:
{% url my-url 'my_param' %}
Unfortunately, the django template engine doesn't let you do this. So I find myself doing this a lot in my view code:
my_context_dict['MY_PARAM'] = 'my_param'
and then in my view code:
{% url my-url MY_PARAM %}
Or creating a series of URL mappings, which I personally try to avoid.
Is it possible to use a string literal in Django templates? Or possibly a more elegant solution? I haven't seen anything on here or in the documentation.
This feels wrong but is right.
text
The nested ""'s don't seem like they should work. They do. The Django {% %} material is simply pulled out of the HTML without regard for surrounding context. So the "duplicated" "'s aren't really duplicated at all.
Use double quotes instead of single quotes:
{% url my_view "my_param" %}
Very wierd - I have a django project that uses single quotes to pass a string value and it functions just fine.
<a href="{% url categories 'vendor' %}"</a>
<a href="{% url categories 'crew' %}"</a>
On further investigation it turns out this has changed in django 1.5. It now requires the quotes even around the url pattern name.