如何将对象从模板传递到Django中的视图? [英] How to pass objects from templates to views in django?

查看:48
本文介绍了如何将对象从模板传递到Django中的视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

必须先问过这个问题,但我找不到.

This must have been asked before but I can't find one.

我在视图中已检索到my_object = get_object_or_404(MyObject,id = obj_id).
我将其传递给模板.
现在,我想在用户单击链接时将相同的my_object传递给另一个视图.

I have retrieved my_object = get_object_or_404(MyObject, id=obj_id) in my view.
I pass it to a template.
Now I want to pass the same my_object to another view when user clicks a link.

<a href="/another_page/my_object.id">here</a>  

但是随后我将不得不在第二个视图中再次通过其id检索my_object.
我可以以某种方式发送整个my_object吗?

But then I'll have to retrieve my_object by it's id again in the second view.
Can I send the entire my_object somehow?

我看过一些示例,其中url_pattern具有\ d +或\ w +来接收整数或字符串变量,但是如何传递对象呢?

I've seen examples where url_pattern has \d+ or \w+ to receive integer or string variable but how to pass objects?.

推荐答案

否,您必须再次在详细视图中检索对象.编写模型的get_absolute_url并在模板中调用它.

No, You must retrieve object again in detail view. Write get_absolute_url for model and call it in templates.

这篇关于如何将对象从模板传递到Django中的视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆