python - 使用django在网页上传递错误信息

标签 python html django

如何将错误异常消息传递到网页上。我正在使用 Atom 文本编辑器和 Django

View .py

try:
    netconnect = ConnectHandler(**devices)
except (AuthenticationException):
    re = print ('Authentication failed ' + ipInsert)
    return render(request,'first_app/forms.html', {'form': form, 'reprinting':re})

表单.html

{% if request.POST %}
<pre>{{ reprinting }}</pre>
{% endif %}

它打印 None 而不是在代码中打印错误消息。

注意:-尽管所描述的错误消息是在文本编辑器的命令行上打印的

完整代码请引用链接: full code

最佳答案

print(..) [Python-doc]函数返回内容。它将值打印到标准输出 channel ,并返回 None

如果你想让re包含错误信息,你需要给它赋值,比如:

try:
    netconnect = ConnectHandler(**devices)
except AuthenticationException:
    <b>re = 'Authentication failed {}'.format(ipInsert)</b>
    print(re)
    return render(request,'first_app/forms.html', {'form': form, 'reprinting':re})

请注意,您应该检查:

{% if <b>request.method == 'POST'</b> %}
{% endif %}

既然一个POST请求可以为空,那么它仍然是一个POST请求。

关于python - 使用django在网页上传递错误信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57037072/

相关文章:

python - Django Celery 任务没有完成并且一直处于待处理状态

python - Django celery - 在前一个实例完成的设定持续时间后启动周期性任务

python - 具有多个数据库的 Django,来自管理员中非默认数据库权限的模型

python - 如何手动安装python库

html - 下拉元素被隐藏!!! z-index 问题

python - 当单个键有多个值时拆分 Python 字典

javascript - 我能用 BeautifulSoup 在 javascript 后面得到一个 iframe 吗?

html - CSS div布局定位

python - 在 Python 中从 parent 那里访问 child 的 __dict__?

python - 如果 lat-lon 键不在其他 json 边界内,则删除 json 的功能