【发布时间】:2015-04-03 11:42:46
【问题描述】:
我正在使用 Django 为一个班级创建一个网络应用程序。下面 HTML 的 sn-p 是我认为可能导致我的问题的原因。
<form action="press" method="POST">
<table>
<tr>
<td colspan="4">
{% if output %}
<input id="output" name="output" type="text" value="{{output}}" readonly>
{% else %}
<input id="output_hidden" name="output" type="text" value="0" readonly>
{% endif %}
<input type="hidden" name="buttVal" value="{{buttVal}}">
<input type="hidden" name="currVal" value="{{currVal}}">
<input type="hidden" name="prevVal" value="{{prevVal}}">
<input type="hidden" name="math" value="{{math}}">
{% csrf_token %}
</td>
我使用隐藏字段来存储值以保持会话无状态(分配点)。我正在尝试调试我的隐藏字段,但是,在我启动我的应用程序并触发下面的第一个 if 语句后,我不再能够查看网页源(通过右键单击网页并单击“查看源” )。当我尝试这样做时,它首先显示“文档已过期”,但它允许我再次尝试请求。当我再次尝试时,下面的 html 显示为源代码。
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="robots" content="NONE,NOARCHIVE">
<title>MultiValueDictKeyError at /press</title>
<style type="text/css">
html * { padding:0; margin:0; }
body * { padding:10px 20px; }
body * * { padding:0; }
body { font:small sans-serif; }
body>div { border-bottom:1px solid #ddd; }
h1 { font-weight:normal; }
h2 { margin-bottom:.8em; }
h2 span { font-size:80%; color:#666; font-weight:normal; }
h3 { margin:1em 0 .5em 0; }
h4 { margin:0 0 .5em 0; font-weight: normal; }
code, pre { font-size: 100%; white-space: pre-wrap; }
table { border:1px solid #ccc; border-collapse: collapse; width:100%; background:white; }
tbody td, tbody th { vertical-align:top; padding:2px 3px; }
thead th { padding:1px 6px 1px 3px; background:#fefefe; text-align:left; font-weight:normal; font-size:11px; border:1px solid #ddd; }
tbody th { width:12em; text-align:right; color:#666; padding-right:.5em; }
table.vars { margin:5px 0 2px 40px; }
table.vars td, table.req td { font-family:monospace; }
table td.code { width:100%; }
table td.code pre { overflow:hidden; }
table.source th { color:#666; }
table.source td { font-family:monospace; white-space:pre; border-bottom:1px solid #eee; }
ul.traceback { list-style-type:none; color: #222; }
ul.traceback li.frame { padding-bottom:1em; color:#666; }
ul.traceback li.user { background-color:#e0e0e0; color:#000 }
div.context { padding:10px 0; overflow:hidden; }
div.context ol { padding-left:30px; margin:0 10px; list-style-position: inside; }
div.context ol li { font-family:monospace; white-space:pre; color:#777; cursor:pointer; }
div.context ol li pre { display:inline; }
div.context ol.context-line li { color:#505050; background-color:#dfdfdf; }
div.context ol.context-line li span { position:absolute; right:32px; }
.user div.context ol.context-line li { background-color:#bbb; color:#000; }
.user div.context ol li { color:#666; }
div.commands { margin-left: 40px; }
div.commands a { color:#555; text-decoration:none; }
.user div.commands a { color: black; }
#summary { background: #ffc; }
#summary h2 { font-weight: normal; color: #666; }
#explanation { background:#eee; }
#template, #template-not-exist { background:#f6f6f6; }
#template-not-exist ul { margin: 0 0 0 20px; }
#unicode-hint { background:#eee; }
#traceback { background:#eee; }
#requestinfo { background:#f6f6f6; padding-left:120px; }
#summary table { border:none; background:transparent; }
#requestinfo h2, #requestinfo h3 { position:relative; margin-left:-100px; }
#requestinfo h3 { margin-bottom:-1em; }
.error { background: #ffc; }
.specific { color:#cc3300; font-weight:bold; }
h2 span.commands { font-size:.7em;}
span.commands a:link {color:#5E5694;}
pre.exception_value { font-family: sans-serif; color: #666; font-size: 1.5em; margin: 10px 0 10px 0; }
</style>
<script type="text/javascript">
//<!--
function getElementsByClassName(oElm, strTagName, strClassName){
// Written by Jonathan Snook, http://www.snook.ca/jon; Add-ons by Robert Nyman, http://www.robertnyman.com
var arrElements = (strTagName == "*" && document.all)? document.all :
oElm.getElementsByTagName(strTagName);
var arrReturnElements = new Array();
strClassName = strClassName.replace(/\-/g, "\-");
var oRegExp = new RegExp("(^|\s)" + strClassName + "(\s|$)");
var oElement;
for(var i=0; i<arrElements.length; i++){
oElement = arrElements[i];
if(oRegExp.test(oElement.className)){
arrReturnElements.push(oElement);
}
}
return (arrReturnElements)
}
function hideAll(elems) {
for (var e = 0; e < elems.length; e++) {
elems[e].style.display = 'none';
}
}
window.onload = function() {
hideAll(getElementsByClassName(document, 'table', 'vars'));
hideAll(getElementsByClassName(document, 'ol', 'pre-context'));
hideAll(getElementsByClassName(document, 'ol', 'post-context'));
hideAll(getElementsByClassName(document, 'div', 'pastebin'));
}
function toggle() {
for (var i = 0; i < arguments.length; i++) {
var e = document.getElementById(arguments[i]);
if (e) {
e.style.display = e.style.display == 'none' ? 'block': 'none';
}
}
return false;
}
function varToggle(link, id) {
toggle('v' + id);
var s = link.getElementsByTagName('span')[0];
var uarr = String.fromCharCode(0x25b6);
var darr = String.fromCharCode(0x25bc);
s.innerHTML = s.innerHTML == uarr ? darr : uarr;
return false;
}
function switchPastebinFriendly(link) {
s1 = "Switch to copy-and-paste view";
s2 = "Switch back to interactive view";
link.innerHTML = link.innerHTML == s1 ? s2: s1;
toggle('browserTraceback', 'pastebinTraceback');
return false;
}
//-->
除此之外,我不确定发生了什么。我试着用谷歌搜索这个问题,但我对这种情况缺乏了解,让我找不到任何有用的东西。希望有人能指出我做的不对的地方。
-- 编辑-- 这是我的 views.py 文件。
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def home_page(request):
index = 'calculator/calculator.html'
return render(request,index,{})
def press(request):
context = {}
operators = ['plus','minus','divide','multiply']
index = 'calculator/calculator.html'
## Discerns from a number and an operator press
if 'val' in request.POST and is_int(request.POST['val']):
if request.POST['output'] == '0':
context['output'] = request.POST['val']
context['buttVal'] = request.POST['val']
context['currVal'] = request.POST['val']
elif request.POST['currVal'] == '0':
context['output'] = request.POST['val']
context['buttVal'] = request.POST['val']
context['currVal'] = request.POST['val']
else:
newOutput = request.POST['output'] + request.POST['val']
context['buttVal'] = request.POST['val']
context['currVal'] = newOutput
context['output'] = newOutput
context['math'] = request.POST['math']
context['prevVal'] = request.POST['prevVal']
elif request.POST['val'] == 'clear':
context['output'] = 0
context['buttVal'] = 0
context['currVal'] = 0
context['prevVal'] = 0
context['math'] = ''
elif request.POST['val'] in operators:
print request.POST
if request.POST['math'] != '':
print "poop"
if request.POST['math'] == 'plus':
result = int(request.POST['currVal']) + int(request.POST['prevVal'])
context['prevVal'] = result
context['output'] = result
if request.POST['math'] == 'minus':
result = int(request.POST['currVal']) - int(request.POST['prevVal'])
context['prevVal'] = result
context['output'] = result
if request.POST['math'] == 'multiply':
result = int(request.POST['currVal']) * int(request.POST['prevVal'])
context['prevVal'] = result
context['output'] = result
if request.POST['math'] == 'divide':
result = int(request.POST['currVal']) / int(request.POST['prevVal'])
context['prevVal'] = result
context['output'] = result
if request.POST['val'] == 'equal':
pass
else:
context['prevVal'] = request.POST['currVal']
#context['math'] = request.POST['val']
context['output'] = request.POST['output']
context['math'] = request.POST['val']
context['buttVal'] = request.POST['buttVal']
context['currVal'] = 0
else:
context['output'] = request.POST['output']
context['buttVal'] = request.POST['buttVal']
context['math'] = request.POST['math']
context['prevVal'] = request.POST['prevVal']
## Submits POST
return render(request, index, context)
## Helper functions
def is_int(string):
try:
int(string)
return True
except ValueError:
return False
【问题讨论】:
-
错误在python代码的某个地方。请显示视图的来源。此外,浏览器中堆栈跟踪的最后一部分应指向发生错误的实际行。
-
@catavaran,所以我的网络应用程序正常运行而不会产生任何错误。当我与它交互时,没有产生传统的 django 错误(指向一条线)。这是让我感到困惑的一部分。我可以判断出问题的唯一原因是当我尝试手动查看网页的源代码时。您所指的观点的来源是什么?是网页源代码还是views.py 代码?如果是源码那么就是上面第二个大代码sn-p。我没有复制整个东西,因为它很大。
-
第一个 sn-p 的 html 代码无法生成
KeyError。所以错误出现在views.py或从views.py调用的代码中。您必须显示映射到此 url 的视图函数的代码。 -
没问题,在我添加之前,我想确保这是您想要看到的。这很难看,只是预先声明。
标签: python html django post hidden-field