Issue
I can't correctly capture (read) the text received from the user why is the data from the fill-in form not being read correctly? thank you in advance
I can 't understand the reason , where did I make a mistake ?
text = str(settings.tg_message)
Name : { name }
Phone : { phone }
Vacancy : { vacancy }
a = text.find("{")
b = text.find("}")
c = text.rfind("{")
d = text.rfind("}")
u = text.find("{")
p = text.rfind("}")
part_1 = text[0:a]
part_2 = text[b + 1:c]
part_3 = text[d + 1:u]
part_4 = text[p:-2]
text_message = part_1 + tg_name + part_2 + tg_phone + part_3 + tg_vacancy + part_4
desired result:
Name : john
Phone : +359557664848
Vacancy : courier
output:
Name : Jhon
Phone : { phone }
Vacancy : +359557664848courier
Solution
The main problem is that you work with indexes, and thus search the first and last indexes for {
and }
. This thus means that no item in the middle can be used.
That being said, using such string manipulation will be very error prone: even if you manage to get this working, it is very sensitive to template changes: if for example you swap the order of the variables in the template, or omit one, it will start failing.
Moreover, you don't need this. Django already has a template render engine that you can use.
I would rewrite the tg_message
setting to:
Name : {{ name }}
Phone : {{ phone }}
Vacancy : {{ vacancy }}
and then we can let Django do all the work:
from django.template import Context, Template
template = Template(settings.tg_message)
text_message = template.render(
Context({'name': 'John', 'phone': '+359557664848', 'vacancy': 'courier'})
)
Answered By - willeM_ Van Onsem
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.