Django & Jinja2 templates using {{ url() }}

I am trying to figure out how to pass my user_id within my html using jinja's {{ url() }}, using urls that don't need any id like /dashboard/ work fine but I need to pass an id to this- example: /user/3 . I have tried the following with no success:

{{ url('detail') }}
{{ url('detail', user_id=User.id) }}
{{ url('detail', User.id) }}

Here's part of my views and html:

views.py

urlpatterns = [ path('dashboard/', dashboard, name='dashboard'), path('user/<int:user_id>/', detail, name='detail'),
]

dashboard.html

{% for User in all_users %} {{ url('detail') }}
{% endfor %}

Any help on this would be appreciated, thanks

2

3 Answers

I found a solution:

{% for User in all_users %} {{ url('detail', args=[User.id] )}}
{% endfor %}
1

This also works:

{% url 'detail' user.id %} 

Then, in views.py, your corresponding function should receive user_id as an argument (internally user.id becomes user_id).

0

Django now has official backend support for Jinja2 - I'm guessing you were using django-jinja which provides an implementation of the url function but it's fairly straightforward to integrate Django and Jinja now without adding any additional dependencies (besides Jinja2).

First, configure your TEMPLATES in your settings file and add a dictionary entry with the jinja2 backend:

TEMPLATES = [ { 'BACKEND': 'django.template.backends.jinja2.Jinja2', 'APP_DIRS': True, 'OPTIONS': { 'extensions': [<custom extensions if any>] 'environment': 'yourapp.jinja2.environment', ... }, }, ...
]

The key part of this is the environment setting. The examples suggest creating a jinja2.py file in your app directory and defining an environment function there that you'll set to a function that returns a Jinja2 Environment, e.g.,

# example yourapp/jinja2.py
from django.conf import settings
from django.urls import reverse
from django.templatetags.static import static
from jinja2 import Environment
def environment(**options): env = Environment(**options) env.globals.update({ 'static': static, 'url': reverse, 'settings': settings, ... }) return env

Here we are binding Python functions (and Django things) to be made available in our Jinja2 environment, so now static in a jinja template can be called static(...) and invoke the Django static function defined in django.templatetags.static, and url is bound to the Django reverse function so url('detail', args=[user.id]) should work. If you prefer a signature like {{ url('detail', pk=123) }} without generating an invalid arguments error you can define your own function:

def jinja_url(viewname, *args, **kwargs): return reverse(viewname, args=args, kwargs=kwargs)

and bind 'url': jinja_url in your TEMPLATES settings instead.

It's also important to note that Django looks for your jinja templates in a jinja2 directory under your app dir, not the templates directory.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like