如何在会话变量中存储主键

时间:2020-01-07 23:18:30

标签: django django-templates django-views

我正在将constructor(props) { super(props); this.state = { //Date appointmentDate: new Date(moment()), modeAppointmentDate: 'date', showAppointmentDate: false, textAppointmentDate: moment().format('Do MMMM YYYY').toString(), //Time appointmentTime: new Date(moment()), appointmentEndTime: new Date(moment().add(1, 'hours')), modeAppointmentTime: 'time', modeEndTime: 'time', showAppointmentTime: false, showEndTime: false, textAppointmentTime: moment().format('h:mm a').toString(), textEndTime: moment().add(1, 'hours').format('h:mm a').toString(), //new timeSlots timeSlots: [], //new date, new start time, new end time initial_date: '', initial_start_time: '', initial_end_time: '', } } addDateTimeAppt = () => { let self = this; self.setState({ initial_date: self.state.appointmentDate, initial_start_time: self.state.appointmentTime, initial_end_time: self.state.appointmentEndTime, }); let date = self.state.initial_date; let start_time = self.state.initial_start_time; let end_time = self.state.initial_end_time; if (start_time < end_time){ if (date !== self.state.appointmentDate || ( ( start_time !== self.state.appointmentTime && start_time <= self.state.appointmentEndTime ) && ( end_time !== self.state.appointmentEndTime && end_time <= self.state.appointmentTime ) ) ) { self.addAppointmentApi(); //add appointment } else { ToastAndroid.show('Sorry! Appointment Time already booked', ToastAndroid.SHORT); } } else { ToastAndroid.show('Invalid appointment time', ToastAndroid.SHORT); } } 存储在会话变量中。

例如在template.html中:

object.pk

但是,当我在{{ request.session.myobject_pk }} <-- results in a numeric value e.g. 10 {{ myobject.pk }} <-- the same numeric value, 10 语句中调用这两个值时,主键与会话变量不匹配:

if

这是为什么?对象是整数,会话变量是字符串吗?如何解决此问题,以便在这样的条件逻辑语句中同时使用它们?

编辑

我在视图中设置了会话变量:

{% if myobject.pk == request.session.myobject_pk %} <-- results in a False 

1 个答案:

答案 0 :(得分:2)

POST词典中的值将始终为字符串,您需要将该值转换为int

post_myobject_pk = int(request.POST.get('field_name'))