【发布时间】:2014-09-16 12:38:32
【问题描述】:
我是 django 新手。
我使用 DetailView 查看页面上的图表列表:
class WidgetDetailView(FormMixin, DetailView):
model = Widget
template_name = 'board/detail.html'
form_class = SelectForm
period = 86400
def get_success_url(self):
return reverse('board:detail', kwargs={'pk': self.object.pk})
def get_context_data(self, **kwargs):
context = super(WidgetDetailView, self).get_context_data(**kwargs)
form_class = self.get_form_class()
context['form'] = self.get_form(form_class)
context['period'] = self.period
return context
def post(self, request, *args, **kwargs):
self.object = self.get_object()
form_class = self.get_form_class()
form = self.get_form(form_class)
if form.is_valid():
return self.form_valid(form)
else:
return self.form_invalid(form)
我有一个带有简单选择的简单表单:
class SelectForm(forms.Form):
CHOICES = (
('3600', u'1 h'),
('7200', u'2 h'),
('10800', u'3 h'),
('21600', u'6 h'),
('43200', u'12 h'),
('86400', u'1 d'),
('604800', u'1 w'),
('1209600', u'2 w'),
('2592000', u'1 m'),
('7776000', u'3 m'),
('15552000', u'6 m'),
('31536000', u'1 y'),
)
select = forms.ChoiceField(
widget = forms.Select(
attrs = {
'class': 'form-control input-sm',
'onchange': 'this.form.submit();',
}
),
choices = CHOICES,
label = u'Period'
)
HTML 代码:
{% extends 'board/base.html' %}
{% block content %}
<div class="page-header">
<h2>{{ widget.title }}</h2>
<p>{{ widget.description }}</p>
</div>
{% if widget.graph_set.all %}
<form method="post" action="." role="form" class="form-inline">
{% csrf_token %}
<label class="col-sm-1 control-label">Period</label>
<div class="form-group">
{{ form.select }}
</div>
</form>
{% endif %}
<div class="row">
{% for graph in widget.graph_set.all %}
<div class="col-md-6">
<h3>{{ graph.title }}</h3>
<img src="https://zabbix.net.eximb.com/zabbix/chart2.php?graphid={{ graph.graph_id }}&width=400&height=200&period={{ period }}" alt="{{ graph.title }}">
<p>{{ graph.description }}</p>
</div>
{% empty %}
<p>No graphs</p>
{% endfor %}
{% endblock %}
所以我想在从下拉列表中选择期间后重新加载页面。如何更新表单(提交后始终设置为默认)并将{{ period }} 传递给模板?
【问题讨论】: