Monday 15 August 2011

django - Detect row difference (view or model)? -


I would like to display a list of publications on my website; However, I'd like to dialect a headline stating the year for each set of publications published on that particular year.

So I would like to be like this for my final result (my reputation 1 :( I can not upload the image):

My There is a table with three columns; the ID (primary key), the title (title of the article), and date (date of publication)

in my template file; print header before every article:

  all_publications% {% for curr_pub in} & lt; h1 & gt; {{curr_pub.date.year}} & lt; / h1 & Gt; & lt; li & gt; {{curr_pub.title}} & lt; / li & gt; {% Endfor%}   

I passing all_publications by ordered '- that the current row is strong > Year , which means curr_pub with the previous one and check whether it is different or not, and accordingly print the header (or not print). Looks like I can not do this in a template.

I am new to Django and Python, I was not sure what to do and it The place where I want to help; My thoughts were the following:

1) model Add a function ( def is_it_first_publication (self): ) that returns true < / Code> or false - but I was not really able to do this: | - ... and I'm not sure if I need to do this or not!

2) Second, in view , and the additional template in the variable (s); Here's an example (which works fine for this case):

Keeping in mind:

  Def publications (request) : All_publications = publishing .objects.order_by ('- date') after_first_row_flag = false f_year = 'null' list_of_ids_of_first_publications = [] for all_publications for curr_pub: if after_first_row_flag: if curr_pub.date.year = f_year: list_of_ids_of_first_publications.append (curr_pub. id) f_year = rest curr_pub.date.year: the first (or earliest) year of publication # # list_of_ids_of_first_publications.append (curr_pub.id) f_year = curr_pub.date.year after_first_row_flag = true template = loader.get_template ( 'counters / p Rkashnon must be added .html ') reference = Anurodcontekst (request, {' all_publications': all_publications, 'list_of_first_publications': list_of_ids_of_first_publications,}) HttpResponse (template.render (context)) back   < p>  template:   
  list_of_first_publications%} & the B.id in curr_pu {% Sbi_pblikeshns to curr_pub%} {% lt; H1 & gt; {{Curr_pub.date.year}} & lt; / H1> {% Endif%} & lt; Li & gt; Placeholder for [Curr_pub.title] & lt; / Li & gt;  regroup  created in the filter {% Endfor%}    

You can do this without your view without seeing it. According to the documentation, it is complicated.

  {% As per the date, make all the federations yearly. Year_list%} {% year_list%} for the year & lt; H1 & gt; {{Year.grouper}} & lt; / H1> 
  • gt; To publish in {% Year.list%}; {{Publication.title}} & lt; / Li & gt; {% Endfor%} {% endfor%}

  • No comments:

    Post a Comment