1

I am having problems constructing a reverse url with a parameter

I have tried the answers to this question but with no luck

urls.py

urlpatterns = [
    path('publication-item/<str:publication_title>', PublicationItemView.as_view(), name='publication-item'),
]

xxx.html

<a href="{% url 'publication-item {{ publication.publication_title | urlencode }}' %}">

gives the error

Reverse for 'publication-item {{ publication.publication_title | urlencode }}' not found. 'publication-item {{ publication.publication_title | urlencode }}' is not a valid view function or pattern name.

Whereas

xxx.html

<a href="{% url 'publication-item' %}?publication_title={{ publication.publication_title | urlencode }}">

produces

Reverse for 'publication-item' with no arguments not found. 1 pattern(s) tried: ['publicationspublication\-item/(?P<publication_title>[^/]+)$']

The following works, but of course it doesn't pass the parameter

urls.py

urlpatterns = [
    path('publication-item', PublicationItemView.as_view(), name='publication-item'),
]

xxx.html

<a href="{% url 'publication-item' %}" class="btn btn-primary">

Where am I going wrong?

1 Answer 1

3

You should not use double curly brackets, you are already in a template tag, hence Django will interpret the variable:

<a href="{% url 'publication-item' publication.publication_title %}">

You do not need to URL encode yourself, Django will automatically URL encode the parameters.

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.