Beto
Beto

Reputation: 579

How to send text by template via url in Django?

I want to send text to a view via template. I have two different types of clients that will be processed differently, to take advantage of code I put it in a single view and the specific part treated it with an if else.

In the template:

<a href="{% url 'client' 'prime' %}"> Client prime </a>
<a href="{% url 'client' 'free' %}"> Client </a>

In the urls.py

....
path('client/<str:typeclient>', Client, name='client'),
.....

In the view:

def Client(request, typeclient):
    ...
    if typeclient == "prime":
       ...
    else:
      ....

However I get the following error:

NoReverseMatch at /

Reverse for 'client' with no arguments not found. 1 pattern(s) tried: ['client\\/(?P<typeclient>[^/]+)$']

Apparently the text is not passing as a parameter that I inserted in the url.

In this sense, how can I pass a text from the template via url?

Upvotes: 2

Views: 437

Answers (1)

rahul.m
rahul.m

Reputation: 5854

try this

 path('client/<typeclient>', Client, name='client'),


 <a href="{% url 'client' 'prime' %}"> Client prime </a>
 <a href="{% url 'client' 'free' %}"> Client </a>

Read this https://docs.djangoproject.com/en/3.1/topics/http/urls/

https://docs.djangoproject.com/en/3.1/ref/templates/builtins/#url

Upvotes: 1

Related Questions