How to send text via the template via url in Django?

Asked

Viewed 50 times

-1

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

In the template:

<a href="{% url 'cliente' 'prime' %}"> Cliente prime </a>
<a href="{% url 'cliente' 'free' %}"> Cliente </a>

In the urls.py

....
path('cliente/<str:tipo>', Cliente, name='cliente'),
.....

In view:

def Cliente(request, tipo):
    ...
    if tipo == "prime":
       ...
    else:
      ....

Yet I get the following error:

NoReverseMatch at /

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

Apparently you are not passing the text as a parameter I entered in the url.

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

2 answers

0

Dentro da views, verifique por usuario:
Identificar se é anonimo:

 if str(request.user) != "AnonymousUser":
     print("usuario não registrado, anonimo")
    ...teu código

if str(request.user) == "prime":
    ...teu código
      print("usuario prime")
    #Se houver algum compo que identifique se o usuario é prime

-1

I tried to simulate the same error that were made available here but could not.

I did as described:

  • on a page index without much HTML code put the hyperlinks:
{% load bootstrap4 %}
{% load static %}
<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <title>Index</title>
    {% bootstrap_css %}
    <link href="{% static 'css/styles.css' %}" rel="stylesheet">
</head>
<body>
    <div class="container">
        <h1>TESTE</h1>
        <a href="{% url 'cliente' 'prime' %}"> Cliente prime </a>
        <a href="{% url 'cliente' 'free' %}"> Cliente </a>
    </div>

{% bootstrap_javascript jquery='full' %}
</body>
</html>

  • then in the views, without much information I did four functions:

def index(request):

    return render(request, 'index.html')


def prime(request):
    return render(request, 'prime.html')


def free(request):
    return render(request, 'free.html')


def Cliente(request, tipo):
    if tipo == "prime":
        print(f'o request: {request} e o tipo: {tipo}')
        return redirect('prime')
    else:
        print(f'o request: {request} e o tipo: {tipo}')
        return redirect('free')

-and in.py urls I forwarded as usual:

from django.urls import path

from .views import index, prime, free, Cliente

urlpatterns = [
    path('', index, name='index'),
    path('prime/', prime, name='prime'),
    path('free/', free, name='free'),
    path('cliente/<str:tipo>', Cliente, name='cliente')
    ]

by clicking on Hyperlinks the text generated in the terminal was:

the request: <Wsgirequest: GET '/client/prime'> and the type: prime

In short: I can try to help, but provide more information than you’re doing, or check what I put in here and see if you missed anything in your code.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.