简体   繁体   English

Django-在新行中返回新表单输出,而不会覆盖先前的输出

[英]Django - Returning new form output in new line without overwriting previous output

I have been developing a bot using python and nltk and it works on the command line. 我一直在使用python和nltk开发一个机器人,它可以在命令行上运行。 Now I want to capture input using a web page, and I am transferring the logic to Django. 现在,我想使用网页捕获输入,并将逻辑转移到Django。 I will use a simpler example to demonstrate the problem I have. 我将使用一个更简单的示例来演示我遇到的问题。

In my index.html, I have a user entering a question into a single input box and upon clicking send, the view simply returns what they entered: their 'question' and 'answer' below the input box. 在index.html中,我有一个用户在一个输入框中输入一个问题,然后单击“发送”,该视图仅返回他们输入的内容:输入框下方的“问题”和“答案”。 The problem is when they enter a new question it overwrites the previous one, but I would want the new question and answer to appear below the previous one as in a chat thread. 问题是当他们输入一个新问题时,它会覆盖前一个问题,但是我希望新问题和答案像在聊天线程中一样显示在前一个问题的下方。

If maybe you do not get what I mean, I'll use images. 如果您可能听不懂我的意思,我将使用图像。

This happens: when the user enters first chat message. 发生这种情况: 当用户输入第一条聊天消息时。 Then when the user enters second chat message. 然后当用户输入第二个聊天消息时。

What I want is this: to have both of them as a thread and not two overwriting one 我想要的是: 将它们都作为线程而不是两个覆盖一个

I have just started using Django with this project and while with the cmd I could just use 我刚刚开始在这个项目中使用Django,而在cmd中,我可以使用

while True:

I am a bit stumped on how to do it using the views and templates in Django. 我对如何使用Django中的视图和模板感到有些困惑。

Here's my index.html template 这是我的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> 

My forms.py 我的forms.py

from django import forms

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

And my views.py 还有我的views.py

# -*- 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})

Any pointers are much appreciated. 非常感谢任何指针。

You have to store your questions and answers in database becouse when you click button you call a view which sends always last question and answer to template. 您必须将问题和答案存储在数据库中,因为当您单击按钮时,会调用一个视图,该视图始终将最后一个问题和答案发送到模板。

And then when your data is in database, you simply get all questions and answers and send it to template and show using {% for %} 然后,当您的数据在数据库中时,您只需获取所有问题和答案并将其发送到模板,然后使用{%for%}进行显示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM