Background of the project: I am working on a text-to-speech (TTS) converter with Django. I have the python code that works well for the TTS converting and output the audio file, but I want to make it as a web app and gonna deploy to the cloud, that's why I am plugging it to Django.
There are only two variables that input from the user: the text content that would be converted and selected language. My thought is to set the input variables as temporary items so that it wouldn't bomb the server - so to the output file to save as temporary file and automatically download to the user and delete the file after finish downloading.
After searching for possible similar cases and solution, I found a post which is explaining how to use in memory and temporary files, but it seems only for uploading files.
My question is, right now I am coding to send the data to the sqlite3 database but obviously it doesn't make sense to input data and then delete frequently. I am thinking to use session with the view.py as follow (not sure if it works):
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
if request.method == 'POST':
text_to_be_converted = request.session['text_content']
if not text_to_be_converted:
return render(request, 'index.html')
language = request.session['language']
But I have no idea how to modify the HTML part, especially the setting of form action - what should I set for the form action?
Here's the HTML part:
<html>
<head>
<title>Text-to-Speech Converter|文字-語音轉換器</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href='https://fonts.googleapis.com/css?family=Nunito:400,300' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
{% load static %}
<link rel="stylesheet" href="{% static 'css/styles.css' %}">
</head>
<body>
<div class="row">
<div class="col-md-12">
<form action="/converter/" method="POST">
{% csrf_token %}
<h2> Text-to-Speech Converter<br>文字-語音轉換器</h2>
<fieldset>
<label for="text-label-text-content">Text Content|文字內容:</label>
<textarea id="text_content" name="text_content"></textarea>
<label for="language_select">Language|語言:</label>
<select id="language" name="language">
<optgroup label="Select Language|選擇語言">
<option value="cantonese">Cantonese|廣東話</option>
<option value="mandarin">Mandarin|國語</option>
<option value="putonghua">Putonghua|普通話</option>
<option value="english">English|英文</option>
<option value="french">French|法文</option>
<option value="japanese">Japanese|日文</option>
<option value="vietnamese">Vietnamese|越南文</option>
</optgroup>
</select>
</fieldset>
<p>Click "Transform|轉換" to transform and download the audio.</p>
<p>按「Transform|轉換」轉換並下載語音檔。</p>
<center><button type="submit">Transform|轉換</button></center>
</form>
</div>
</div>
</body>
</html>
I think my urls.py setting is fine but just in case I leave them here.
This is the project/urls.py:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('converter/', include('converter.urls')),
path('admin/', admin.site.urls),
And this is the converter/urls.py (app):
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
Many thanks!