I am trying to create a website using Django which will take some text as input and convert it into audio.
I created an empty array that should store the value of the input and convert it into audio. But when I run the server, it gives me an error saying - gTTS.say(text) takes string.
My views.py
#An empty array called textForTts
textForTts = []
class UploadFileForm(forms.Form):
tts = forms.CharField(label = "Convert text to audio here")
def index(request):
if request.method == "POST":
form = UploadFileForm(request.POST)
if form.is_valid():
tts = form.cleaned_data["tts"]
textForTts.append(tts)
obj = say(language='en-us', text= textForTts)
return render(request,'demo/website.html',{
"form" : UploadFileForm(),
'obj':obj
})
my index.html:
{% block body %}
{% load gTTS %}
<form method="post">
{% csrf_token %}
<audio
src = "{{obj}}"
controls
></audio>
{{form}}
<input type = "submit">
</form>
{% endblock %}
This is my first time trying out Django. What should I do to fix my errors?
Thanks in advance.
Do you use Django-Gtts, right? AFAIK, correct form to call say
inside Django template is:
{% load gTTS %}
<audio
src="{% say 'en-us' 'text to say' %}"
controls
></audio>
I'd suggest to refactor your view as follows:
return render(request, "demo/website.html", {
"form": UploadFileForm(),
"lang": "en-us",
"text": "text to say",
})
This will allow you to call say in Django template as follows:
{% load gTTS %}
<audio
src="{% say lang text %}"
controls
></audio>