Create views of model instances in Django -


i looking conceptual guidance on how should go implementing following system.

i have search facility can return list of model instances, e.g. if model cats, search returns list of cats in database.

what achieve assign url each of instances of model, such user can click on when search results returned , taken page populated other relevant information pertaining instance of model. system must dynamic, database may contain hundreds or thousands of entries.

as understand it, require following:

  • a dynamic assignment of urls each instance of model
  • a url config passes unique url of each instance view
  • a view can decipher instance of model url points , dynamically populates template relevant information

however, new user of django , python in general, not sure how fit together. i uncertain how assign , route urls. guidance appreciated.

thank-you in advance!

p.s. please note i'm not looking code, perhaps workflow or other representation of how system work.

///////edit///////

hi guys, responses. if ok, ask additional before close question out.

i took @ answers , had quick stab @ creating solution. however, believe having issues get_absolute_url function, seems returning either nothing or empty string (cant tell).

here rudimentary code:

models.py

class cats(models.model):     name = models.charfield(max_length=20)      @models.permalink     def get_absolute_url(self):         return "catspage", self.name 

results.html

{% result in page.object_list %}     <p>         <a href="{{ result.object.get_absolute_url }}">{{ result.object }}</a>     </p> {% empty %}         <p>no results found.</p> {% endfor %} 

urls.py

urlpatterns = patterns('',     url(r'^catsview/(?p<name>-[w]+)/$', views.catsview.as_view(), name='catspage')) 

views.py

from django.views import generic   class catsview(generic.detailview):     model = cats     template_name = 'catsdb/object.html' 

so, way works view outputs page called results.html results search function, i.e. result.object above.

so, in template, each result should link absolute url, defined in models.py. there generic view detailview which, through url config, should route absolute urls page object.html.

the bug: links blank, url empty.

django has built in views trying do!

combining listview , detailview should fulfill requirements. documentation links provide urlconf examples.

you can use listview cat instances in system, , render them. django has convention of using model instance method called get_absolute_url link view individual instance. link should point detailview set render cat instances.

  1. use listview load cat instances
  2. render cat instances link detailview using get_absoulte_url
  3. use detailview render individual cat instance.

the important thing is, django provides tools (in form of classbasedviews) accomplish outlined in question


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -