从 django 表单获取会话值
Posted
技术标签:
【中文标题】从 django 表单获取会话值【英文标题】:Get session value from django form 【发布时间】:2022-01-22 09:48:12 【问题描述】:我有两种形式,一种是主要的,另一种在模式窗口中打开。 我需要我的第二个表单来获取公司会话值 (AIGN_EMP_ID),但是,当我从 Context 调用我的表单时,我不知道如何发送此值。
第一种形式.py
class MayoresForm(Form):
act_cuenta = ()
act_fechaini = DateField(
widget=DatePickerInput(
format=Form_CSS.fields_date_format,
options=Form_CSS.fields_date_opts,
attrs='value': Form_CSS.fields_current_date
),
label="Fecha desde: ",
required=True,
)
act_fechafin = DateField(
widget=DatePickerInput(
format=Form_CSS.fields_date_format,
options=Form_CSS.fields_date_opts,
attrs='value': Form_CSS.fields_current_date
),
label="Fecha hasta: ",
required=True,
)
def __init__(self, *args, **kwargs):
# --------------------------------------------------------------------------------------
self.AIGN_OPCIONES = kwargs.pop("AIGN_OPCIONES")
self.PERMISOS = [] # para recuperar los permisos de la tabla
__json_values = json.loads(json.dumps(self.AIGN_OPCIONES))
self.PERMISOS = recuperarPermisos(__json_values, 'con.transaccioncab')
# --------------------------------------------------------------------------------------
# Obtiene la variable de sesion desde la vista y la asigna al self
self.AIGN_EMP_ID = kwargs.pop("AIGN_EMP_ID")
super(MayoresForm, self).__init__(*args, **kwargs)
self.fields['act_cuenta'] = ChoiceField(label='Cuenta: ', choices=self.get_choices(), required=True)
for form in self.visible_fields():
# form.field.widget.attrs['placeholder'] = Form_CSS.fields_placeholder + form.field.label.lower()
form.field.widget.attrs['autocomplete'] = Form_CSS.fields_autocomplete
form.field.widget.attrs['class'] = Form_CSS.fields_attr_class
self.helper = FormHelper(self)
self.helper.form_method = 'post'
self.helper.form_id = Form_CSS.getFormID(self)
self.helper.attrs = Form_CSS.form_attrs
self.helper.form_tag = True
self.helper.form_error_title = Form_CSS.form_err_title
self.helper.form_class = Form_CSS.form_class
self.helper.label_class = 'col-sm-3 text-right form-control-sm'
self.helper.field_class = 'col-sm-6'
self.helper.layout = Layout(
Div(
DivHeaderWithButtons(instance_pk=None, remove_create=False, remove_delete=True, remove_print=True,
remove_cancel=False,
permisos=self.PERMISOS,
save_name=' Consultar'),
Div(
Div(
Div(
Div(
Div(
html("<h3 class='card-title'>Buscar por:</h3>"),
css_class='card-header'
),
Div(
Div(
'act_cuenta',
css_class='card-body'
),
Div(
'act_fechaini',
'act_fechafin',
css_class='card-body'
),
css_class="row"
),
css_class='card card-secondary'
),
css_class='col-sm',
),
css_class='row',
),
css_class='card-body'
),
css_class='card'
),
# Grid para el detalle del mantenmiento
Div(
DivGridHeaderWithButtons(grid_opts=get_MayoresDetForm(None)),
# aqui va el detalle
css_class='col-sm'
)
)
def get_choices(self):
all_tipoaux = Con_Cuenta.objects.filter(cue_grupo='M', cue_estado=1, emp_id=self.AIGN_EMP_ID).order_by(
'cue_codigov').values()
DOC = [(d['cue_codigov'], d['cue_codigov'] + ' - ' + d['cue_nombre']) for d in all_tipoaux]
return DOC
第二种形式.py
class MayoresPrintForm(Form):
act_cuenta_ini = ()
act_cuenta_fin = ()
act_fechaini = DateField(
widget=DatePickerInput(
format=Form_CSS.fields_date_format,
options=Form_CSS.fields_date_opts,
attrs='value': Form_CSS.fields_current_date
),
label="Fecha desde: ",
required=True,
)
act_fechafin = DateField(
widget=DatePickerInput(
format=Form_CSS.fields_date_format,
options=Form_CSS.fields_date_opts,
attrs='value': Form_CSS.fields_current_date
),
label="Fecha hasta: ",
required=True,
)
def __init__(self, *args, **kwargs):
super(MayoresPrintForm, self).__init__(*args, **kwargs)
self.fields['act_cuenta_ini'] = ChoiceField(label='Cuenta desde: ', choices=self.get_choices(), required=True)
self.fields['act_cuenta_fin'] = ChoiceField(label='Cuenta hasta: ', choices=self.get_choices(), required=True)
for form in self.visible_fields():
# form.field.widget.attrs['placeholder'] = Form_CSS.fields_placeholder + form.field.label.lower()
form.field.widget.attrs['autocomplete'] = Form_CSS.fields_autocomplete
form.field.widget.attrs['class'] = Form_CSS.fields_attr_class
self.helper = FormHelper(self)
self.helper.form_method = 'post'
self.helper.form_id = Form_CSS.getFormID(self)
self.helper.attrs = Form_CSS.form_attrs
self.helper.form_tag = True # esta etiqueta te dice si renderiza o no la etiqueta form
self.helper.form_error_title = Form_CSS.form_err_title
self.helper.form_class = Form_CSS.form_class
self.helper.label_class = 'col-sm-4 text-right form-control-sm'
self.helper.field_class = 'col-sm-7'
self.helper.layout = Layout(
Div(
Div(
# <h5 class="modal-title">Modal title</h5>
Div(
HTML("<h5 class='modal-title'><b>Buscar por:</b></h5>"),
css_class='modal-header'
),
Div(
Div(
'act_cuenta_ini',
'act_cuenta_fin',
css_class='card-body'
),
Div(
'act_fechaini',
'act_fechafin',
css_class='card-body'
),
css_class="row"
),
css_class='modal-body',
),
Div(
HTML("<button type='button' class='btn btn-secondary' data-dismiss='modal'>Cancelar</button>"),
Submit('PRINT', 'Descargar', css_class='btn btn-primary'),
css_class='modal-footer',
)
),
)
def get_choices(self):
all_tipoaux = Con_Cuenta.objects.filter(cue_grupo='M', cue_estado=1, emp_id=request.session.AIGN_EMP_ID).order_by(
'cue_codigov').values()
DOC = [(d['cue_codigov'], d['cue_codigov'] + ' - ' + d['cue_nombre']) for d in all_tipoaux]
return DOC
request.session.AIGN_EMP_ID 是我想要获取的值,重点是获取从我的第二个表单中获取会话的值。
view.py
import django
from django.db import connection
from django.shortcuts import render, redirect
from django.urls import reverse_lazy
from django.views.generic import FormView
from appls.con_man.forms import MayoresForm, get_MayoresDetForm, MayoresPrintForm
from vars import rs
class Con_MayoresCreateView(FormView):
form_class = MayoresForm
template_name = 'con_reportes/form_mayores.html'
success_url = reverse_lazy('con_man:con_mayores_rep')
# Asigna al kwargs la variable de session desde el formulario
def get_form_kwargs(self):
kwargs = super(Con_MayoresCreateView, self).get_form_kwargs()
kwargs.update('AIGN_EMP_ID': self.request.session['AIGN_EMP_ID'])
kwargs.update('AIGN_OPCIONES': self.request.session['AIGN_OPCIONES'])
return kwargs
def post(self, request, *args, **kwargs):
form = self.get_form()
extra_errors = []
request.POST._mutable = True
ctxt =
print(request.POST)
if 'PRINT' in request.POST:
mayores_form = MayoresPrintForm(request.POST)
if mayores_form.is_valid():
url = rs.ELEMENTS
url.ID = rs.REP_CON_MOVIMIENTOCUE_RANGO_ID
url.PARAMETROS = 'p_per_id': self.request.session['AIGN_PER_ID'],
'p_cue_codigov_d': mayores_form.data['act_cuenta_ini'],
'p_cue_codigov_h': mayores_form.data['act_cuenta_fin'],
'p_fecha_desde': mayores_form.data['act_fechaini'],
'p_fecha_hasta': mayores_form.data['act_fechafin']
r = url.getURL(url)
return redirect(r)
else:
ctxt['mayores_form'] = mayores_form
if 'CREATE' in request.POST:
try:
context = self.get_context_data(per_id=self.request.session['AIGN_PER_ID'],
cue_id=form.data['act_cuenta'],
f_ini=form.data['act_fechaini'],
f_fin=form.data['act_fechafin'],
**kwargs)
return render(request, self.template_name, context)
except django.db.utils.InternalError as e:
extra_errors.append(str(e))
return render(request, self.template_name, self.get_context_data(**ctxt))
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['page_title'] = 'Mayores'
context['url_list'] = self.success_url
context['MayoresPrintForm'] = MayoresPrintForm()
ret = []
ret.append(get_MayoresDetForm(kwargs).to_JSON())
context['grids_detalles'] = ret
return context
context['MayoresPrintForm'] = MayoresPrintForm() 是我需要发送会话值的地方。
模板.html
% extends 'body.html' %
% load static %
% load crispy_forms_tags %
% block headScripts %
<link href="//cdn.syncfusion.com/ej2/ej2-base/styles/material.css" rel="stylesheet">
<link href="//cdn.syncfusion.com/ej2/ej2-grids/styles/material.css" rel="stylesheet">
<link href="//cdn.syncfusion.com/ej2/ej2-buttons/styles/material.css" rel="stylesheet">
<link href="//cdn.syncfusion.com/ej2/ej2-popups/styles/material.css" rel="stylesheet">
<link href="//cdn.syncfusion.com/ej2/ej2-navigations/styles/material.css" rel="stylesheet">
<link href="//cdn.syncfusion.com/ej2/ej2-dropdowns/styles/material.css" rel="stylesheet">
<link href="//cdn.syncfusion.com/ej2/ej2-lists/styles/material.css" rel="stylesheet">
<link href="//cdn.syncfusion.com/ej2/ej2-inputs/styles/material.css" rel="stylesheet">
<link href="//cdn.syncfusion.com/ej2/ej2-calendars/styles/material.css" rel="stylesheet">
<link href="//cdn.syncfusion.com/ej2/ej2-splitbuttons/styles/material.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/dist/ej2.min.js" type="text/javascript"></script>
<script src="% static 'js/util_dev.js' %"></script>
% endblock %
% block content %
% crispy form %
<div class="modal fade" tabindex="-1" role="dialog" id="imprimir_id" aria-labelledby="exampleModalCenterTitle">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
% csrf_token %
% crispy MayoresPrintForm %
</div>
</div>
</div>
% endblock %
% block bodyFooterScripts %
<!-- Datepicker libs
-->
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.47/css/bootstrap-datetimepicker.css"
type="text/css" media="all" rel="stylesheet">
<link href="/static/bootstrap_datepicker_plus/css/datepicker-widget.css" type="text/css" media="all"
rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.9.0/moment-with-locales.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.47/js/bootstrap-datetimepicker.min.js"></script>
<script src="/static/bootstrap_datepicker_plus/js/datepicker-widget.js"></script>
<script>
$(document).ready(function ()
$("small[id^=hint_]").each(function (index, small_tag)
$("label[for='" + small_tag.id.substring(5, small_tag.id.length) + "']").attr('title', small_tag.innerText);
small_tag.remove();
);
);
</script>
# Convierte el grid de texto a un json para maniuplarlo #
grids_detalles|json_script:"gr_dets"
# importanto libreria js #
<script>
const gr_dets = get_json_from_script();
const elem_unimeds = gr_dets[0];
console.log("elemento de la grid");
console.log(elem_unimeds);
//Inicializa el grid (Detalle)
ej.grids.Grid.Inject(ej.grids.Edit, ej.grids.ExcelExport);
const grid = new ej.grids.Grid(elem_unimeds.gridOptions);
//agrega el exportExcel
grid.toolbarClick = function (args)
if (args['item'].id === 'MayorDetalle_id_excelexport')
grid.excelExport();
grid.appendTo("#" + elem_unimeds.grid_id);
//Se agrega un boton descargar pero sin efecto submit ya que este solo abrirá la ventana modal
document.getElementById('save_id').insertAdjacentHTML("afterend",
" <a id=\"download_id\" class=\"btn btn-dark btn-sm\"><i class=\"fas fa-file-pdf-o\"> </i> Descargar</a>");
document.getElementById("download_id").onclick = function ()
$('#imprimir_id').modal('show');
;
</script>
% endblock %
如果您能帮我提出任何建议,我将不胜感激。
【问题讨论】:
这个答案对你有帮助吗? ***.com/questions/61734025/… 【参考方案1】:目前,我们的 Syncfusion 控件无法满足您的要求。
问候,
悟空
【讨论】:
您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center。【参考方案2】:在get_context
中,我可以发送带有值会话MayoresPrintForm(data=self.request.session)
的变量数据
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['page_title'] = 'Mayores'
context['url_list'] = self.success_url
context['MayoresPrintForm'] = MayoresPrintForm(data=self.request.session)
ret = []
ret.append(get_MayoresDetForm(kwargs).to_JSON())
context['grids_detalles'] = ret
return context
但我必须更改 Post 方法才能从那里检索表单值,如下所示:request.POST['act_cuenta_ini']
这是最终代码:
import django
from django.db import connection
from django.shortcuts import render, redirect
from django.urls import reverse_lazy
from django.views.generic import FormView
from appls.con_man.forms import MayoresForm, get_MayoresDetForm, MayoresPrintForm
from vars import rs
class Con_MayoresCreateView(FormView):
form_class = MayoresForm
second_form_class = MayoresPrintForm
template_name = 'con_reportes/form_mayores.html'
success_url = reverse_lazy('con_man:con_mayores_rep')
# Asigna al kwargs la variable de session desde el formulario
def get_form_kwargs(self):
kwargs = super(Con_MayoresCreateView, self).get_form_kwargs()
kwargs.update('AIGN_EMP_ID': self.request.session['AIGN_EMP_ID'])
kwargs.update('AIGN_OPCIONES': self.request.session['AIGN_OPCIONES'])
return kwargs
def post(self, request, *args, **kwargs):
form = self.get_form()
extra_errors = []
request.POST._mutable = True
if 'PRINT' in request.POST:
url = rs.ELEMENTS
url.ID = rs.REP_CON_MOVIMIENTOCUE_RANGO_ID
url.PARAMETROS = 'p_per_id': self.request.session['AIGN_PER_ID'],
'p_cue_codigov_d': request.POST['act_cuenta_ini'],
'p_cue_codigov_h': request.POST['act_cuenta_fin'],
'p_fecha_desde': request.POST['act_fechaini'],
'p_fecha_hasta': request.POST['act_fechafin']
r = url.getURL(url)
return redirect(r)
if 'CREATE' in request.POST:
try:
context = self.get_context_data(per_id=self.request.session['AIGN_PER_ID'],
cue_id=form.data['act_cuenta'],
f_ini=form.data['act_fechaini'],
f_fin=form.data['act_fechafin'],
**kwargs)
return render(request, self.template_name, context)
except django.db.utils.InternalError as e:
extra_errors.append(str(e))
return render(request, self.template_name, self.get_context_data(context))
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['page_title'] = 'Mayores'
context['url_list'] = self.success_url
context['MayoresPrintForm'] = MayoresPrintForm(data=self.request.session)
ret = []
ret.append(get_MayoresDetForm(kwargs).to_JSON())
context['grids_detalles'] = ret
return context
【讨论】:
以上是关于从 django 表单获取会话值的主要内容,如果未能解决你的问题,请参考以下文章