On Mon, 2006-07-10 at 08:55 -0700, Tomas Jacobsen wrote: > Im trying to make a blog-like portfolio with finished projects I have > done. I want it to be easy to update when I have done something new, so > I thougth django seemed interesting for my webpage. I have been reading > some tutorials, and I finaly have manged to get in all the fields I > want in the admin, I have even manged to create categories. But it > stops there. I don't know how I get the posts inside admin to an > outside page. I have read the source code of lost-theories.com, but I > don't understand how I cant get the categories and entries out to the > "public". I understand I need to make views for my project, but I just > don't know what Im supposed to write there.
Django's view functions responsible for extracting the data you need from the database (using the models you created), putting it into a dictionary-like object (a Context) with the right names attached to each item and then passing that context to a Template object, which turns a template + context into some output (the HTML you end up sending to the user). So let's break that down into a few steps (and I'll use your example as a rough guide): (1) What information do you want to display? I'm not interested here in *how* it is going to be displayed (that is the template's job); rather, trying to work out what we need to extract from the database. - on one page (one view) you want a list of each available category, so you need to extract the distinct category names. - without testing it (just looking at your models), this would be categories = Category.objects.all() - in another view, you wanted the ten latest items for that category, so you need to be able to filter out the latest items for a category. Suppose you already have the category_id for this category (if you have the category instance, it will be category.id) Project.objects.filter(category = category_id)[:10] (I am exploiting the fact that you are already ordering by "-created" here, so they come out in the right order). (2) How will this information appear in the templates? Your cases are simple enough that the "object_list" generic view will probably work nicely for you. The queryset you pass to the generic view will be, for example, the Category.objects.all() queryset above (for your first page). If you do no customisation at all, your template will be passed a variable called "object_list" that will be the list of categories. So you can do things like "{% for object in object_list %}" to loop over this list and each "object" will be a Category object, so you can output it's name using "{{ object }}" and so forth. You therefore just need to write a template to display this. Again, if you do no customisation at all, the template needs to be called "<app_label>/category_list.html" (obviously, replace <app_label>), but you can customise that by passing in a "template_name" parameter to the generic view (this is all described in the generic view documentation, so I am not about to write it all out again here). Note that the generic views take care of wrapping up the data in a context and passing that into the template. (3) How is a particular URL request made to call this view? You need to set up a urls.py file that maps the URLs you want to use onto the view functions. There are examples at the top of the generic views documentation (although they are for date-based views, so they contain more information than you need to supply). You might choose, for example, to do something like this info = {'queryset': Category.objects.all()} urlpatterns = patterns('django.views.generic.list_detail', (r'^overview/$', 'object_list', info), ) Create a template with the right name and this should "just work", without too much trouble. So there are a few things that need to be done here, it looks like, but none of them are particularly difficult. Extracting the data from the database and into your models is described in the "database API" documentation, the generic views have their own documentation (called "generic views") and creating the templates is in the "template guide for HTML authors" documentation. The last half or so of tutorial #1 also gives some examples of using the database API to work with models. And tutorial #3 talks about putting that data into templates using views. If you have not worked your way through the tutorials, you are at a grave disadvantage. If you have gone through them, it might be worthwhile to revisit them again with this new information in hand. Maybe things will become a bit clearer. Regards, Malcolm --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users -~----------~----~----~----~------~----~------~--~---