我目前正在我的网站上工作,这是一个翻译器,你输入一个短语,它就会被翻译成一种发明的语言。
下面是翻译函数的代码:
def translator(phrase):
translation = ""
for letter in phrase:
if letter.lower() in "a":
if letter.isupper:
translation = translation + "U"
else:
translation = translation + "u"
elif letter.lower() in "t":
if letter.isupper:
translation = translation + "A"
else:
translation = translation + "a"
elif letter.lower() in "c":
if letter.isupper:
translation = translation + "G"
else:
translation = translation + "g"
elif letter.lower() in "g":
if letter.isupper:
translation = translation + "C"
else:
translation = translation + "c"
return translation
但是,我一直坚持在我的网站上展示这个功能,这是 views.py 中的代码:
from .translate import translator
def translator_view(request):
return render(request,'main/translator.html')
def translated_view(request):
text = request.GET.get('text')
print('text:', text)
translate = translator
dt = translator.detect(text)
tr = translated.text
context = {
'translated': tr
}
return render(request, context, 'main/translated.html')
这是您介绍文本的模板:
<form action="{% url 'translated' %}" method= "get">
<div class="form-group">
<center><h2 class = "display-3">TRANSLATE YOUR DNA CHAIN</h2></center>
<br>
<br>
<textarea class="form-control" id="exampleFormControlTextarea1" rows="6"></textarea>
<br>
<button type='Submit' class= "btn btn-primary btn-lg btn-block">Translate</button>
</div>
</form>
这是应该显示翻译的模板。
{% extends "base.html"%}
{% block content%}
<div>
<center><h2 class="display-4">DNA TRANSLATED SUCCESFULLY INTO</h2></center>
<br>
<br>
<br>
<h3>
{{ translated }}
</h3>
</div>
{% endblock content%}