Django:将对象从模板传递到视图
我想使用相同的模板来查看有关每个数据库对象的信息.我希望能够单击列表中的每个元素,并将其链接到包含有关此信息的页面.我在想,有一个比为每个唯一对象创建视图更简单的方法.
I want to use the same template to view information about each of my database objects. I would like to be able to click on each element in the list and have it link me to a page with info about it. I'm thinking there's an easier way than making a view for each unique object.
我在我的list.html上列出了所有数据库对象,如下所示:
I'm listing all of my database objects on my list.html like this:
{% for instance in object_info %}
<li><a href="object">{{ instance.name }}</a></li>
{% endfor %}
我的views.py具有此视图:
My views.py has this view:
def object_view(request):
data = Object.objects.filter(name="")
context={
'object_info':data
}
return render(request, "object.html", context)
我可以将每个 {{instance.name}}
传递给视图,并将其用作过滤器的变量吗?
Can I pass each {{ instance.name }}
to the view and use that as a variable for my filter?
首先,永远不要这样做:
Okay first off never do this:
data = Object.objects.filter(name="")
Django有一个 all()
函数,该函数将返回所有对象:
Django has an all()
function that will return all objects:
data = Object.objects.all()
第二,我希望 object_view
, data
, object_info
, object.html
不是您的实际变量名!如果是这样,请确保它们对您的应用程序有意义.
Secondly, I hope object_view
, data
, object_info
, object.html
are not your actual variable names! If so, please make sure they are meaningful to your application.
好,回到您的问题上.好了,您不需要为每个对象都创建一个视图.我假设< a href ="object"> ...</a>
应该引用将填充所选对象的新页面.
Okay back to your problem. Well, you don't need to make a view for every single object. I am assuming that <a href="object">...</a>
should refer to a new page that will be populated with the selected object.
如果是这样,您可能希望在< a>
标记中包含以下网址:/objects/object_id/
.
If so, you would want to have urls in the <a>
tags like this: /objects/object_id/
.
需要在 urls.py
中这样定义新的URL:
This new url needs to be defined like this in urls.py
:
urlpatterns += [
url(r'^objects/(?P<oid>[0-9]+)/$', views.object_specific_view, name='objects'),
]
注意 oid
url参数.我们将使用它来访问我们的特定对象.
Note the oid
url argument. We will be using it to access our specific object.
现在您的原始模板 list.html
应该如下所示:
Now your original template, list.html
, should look like:
{% for instance in object_info %}
<li><a href="{% url 'objects' oid = instance.id %}">instance.name</a></li>
{% endfor %}
在向 oid
url参数提供 instance.id
的地方,生成类似 objects/1/
或 objects/2/
等
Where we supply instance.id
to oid
url argument to produce something like objects/1/
or objects/2/
etc.
现在,这意味着您将只需要使用另一个模板再创建一个视图.
Now, this means that you will only need to create one more view with another template.
您的第二个视图 object_specific_view
:
def object_specific_view(request, oid): # The url argument oid is automatically supplied by Django as we defined it carefully in our urls.py
object = Object.objects.filter(id=oid).first()
context={
'object':object
}
return render(request, "specific_object.html", context)
现在,您只需要设计 specific_object.html
并访问 object
实例以显示特定对象的详细信息即可:).
Now you just need to design your specific_object.html
and access the object
instance to show details of a specific object :).