使用一个表单中的值作为重定向模板 (Django) 中另一个表单的值



我有两个模型


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_typeprice。我想我可以从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']

最新更新