将链接放在list_detail.object_list中到list_detail.object_detail [英] Putting links in list_detail.object_list to list_detail.object_detail

查看:37
本文介绍了将链接放在list_detail.object_list中到list_detail.object_detail的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始使用Django,并且可以直接进入通用视图.伟大的建筑!好吧,文档很棒,但是对于绝对的初学者来说,它有点像unix文档,当您已经知道自己在做什么时,它们才最有意义.我已经查看过并且找不到具体的内容,也就是说,如何设置object_list模板,以便可以单击渲染屏幕中的条目并获取object_detail?

以下工作.我问的原因是看我是否走了一条合理的路线,或者是否有更好,更Django的方式做到这一点?

我有一个定义了 unicode 的模型,以便可以以人类可读的形式识别数据库条目.我想单击object_list生成页面中的链接以转到object_detail页面.我知道,执行此操作的一种好方法是创建一个系统,其中详细信息的网址看起来像 http://www.example.com/xxx/5/,这将调出数据库中第5行的详细信息页面.所以,我只想出了以下几点,我的问题是我走对了吗?

我为列表视图制作了一个模板页面,其中包含以下内容:

 < ul>{object_list%中的住院病人所占的百分比}< li>< a href ='/inpatient-detail/{{aninpatient.id}}/'> {{aninpatient}}</a</li>{%endfor%}</ul> 

在此,object_list来自list_detail.object_list通用视图.for循环逐步遍历对象列表object_list.在每一行中,我都在html中创建一个锚点,该锚点引用了所需的href"/inpatient-detail/nn/",其中nn是数据库表中每一行的id字段.显示的链接是 unicode 字符串,因此是可点击的链接.我已经设置了模板,效果很好.

那么,我朝着正确的方向前进吗?似乎可以很容易地扩展此功能,以便能够在模板中也放置编辑和删除链接.

是否存在利用该模型创建详细信息页面的通用视图?我使用了django.forms的ModelForm helper来制作表单对象,这非常适合创建输入表单(具有自动验证功能!太酷了!),所以是否有类似的东西可以创建详细视图页面?>

史蒂夫

解决方案

如果您使用的是Django<1.3,那么您所做的基本上就是完美的.这些通用视图非常适合快速创建页面.如果您使用的是django 1.3,则需要使用基于类的通用视图.一旦处理了这些问题,它们就会变得非常好.

仅需注意的是,您应该在模板中使用{%url%}标签,而不是对URL进行硬编码.在您的urls.conf文件中,定义命名的url,例如:

  url('indoor-detail/(?P< inpatient_id> \ d +)/$','your_view',name ='inpatient_detail') 

和您的模板中(对于Django< 1.3):

 < a href ="{%url inpatient_detail inpatient_id = anin Patient.id%}"> ...</a> 

1.3中提供了一个新的url标签,可以进一步改善生活.

I've started using Django and am going right to generic views. Great architecture! Well, the documents are great, but for the absolute beginner it is a bit like unix docs, where they make the most sense when you already know what you're doing. I've looked about and cannot find this specifically, which is, how do you set up an object_list template so that you can click on an entry in the rendered screen and get the object_detail?

The following is working. The reason I'm asking is to see if I am taking a reasonable route or is there some better, more Djangoish way to do this?

I've got a model which has a unicode defined so that I can identify my database entries in a human readable form. I want to click on a link in the object_list generated page to get to the object_detail page. I understand that a good way to do this is to create a system where the url for the detail looks like http://www.example.com/xxx/5/ which would call up the detail page for row 5 in the database. So, I just came up with the following, and my question is am I on the right track?

I made a template page for the list view that contains the following:

<ul>
{% for aninpatient in object_list %}
<li><a href='/inpatient-detail/{{ aninpatient.id }}/'>{{ aninpatient }}</a></li>
{% endfor %}
</ul>

Here, object_list comes from the list_detail.object_list generic view. The for loop steps through the object list object_list. In each line I create an anchor in html that references the desired href, "/inpatient-detail/nn/", where nn is the id field of each of the rows in the database table. The displayed link is the unicode string which is therefore a clickable link. I've set up templates and this works just fine.

So, am I going in the right direction? It looks like it will be straightforward to extend this to be able to put edit and delete links in the template as well.

Is there a generic view that takes advantage of the model to create the detail page? I used ModelForm helper from django.forms to make the form object, which was great for creating the input form (with automatic validation! wow that was cool!), so is there something like that for creating the detail view page?

Steve

解决方案

If you're on django < 1.3 then what you are doing is basically perfect. Those generic views are quite good for quickly creating pages. If you're on django 1.3 you'll want to use the class based generic views. Once you get a handle on those they are are crazy good.

Only note I have is that you should use {% url %} tags in your templates instead of hardcoding urls. In your urls.conf file(s) define named urls like:

url('inpatient-detail/(?P<inpatient_id>\d+)/$', 'your_view', name='inpatient_detail')

and in your template (for django < 1.3):

<a href="{% url inpatient_detail inpatient_id=aninpatient.id %}">...</a>

In 1.3 a new url tag is available that improves life even more.

这篇关于将链接放在list_detail.object_list中到list_detail.object_detail的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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