繁体   English   中英

如何使用 django 模板在一个 for 循环中同时遍历两个列表,以在聊天机器人应用程序中实现

[英]How can I traverse through two list concurrently in one for loop using in django templates to be implemented in a chatbot app

所以我正在尝试一个聊天机器人应用程序,用户将在其中接受用户字符串响应,然后聊天机器人会抛出一个回复。 这意味着它应该能够在一个for loop中遍历两个字符串列表user_responsechat_response 我遇到的问题是我不知道如何在 Django 中实现它,这适合我需要实现它的方式。 我应该这样实现它:

    {% for chat in user_chat#<-not sure here %}
        <div class="chat self animated slideInUp fast">
                <div class="user-photo">
                    <img src="{% static 'images/account_200px.png'%}"alt="Self"></div>
                    <div class="chat-message">{{ user_chat }}
                </div>
        </div>
        <div class="chat friend animated slideInUp fast">
            <div class="user-photo">
                 <img src="{% static 'images/icon-at.png'%}"alt="Dog"></div>
                 <div class="chat-message">
                    {{ response #<-the response from system|safe}}
                 </div>
           </div>
   {% endfor %}

这意味着在循环的一个实例中,我应该能够呈现具有不同类的两个 div,这些类显然具有来自两个不同字符串列表的不同消息。 喜欢

for loop{

      render(user_chat)
      render(system_response)
}

这反过来应该产生这种响应鉴于:

user_chat = ["Hi", "I am okay", "good!"]
system_response = ["How are you", "How Okay are you?", "Oh that's great to hear"]

会让

Hi
How are you
I am okay
How Okay are you?
good!
Oh! that's great to hear

我已经尝试过这个解决方案,如何在 django 模板中设置两个 for 循环来发送和接收聊天?

但是,可能是由于我的限制,或者可能是因为解决方案的实现方式不符合我正在执行的程序的规范,我未能有效地做到这一点,只产生了这个:

Hi
Hi
I am okay
I am okay
good!
good!
How are you
How are you
How Okay are you?
How Okay are you?
Oh! that's great to hear
Oh! that's great to hear

我做错什么了吗? 有人可以告诉我解决方法吗?

这是我实施解决方案后的 html

{% for chat in message %}
                <div class="chat self animated slideInUp fast">
                    <div class="user-photo"><img src="{% static 'images/account_200px.png'%}"alt="Self"></div>
                    <div class="chat-message">{{ chat }}</div>
                </div>
                <div class="chat friend animated slideInUp fast">
                    <div class="user-photo"><img src="{% static 'images/icon-at.png'%}"alt="Dog"></div>
                    <div class="chat-message">{{ chat|safe}}</div>
                </div>
                {% endfor %}

这是我现在的views.py

from django.shortcuts import render
import operator
from itertools import chain

# Create your views here.
def messenger_view(request):
    chat = ["hi","ho","hey"]
    response =  ["hille","hoe","hey0"]
    message = chain(chat, response)
    context = {
        "lists":["hi","ho","hey"],
        "message": message
    }
    return render(request, "chatbot/index.html",context)

谢谢!

如果要创建两个列表的列表。 您可以 zip 并迭代它们:

a = [1,3,5,7,9,11]
b = [2,4,6,8,10]
c = []
for i, j in zip(a,b):
  c.extend([i,j])
#to handle uneven number of responses
if len(a)>len(b): c.append(a[-1])
print(c)

output:[1,2,3,4,5,6,7,8,9,10,11]

PS:计算 c 的更简洁的方法是嵌套列表理解,但我认为 for 循环更容易阅读:

c = [message for pair in zip(a,b) for message in pair]

暂无
暂无

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

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