我有两个模型
def CarModel(models.Model):
type = models.CharField()
price = models.FloatField()
img = models.URLfield()
def WrongCar(models.Model):
wrong_type = models.CharField()
correct_type = models.CharField()
price = models.FloatField()
我有一个产品列表,比如说,使用该CarModel
的汽车,但如果汽车被分类错误,用户应该能够通过单击名为"正确类型"的按钮填写WrongCar
中的字段来更正它,然后用户被重定向到wrong_car
模板,其中WrongCar
的字段可以填写。发生这种情况时,wrong_type
应自动填写CarModel
中的值type
,以便用户仅应填写correct_type
和price
。我想我可以从request
对象中提取它,但我真的不知道如何做到这一点。
您可以通过存储在request.session(字典)中的重定向传递值,例如:
correct_car_view.py
request.session['wrong_car_type'] = 'type_you_want'
return redirect(reverse('wrong_car_url'))
wrong_car_view.py
try:
wrong_car_type = request.session['wrong_car_type']
except KeyError:
wrong_car_type = None
else:
del request.session['wrong_car_type']