Entering links in list_detail.object_list in list_detail.object_detail

I started using Django and moved on to general concepts. Great architecture! Well, the docs are great, but for an absolute beginner, this is a bit like unix docs, where they make the most sense when you already know what you are doing. I searched and can’t find it specifically, that is, how do you customize the object_list template so that you can click on an entry on the rendering screen and get the object_ detail?

Works. The reason I ask is to check if I am taking a reasonable route, or is there some better, simpler Djangoish way?

I have a model that has unicode so that I can identify my records in the database in a human readable form. I want to click the link on the page generated by object_list to go to the page object_detail. I understand that a good way to do this is to create a system in which the part URL looks like http://www.example.com/xxx/5/ , which would call the page detail 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 created a template page for a list view containing the following:

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

Here object_list appears from the general view of list_detail.object_list. The for loop processes a list of object_list objects. In each row, I create an anchor in html that references the desired href, "/ inpatient-detail / nn /", where nn is the id field of each row in the database table. The displayed link is a unicode string, which is therefore a clickable link. I created templates and it works fine.

So, am I going in the right direction? It looks like it will be easy to expand to be able to add and edit links in the template.

Is there a general view that the model uses to create the detail page? I used the ModelForm helper from django.forms to create a form object that is great for creating an input form (with automatic validation! Wow, which was cool!), So is there something similar for creating a detail view page?

Steve

0
source share
1 answer

If you are on django <1.3 then what you are doing is basically perfect. These general views are pretty good for quickly creating pages. If you are using django 1.3, you will want to use generic class-based views. As soon as you get a pen on those with whom they are crazy.

Just note that I have that you should use the {% url%} tags in your templates instead of hardcodes. In your urls.conf file, define named URLs, for example:

 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> 

A new url tag is available in 1.3, which improves life even more.

+1
source

Source: https://habr.com/ru/post/891911/


All Articles