[Fixed]-Dynamic URL with variable django template

19👍

You can use the add template filter:

{% url "base:"|add:section pk=project.id %}

5👍

for my case that also worked

{% for section in sections %}
<a href="{% url "base" pk=object.id %}">..</a>
{% endfor %}

where url pattern is

url=[
path('base/<pk>/',base,name='base'),
]

2👍

Each of my models has a list view, a create/update view, and a delete view. These will be used by different functions within the customer’s organisation to maintain the data they are responsible for. Each list view has links to the relevant create, update, and delete views. I wanted to build a page with a list of links to the list view. Here’s how I did it.

I created a function based view in views.py.

def index(request):
     app     = request.resolver_match.app_name
     models  = apps.get_app_config(app).get_models()
     names   = [model._meta.model.__name__ for model in models]
     context = {
         "names" : names,
     }
     return render(request, app + '/index.html', context)

I created a template app/templates/app/index.html

{% for name in names %}
<li><a href="{% url request.resolver_match.app_name|add:':'|add:name|lower|add:'-review'%}">{{ name}}</a></li>                                              
{% endfor %}

Leave a comment