Django在新行中返回新窗体输出,而不覆盖以前的输出

2024-07-03 07:23:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我用python和nltk开发了一个bot,它在命令行上工作。现在,我想用一个网页捕获输入,并将逻辑传输到Django。 我将用一个简单的例子来说明我的问题

在my index.html中,我让一个用户在一个输入框中输入一个问题,单击send后,视图只返回他们输入的内容:输入框下方的“问题”和“答案”。 问题是,当他们输入一个新的问题,它覆盖了前一个,但我希望新的问题和答案出现在下面的前一个聊天线程

如果你不明白我的意思,我就用图像

发生这种情况: when the user enters first chat message. 然后 when the user enters second chat message.

我想要的是: to have both of them as a thread and not two overwriting one

我刚刚开始在这个项目中使用Django,而在cmd中我可以使用它

while True:

我对如何使用Django中的视图和模板有点困惑

这是我的index.html模板

<!DOCTYPE html>
<html>
    <head>
        <title>Tedbot</title>
    </head>
    <body>
        <div>
            <h1>Hello, I'm Tedbot</h1>
            <p>I'll help you join aBc University</p>
        </div>
        <div>
            <strong>{{ boldmessage }}</strong>
        </div>
        <div>
            <form name='form' action="{% url 'index' %}" method="POST"> {% csrf_token %}
                <input type="text" name="question"/>
                <input type="submit" name="send" value="send">
            </form>
        </div>

        <div>
            {{question}} <br />
            {{ answer }}
        </div>

    </body>
</html> 

我的表格.py

from django import forms

class ReturnAnswer(forms.Form):
    question = forms.CharField(max_length=200)

还有我的观点

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import nltk
import sklearn
from django.shortcuts import render
from django.http import HttpResponse
from tedbot.forms import ReturnAnswer

# Create your views here.
def index(request):
    answer = "Something"
    question = "Something else"
    if request.method == 'POST':
        myform = ReturnAnswer(request.POST)

        if myform.is_valid():
            question = myform.cleaned_data['question']
            answer = myform.cleaned_data['question']
    else:
        myform = ReturnAnswer()
    #context_dict = {'boldmessage': "Sum messages"}
    return render(request, 'tedbot/index.html', {"answer": answer, "question":question})

非常感谢任何指点


Tags: djangoanswernamefromimportdivformsend
1条回答
网友
1楼 · 发布于 2024-07-03 07:23:54

您必须将您的问题和答案存储在数据库中,因为当您单击按钮时,您将调用一个始终将最后一个问题和答案发送到模板的视图

然后,当您的数据在数据库中时,您只需获取所有问题和答案并将其发送到模板,然后使用{%for%}显示

相关问题 更多 >