0

Suppose I have this view

def foo_bar(request):

    context = {
        'url': 'app_name:foo"
    }

    return render(request, 'template.html', context)

And in the template, I want something like this:

<form action="{% url {{ url }} %}">
...
</form>

However, it throws ID expected

Is there a way to make this work?

2
  • That would not possibly show "ID expected". Please show the actual code you are using and the exact error message. Commented Apr 26, 2018 at 11:42
  • 1
    Don't use {{ }} inside a template tag. Use {% url url %} instead. Commented Apr 26, 2018 at 12:12

1 Answer 1

4

Maybe a better way will be to use django reverse?

from django.urls import reverse

def foo_bar(request):

    context = {
        'url': reverse('app_name:foo'),
    }

    return render(request, 'template.html', context)

And in the template simply:

<form action="{{ url }}">
...
</form>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.