我有一个表格分页,我想在本地存储中保存用户的输入。我已经设法在我的jquery脚本中获取此变量:
var fields = [< django.forms.boundfield.BoundField对象at 0x000000000E415D30>,< django.forms.boundfield.BoundField对象 在0x000000000E415400>,< django.forms.boundfield.BoundField 对象位于0x000000000E415390>, < django.forms.boundfield.BoundField对象at 0x000000000E415AC8>,< django.forms.boundfield.BoundField对象 在0x000000000E415BE0>,< django.forms.boundfield.BoundField 对象位于0x000000000E415208>, < django.forms.boundfield.BoundField对象at 0x000000000E4158D0>,< django.forms.boundfield.BoundField对象 在0x000000000E4154E0>,< django.forms.boundfield.BoundField 对象位于0x000000000E415860>, < django.forms.boundfield.BoundField对象at 0x000000000E80DEF0>]
我的问题是我不知道如何保存这个字段对象'键/值到本地存储。有大量的页面,所以这是通用的,我不应该使用字段名来访问字段值,只是这个字段对象。
我能做些什么来实现这个目标?
答案 0 :(得分:0)
forms.py file
=============
class ControlElementForm(BaseForm):
rundescriptor = forms.CharField(max_length=50, initial='rsmgui', label='Run Descriptor')
typechoices = [(0, 'hour'), (1, 'minute'), (2, 'day')]
tstype = forms.ChoiceField(choices=typechoices, label='Time Step',
widget=forms.Select())
lenchoices = [(0, 24), ]
tslen = forms.ChoiceField(choices=lenchoices, label='Time Length',
widget=forms.Select())
svchoices = [(0, "PETSC"),]
solver = forms.ChoiceField(choices=svchoices, label="Solver Package", help_text="Portable, Extensible Toolkit for Scientific Computation")
mchoices = [(0, "gmres"),]
method = forms.ChoiceField(choices=mchoices, label="Solver Method")
alpha = forms.FloatField(max_value=1.0, min_value=0.0, initial=0.900)
cchoices = [(0, "ilu"),]
precond = forms.ChoiceField(choices=cchoices, label='Pre Condition') # TODO ask the meaning
uchoices = [(0, "english"),]
units = forms.ChoiceField(choices=uchoices)
views.py file
============
def rsminput(request):
if request.POST:
form = XMLElementsForm(request.POST)
# process the data in form.cleaned_data as required
if form.is_valid():
# do something
messages.success(request, 'Your data was saved successfully!')
return render(request, 'rsmgui_home.html')
else:
messages.error(request, form.errors)
boundfield_list = tuple(XMLElementsForm())
page = request.GET.get('page', 1)
paginator = Paginator(boundfield_list, 10)
try:
elements = paginator.page(page)
except PageNotAnInteger:
elements = paginator.page(1)
except EmptyPage:
elements = paginator.page(paginator.num_pages)
return render(request, 'rsmgui_input.html', {'elements': elements })
urls.py file
=============
from django.conf.urls import url, include
from django.views.decorators.cache import cache_page
from .views import rsminput
urlpatterns = [
url(r'^input$', rsminput, name='rsminput'),
]
rsmgui_input.html file
==========================
{% extends "rsmgui_nav.html" %}
{% block title %}RSM Input{% endblock %}
<script src="{% static 'jquery/jquery-3.3.1.min.js' %}"></script>
<script src="{% static 'jquery/jquery-ui.min.js' %}"></script>
{% block center_menu %}
<form class="form-horizontal" id="input-form" role="form" action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
<div class="row">
<div class="col col-sm-7">
<p>Model Input: <Control Elements></p>
</div>
<div class="col col-sm-5 text-right">
<div class="btn-group" role="group" aria-label="input-xml">
<button id="input-submit" type="submit" class="btn btn-default btn-trans" data-toggle="tooltip" data-placement="bottom" title="save">
<span class="glyphicon glyphicon-save"></span>
</button>
<button type="button" class="btn btn-default btn-trans" data-toggle="tooltip" data-placement="bottom" title="reset">
<span class="glyphicon glyphicon-minus"></span>
</button>
<button id="input-close" type="button" class="btn btn-default btn-trans"
data-toggle="tooltip" data-placement="bottom" title="close">
<span class="glyphicon glyphicon-remove"></span>
</button>
</div>
</div>
</div>
<div >
{% for boundfield in elements %}
<div class="form-group">
<label class="control-label col-sm-5" style="text-align:right;"
for="{{boundfield.id_for_label}}">{{ boundfield.label }}
</label>
<div class="col-sm-7" id="{{boundfield.id_for_label}}"
aria-describedby="{{boundfield.id_for_label}} | addstr:ex">{{ boundfield }}
</div>
</div>
{% if boundfield.help_text %}
<p class="col-sm-offset-5 col-sm-7"> <small style="color: grey">{{ boundfield.help_text|safe }}</small></p>
{% endif %}
{% for error in boundfield.errors %}
<div class="col-sm-offset-2 col-sm-10"><span class="text-danger small">{{ error }}</span></div>
{% endfor %}
{% endfor %}
</div>
<!-- For pagination-->
<div class="text-center">
{% if elements.has_other_pages %}
<ul class="pagination">
{% if elements.has_previous %}
<li><a href="?page={{ elements.previous_page_number }}">«</a></li>
{% else %}
<li class="disabled"><span>«</span></li>
{% endif %}
{% for i in elements.paginator.page_range %}
{% if elements.number == i %}
<li class="active"><span>{{ i }} <span class="sr-only">(current)</span></span></li>
{% else %}
<li><a href="?page={{ i }}">{{ i }}</a></li>
{% endif %}
{% endfor %}
{% if elements.has_next %}
<li><a href="?page={{ elements.next_page_number }}">»</a></li>
{% else %}
<li class="disabled"><span>»</span></li>
{% endif %}
</ul>
{% endif %}
</div>
</form>
{% endblock %}
<script type="text/javascript">
$(document).ready(function() {
function loadInputLocalStorage(){
$('#input-form :input').each(function(){
var key = $(this).attr('id');
if(localStorage.getItem(key)) {
var value = localStorage.getItem(key);
$(this).val(value);
}
});
}
loadInputLocalStorage();
function saveInputLocalStorage() {
$('#input-form :input').each(function(){
var id = $(this).attr('id');
var value = $(this).val();
localStorage.setItem(id, value);
});
}
$('#input-submit').on('click', function(){
saveInputLocalStorage();
});
}); // end of document
</script>