尝试将用户输入的输出显示到同一个 html 屏幕上
Trying to display output from the user input onto the same html screen
我相信这并不难,我是 Django 的新手,非常感谢您的帮助。
我一直在尝试将两个输入('sentence' 变量和 'word' 变量)作为 CharFields。然后获取这两个输入并用它们创建一个新的输出。我可以在控制台上打印它,但不能在 html 页面上显示它。
谢谢。
这是我得到的。
--> form.html
<div class="main">
<div class="container">
<div class="row">
<form class="content" method="post">
{% csrf_token %}
{{ form }}
{% if output %}
<h3>{{ output }}</h3>
{% endif %}
<input type="submit" name="submit_cmd" value=Run />
</form>
</div>
</div>
</div>
--> views.py
from django.shortcuts import render
from django.http import HttpResponse
from .forms import ContactForm
import subprocess as sp
import nltk
from nltk.corpus import wordnet
nltk.download('wordnet')
def contact(request):
syn = set()
output = ""
form = ContactForm()
if request.method == 'POST':
form = ContactForm(request.POST)
if form.is_valid():
sentence = form.cleaned_data['sentence']
word = form.cleaned_data['word']
for synonym in wordnet.synsets(word):
for l in synonym.lemmas():
if l.name() != word:
syn.add(l.name().lower())
for new_wrd in list(syn):
print(sentence.replace(word, new_wrd))
output += sentence.replace(word, new_wrd)
return render(request, 'pk_app/form.html', {'form': form})
--> forms.py
from django import forms
class ContactForm(forms.Form):
sentence = forms.CharField(required=True)
word = forms.CharField(required=True)
您正在创建 output
变量,但从未将其附加到您 return 模板的上下文指令。
return render(request, 'pk_app/form.html', {'form': form, 'output': output})
另外:您确定它属于在 HTML 表单元素中吗?
我相信这并不难,我是 Django 的新手,非常感谢您的帮助。
我一直在尝试将两个输入('sentence' 变量和 'word' 变量)作为 CharFields。然后获取这两个输入并用它们创建一个新的输出。我可以在控制台上打印它,但不能在 html 页面上显示它。
谢谢。 这是我得到的。
--> form.html
<div class="main">
<div class="container">
<div class="row">
<form class="content" method="post">
{% csrf_token %}
{{ form }}
{% if output %}
<h3>{{ output }}</h3>
{% endif %}
<input type="submit" name="submit_cmd" value=Run />
</form>
</div>
</div>
</div>
--> views.py
from django.shortcuts import render
from django.http import HttpResponse
from .forms import ContactForm
import subprocess as sp
import nltk
from nltk.corpus import wordnet
nltk.download('wordnet')
def contact(request):
syn = set()
output = ""
form = ContactForm()
if request.method == 'POST':
form = ContactForm(request.POST)
if form.is_valid():
sentence = form.cleaned_data['sentence']
word = form.cleaned_data['word']
for synonym in wordnet.synsets(word):
for l in synonym.lemmas():
if l.name() != word:
syn.add(l.name().lower())
for new_wrd in list(syn):
print(sentence.replace(word, new_wrd))
output += sentence.replace(word, new_wrd)
return render(request, 'pk_app/form.html', {'form': form})
--> forms.py
from django import forms
class ContactForm(forms.Form):
sentence = forms.CharField(required=True)
word = forms.CharField(required=True)
您正在创建 output
变量,但从未将其附加到您 return 模板的上下文指令。
return render(request, 'pk_app/form.html', {'form': form, 'output': output})
另外:您确定它属于在 HTML 表单元素中吗?