[英]How can I see a translation in my website
I am currently working on my website, which is a translator which you input a phrase and it gets translated into an invented language.我目前正在我的网站上工作,这是一个翻译器,您输入一个短语并将其翻译成一种发明的语言。
Here's the code of the translator function:这是翻译函数的代码:
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
However, I am stuck in showing this funcion in my web, here's the code in views.py:但是,我坚持在我的网络中显示此功能,这是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')
Here's the template where you introduce the text:这是您介绍文本的模板:
<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>
Here's the template that should would show the translation.这是应该显示翻译的模板。
{% 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%}
Why are you using GET request I think the best thing to do is to use a POST request.为什么要使用 GET 请求,我认为最好的方法是使用 POST 请求。
def translated_view(request):
text = request.POST['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="post">
<div class="form-group">
<center><h2 class = "display-3">TRANSLATE YOUR DNA CHAIN</h2></center>
<br>
<br>
<textarea class="form-control" name="text" id="exampleFormControlTextarea1" rows="6"></textarea>
<br>
<button type='Submit' class= "btn btn-primary btn-lg btn-block">Translate</button>
</div>
</form>
But if you really want to use get request then you have an error.但是如果你真的想使用 get 请求,那么你就会出错。 You haven't put a name attribute in the textarea so in your case it's:您还没有在 textarea 中放置 name 属性,因此在您的情况下是:
<textarea class="form-control" name="text" id="exampleFormControlTextarea1" rows="6"></textarea>
and it your view its:你认为它是:
text = request.GET.get('text')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.