如何在 PythonAnywhere 上将变量打印到 Bash 控制台?

How to I print variables to the Bash console on PythonAnywhere?

我现在有一个奇怪的问题,我在 Django 管理中的操作菜单功能中插入到我的 MySQL 数据库中的文本在左侧附加了一个 'n' 字符细绳。这只发生在 PythonAnywhere 上。我主要在我自己的机器上的本地主机服务器上开发应用程序,但那里没有显示错误。

我想知道的主要是如何将变量打印到 Bash 控制台实例,以便我可以诊断发生了什么。我尝试在操作函数中编写 print(word),但输出没有回显到我在我的应用程序 运行 中打开的虚拟环境 (django18) 中打开的 Bash 控制台。在我可以将变量打印到控制台之前,我必须执行其他步骤吗?

或者,如果有人知道为什么 'n' 会被预先添加到我插入数据库的文本中,这将是非常有用的信息。起初我以为是因为输入变量是unicode文本,但是在插入它们之前将它们编码为utf-8没有区别。

我的动作函数的完整源代码如下:

from django.contrib import admin
from django.forms import Textarea
from django.db import models

from .frequency import VocabularyProcess
from .models import School, Grade, Book, Bookmark, Word


# Action menu item
def process_content(modeladmin, request, queryset):
    # Get the text from the database bookmark item
    table = queryset.values_list('id', 'content')
    # Process each record in the queryset
    for record in table:
        bookmark_id = int(record[0])
        content_text = str(record[1].encode('ascii', 'ignore')).lower()
        # make it into a list of lines
        content_list = content_text.splitlines()
        # Process the text into a dictionary of words with counts
        vocabulary_obj = VocabularyProcess(content_list)
        words = vocabulary_obj.get_frequency_dict()
        # Update or create the words and counts in the Words table.
        for word, count in words.items():
            # Encode to utf-8 for compatibility with current DB char encoding
            word = word.encode('utf-8')
            if len(word) <= 20:  # Ensure the word will fit in the DB record
                Word.objects.update_or_create(word=word, \
                           bookmark_id=bookmark_id, defaults={'count': count})
print >>sys.stderr, 'message'

但是,正如 Daniel Roseman 指出的那样,正确的解决方案是使用 Python 记录器。这就是 log.debug() 的目的。

您无法从 Web 应用程序打印到控制台,它们彼此相距甚远。您可以打印到 Web 应用程序的日志文件。您发送到 stderr 的任何输出最终都会出现在您的日志中。根据您的框架对日志记录所做的操作,它可能在您的服务器日志中,也可能在您的错误日志中。