Pass variable to href template in Django 2

Jose Manuel

I have below function in my views.py Django 2 project and I am trying to pass the variable "stylefolder" from views.py to my base.html template in order to dynamically get the correct href in my link tag.

Below is my current code:

def home(request):
  products = Product.objects.order_by('-votes_total')
  paginator = Paginator(products, 10)
  page = request.GET.get('page')
  paged_listings = paginator.get_page(page)

  context = {
    'products':paged_listings,
    'stylefolder': 'cryptoblog/style.css'
  }
  return render(request,'home1.html', context)

In the base.html file, I have below code.

href="{% static '{{ stylefolder }}' %}">

The problem is that when I look into the view:Source section of the page, I get as a result:

 <link rel="stylesheet" type="text/css" href="/static/%7B%7B%20stylefolder%20%7D%7D">

instead of below desired result:

 <link rel="stylesheet" type="text/css" href="/static/cryptoblog/style.css">

What should I change to get "/static/cryptoblog/style.css" in the href tag?

Willem Van Onsem

Since you are in a template tag, you should not add the double curly brackets {{ .. }}, you can use the variablename instead, like:

<link href="{% static stylefolder %}" rel="stylesheet" type="text/css"">

The reason why you get /static/%7B%7B%20stylefolder%20%7D%7D is because this is the percent-encoding [wiki] of the '{{ stylefolder }}' string. This is logical, since it looks like you passed a string literal:

>>> from urllib.parse import unquote
>>> unquote('/static/%7B%7B%20stylefolder%20%7D%7D')
'/static/{{ stylefolder }}'

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Django pass variable into template

Pass variable in to template in django

Passing variable to href django template

is it possible to pass template variable to django template tag

Pass variable in Angular 2 template

Is there a way to pass a variable to an 'extended' template in Django?

Pass in Django Template Variable to JS function

Django - Pass a variable from the Template to the Manager

Is there a way to pass in variable to <div> id in django template?

How to pass a variable from template to views in django?

Pass variable from template to view django

Pass in dynamic href using Django include template tag AND url tag

pass parameter to form from template in Django 2

How to pass a template tag from one template to another in django 2

DJango Template - Custom template tags pass 2 variables

Django: If value exists in database, pass it as a variable from view to template

how to pass many values through the same variable in Django template

How to pass a variable from python Django to HTML template

Is it possible to pass Ajax response as template context variable in Django?

How to pass variable from Python API to Django template

django: how to pass template variable to javascript onclick routine?

How to pass template variable to slice filter in Django templates

Is it possible to pass a variable as a CSS class to a Django html template?

How to pass JavaScript variable from Django-object in html template?

Django 1.9 cannot pass context variable to template using {{}}

How can I conditionally pass a value to a with variable in Django template?

Variable in a Variable in Django Template

Pass variable to component template

Pass a variable to the next template