I know it's a newbie question, but, I want to render a tabular list view of a model in one of my own templates.
It seems like it should be a no brainer to pick up the form from the admin interface rather than 开发者_C百科write my own. Have I got the wrong idea here?
As usual, the documentation seems to take me so far and...
If this is a sensible approach how do I go about it? If it is not - are there good snippets available?
I made the mistake when beginning with Django of thinking in terms of the django-admin for everything. Try treat the admin as "just another app". What are you trying to do? Do you just want to list the objects for a particular model? If so, that's quite easy to write it manually:
views.py
def list_objects(request):
return simple.direct_to_template(request,
template='folder/templ.html',
extra_context={
"objects" : MyModel.objects.all().order_by('some_field')
})
templ.html
...
<ul>
<li>
{{ object.title }}
</li>
</ul>
On the other hand, if you want to add functionality like add/delete/edit objects then it's a little more complicated. You need to create the forms for your models, along with a few more views to take care of the different tasks.
Django tries to make life easy when doing these repetitive by offering generic_views. Have a look though and you will see they take some of the pain out of having to write CRUD (create, update delete) interfaces etc.
精彩评论