0

I am using Django 1.5.1

I am in need of passing the view name as a variable to my template.

Here is the relevant part of my view

return render(request, 'testcases/execute.html', {
        'table': table ,
        'submit_view': 'testcase_execute' ,
        "project": project})

And here is the relevant part of my template

<form method="POST" action="{% url submit_view project.slug %}">
                {% render_table table %}
                <input type="submit"/>
</form>

As you can see, I am trying pass the view name ('testcase_execute') as a variable (submit_view)to the template.

But this way I am getting the error

'url' requires a non-empty first argument. The syntax changed in Django 1.5, see the docs.

I dont see whats wrong, as per this SO thread (How to pass a variable to {% url %} tag in django?), if I need to use a variable with the url tag, I should not put quotes. There also exists other SO threads which tells the same thing.

I saw another SO thread (Passing variable urlname to url tag in django template) which states this kind of thing (using a variable with url tag) isnt supported, I should be using django-reversetags for this.

Please throw some light on this- Is there an official (Django core ) way of doing this or should I go with django-reversetags

Community
  • 1
  • 1
binithb
  • 1,910
  • 4
  • 23
  • 44
  • 3
    Have you double checked that `submit_view` is in your template context? What displays when you put `{{ submit_view }}` in your template? – Alasdair May 17 '13 at 09:33
  • 2
    Your looks fine. Reversetags is old, it was before this new url tag was created in django. Your error most likely because you somehow didnt pass submit_view variable to your template – Aldarund May 17 '13 at 10:02
  • 2
    Found the problem, thanks for the help.As both of you suggested, submit_view was not reaching the template. The reason was, the aforementioned template code was from base template X, and in the actual template used by the calling view , some other template B was being used as base. So , for anybody who stumbles on this thread, there is no need to use django-reversetags anymore, Django (atleast 1.5 and upwards) is good enough to pass on view names as variables to url tags. thank you @Alasdair and Aldarund – binithb May 17 '13 at 11:33

2 Answers2

2

Prior to Django 1.3, syntax like {% url myview %} was interpreted incorrectly […]

[…] you’ll need to change tags like {% url myview %} to {% url "myview" %}. If you were using {% load url from future %} you can simply remove that line under Django 1.5

https://docs.djangoproject.com/en/1.5/releases/1.5/#overview

Denilson Sá Maia
  • 47,466
  • 33
  • 109
  • 111
1

{% url 'submit_view' project.slug %} i guess this can help you

Denilson Sá Maia
  • 47,466
  • 33
  • 109
  • 111
Deniz Kaplan
  • 1,549
  • 1
  • 13
  • 18