Javascript Django-表格词典
我正在尝试实现一个表单字典,其中两个填充用我拥有的一些值初始化。我正在通过Jquery函数传递它,但是当它要将它添加到模板中时,Jquery函数有一个错误,当试图执行此Javascript Django-表格词典,javascript,jquery,python,django,dictionary,Javascript,Jquery,Python,Django,Dictionary,我正在尝试实现一个表单字典,其中两个填充用我拥有的一些值初始化。我正在通过Jquery函数传递它,但是当它要将它添加到模板中时,Jquery函数有一个错误,当试图执行此文档时,无法将属性“value”设置为null 我的代码在这里: view.py def exampleCaesar(request): if request.is_ajax() and request.method == "GET": form = caesarCipher(request.GET or Non
文档时,无法将属性“value”设置为null代码>
我的代码在这里:
view.py
def exampleCaesar(request):
if request.is_ajax() and request.method == "GET":
form = caesarCipher(request.GET or None)
if form.is_valid:
wordToEncrypt = request.GET.get('word')
wordToEncrypt = wordToEncrypt.upper()
wordLength = len(request.GET.get('word'))
key = request.GET.get('the_key')
print(wordLength)
print(key)
equations = {}
for x in range(wordLength):
exampleForm = caesarCipher(initial={'letterOfWord' : wordToEncrypt[x], 'key' : key})
##print(exampleForm)
if exampleForm.is_valid:
equations = (exampleForm)
print(equations)
context = { 'equations' : equations
}
return render(request, "content/exampleCaesar.html", context)
Javascript文件
$("#encryptButton").on({
click : function() {
var variable = document.getElementById('id_plaintext');
console.log(variable.value)
$.ajax( {
url: "/exampleCaesar",
type : "GET",
data: { CSRF: 'csrf_token',
word: $('#id_plaintext').val(),
the_key: $('#id_key').val()
},
success : function(example) {
$('#example1').show();
$('#example1').html(example);
document.getElementById('example1').value = example;
console.log(example);
}
}); //END OF Ajax
} //END OF FUNCTION
}); //END OF encryptButton
模板文件
{% for letterOfWord, key in equations.items %}
<form onsubmit="return false;" method="GET" class="exaSubmit" enctype="multipart/form-data">
{% csrf_token %}
<div id="example1" type="hidden">
( {{ letterOfWord }} + {{ keyToUse }} ) MOD 26 =
{{ letterToFill }} <button name="action" class="validateButton" value="validate"> Validate </button> <br>
</div>
</form>
{% endfor %}
{%对于letterOfWord,输入等式。项%}
{%csrf_令牌%}
({{letterOfWord}}+{{keyToUse}})MOD 26=
{{letterofill}}验证
{%endfor%}
我认为我没有用正确的方法填写字典。当我试图在控制台中打印出来以查看值时,只显示字段的名称,而不显示值。我的赌注在本节中,但不完全确定在我向其添加信息时如何实现这一点。要获得值而不是字段名,必须在模型中添加def\uu unicode\uuuu(self):return unicode(self.field\u name)。您能解释一下您的建议吗?似乎无法理解您引用的帖子。请显示完整视图和JS的完整上下文。是什么调用了ajax()
函数?不要骂人;这里不需要它。添加了您要求的信息。很抱歉,代码中的那个评论是我的一个朋友昨天晚上写的。