RISOLTO Concatenare una str e un int nel template di python django framework

BigIssue

Utente Attivo
221
18
CPU
intel dual core n3050
Scheda Madre
asus x540s
RAM
4gb
GPU
intel HD
OS
Windows 10
Buongiorno a tutti,
non riesco a trovare una risposta. Forse mi potete aiutare voi.

Python:
<form id="formDoc" style="margin-top: 10px;" action="/" method="post">
        {% for keyFi,valueFi in tmplVar.jsonKeysDocFields.items %}
           
        {% with field_index="field_"|add:keyFi|stringformat:"s" %}
   
            <div style="margin-bottom: 5px; display: none;" id="{{field_index}}" class="docFieldWrapper">

Non riesco a capire perchè field_index è vuota cioè id="". Dovrebbe stampare qualcosa come field_version_ oppure field_controparte_
Dove sbaglio?
 

BigIssue

Utente Attivo
221
18
CPU
intel dual core n3050
Scheda Madre
asus x540s
RAM
4gb
GPU
intel HD
OS
Windows 10
Ho provato anche a costruire un tag custom

file pyconcat_tags.py
Python:
from django import template
register = template.Library()
@register.filter
def concat_string(value_1, value_2):
    return str(value_1) + str(value_2)

file home.html
HTML:
{% load pyconcat_tags %}
    <form id="formDoc" style="margin-top: 10px;" action="process2_module_doc.php" method="post">
        {% for keyFi,valueFi in tmplVar.jsonKeysDocFields.items %}
            
        {% with field_index=concat_string "ciao" "come" %}
        <div style="margin-bottom: 5px; display: none;" id="{{field_index}}" class="docFieldWrapper">

L'erorre che ricevo è questo: TamplateSyntaxError
'with' received an invalid token: '"ciao"'

Bho, la with è una parola riservata e serve per definire variabili dentro il template. la stringa "ciao" è un parametro per la funzione concat_string cosi anche "come".
 

BigIssue

Utente Attivo
221
18
CPU
intel dual core n3050
Scheda Madre
asus x540s
RAM
4gb
GPU
intel HD
OS
Windows 10
Qui dice che non puoi chiamare una funzione nel with.
Si giusto, infatti dice che devi costruire un tag custom. Forse è la sintassi che con la versione 4 cambia. Provo così
"Field_" | concat_string: keyFi
Notare i ":"
Appena sono sul PC la provo ma penso sia la soluzione
 
  • Mi piace
Reazioni: Mursey

Entra

oppure Accedi utilizzando
Discord Ufficiale Entra ora!

Discussioni Simili