2016-11-22 2 views
1

views 중 하나가 데이터베이스에 저장된 정보를 기반으로 여러 테이블을 표시하는 Django 프로젝트가 있습니다. 내가하고 싶은 무엇Django-보기로 만든 테이블에 'autonumber'열을 추가하는 방법은 무엇입니까?

def pipeline(request): 
    ... 
    tables = [] 
    def make_table(detailed_status, projects, status_view=False, first_table=False): 
     ... 
     table_context_map = { 
      Project.ds2: {'fields': [['date added',1], ['site visit date',1], ['initial exc VAT',1]]}, 
      ... 
      # Similar lines to populate the tables with data from the database 
      ... 
     } 
     table_context = table_context_map[detailed_status] 
     ... 
     table_context['fields'] = [['project name',1], ['town',1], ['postcode',1], ['contact name',1]] + table_context['fields'] 
     table = render_to_string('.../....html', table_context) 
    ... 
    return render(request, 'abc.html', context) 

는이 view에 의해 생성 된 각 테이블에 열에서, 그리고 테이블의 모든 행에 대해 해당 열에서 '일련 번호'를 삽입 다음과 같이 view가 정의됩니다. view이 실행되고 웹 페이지가로드 될 때마다 데이터베이스 쿼리를 기반으로 테이블이 동적으로 채워지 며, 작성된 각 테이블의 항목 목록에 번호를 지정하기 만하면됩니다.

어떻게하면됩니까? 파이썬 장고에 대해 알고 있기 때문에 도움이나 안내를 많이 주시면 감사하겠습니다.

편집

현재 웹 페이지에서이 테이블을 표시하는 HTML의 일부는 다음과 같습니다

<div class="content"> 
    {% block tables %} 
     {% for table in tables %} 

       {# Only shows table headers on first table (css first of type on multisection thead) #} 

       {{table}} 

     {% endfor %} 
    {% endblock tables %} 
</div> 

편집

HTML을에 전달 된 파일 render_to_string(...)보기의 구조는 다음과 같습니다.

{% load getters money_handling staticfiles utilities %} 

{% if projects %} 
    <div class="table-container m-t-lg"> 

     <table class="multisection pipeline left"> 
      <tr class="sub-summary"> 
       <th colspan="4"><a href="?detailed_status={{detailed_status}}"><h3 class="p-l-sm">{{detailed_status_str}}</h3></a></th> 
       {% if total_i %}<th>Initial exc VAT: {{total_i|money:"£"}}</th>{% endif %} 
       {% if total_u %}<th>Latest exc VAT: {{total_u|money:"£"}}</th>{% else %} 
       <th></th> 
       {% endif %} 
      </tr> 
     </table> 
     <table class="multisection pipeline left m-b-xl"> 
      <tr class="summary"> 
       <th style="width: 3em;"></th> 
       {% for field in fields %} 
        <th class="text-sm p-l-sm p-t-sm p-b-sm" style="width:{{widths|getval:forloop.counter0}}"> 
        {% if field.1 %} 
         {% if sort == field.0 and not reverse %} 
          <a href="?sort=-{{field.0}}&detailed_status={{detailed_status}}">{{field.0}}</a> 
         {% else %} 
          <a href="?sort={{field.0}}&detailed_status={{detailed_status}}">{{field.0}}</a> 
         {% endif %} 
        {% else %} 
         {{field.0}} 
        {% endif %} 
        </th> 
        {# Make all have the same number of columns (8) #} 
        {% if forloop.last %} 
         {% for i in ',,,,,,,,' %} 
          {% if forloop.counter|add:forloop.parentloop.counter0 < 11 %} 
           <th>&nbsp;</th> 

          {% endif %} 
         {% endfor %} 
         {% if detailed_status == "ds4"|ds %} 
          <th></th> 
         {% endif %} 
        {% endif %} 
       {% endfor %} 
      </tr> 
      {% with user.employee.full_name|is:'Nick Ross' as summary_link %} 

      {% for project in projects %} 
       <tr data-project-id="{{project.id}}" class="even {% if project.office == 2 %} col{% endif %}"> 
        {% with initial_details=project.initial_details survey=project.survey %} 
         {# Open lightbox #} 
         <td> 
          {# ERF(22/11/2016 @ 1450) Add a counter to display table row numbers #} 
          {% if user.is_superuser %} 
           <a class="gallery-loader" data-project-id="{{project.id}}"><i class="icon info"></i></a> 

           {% if forloop.first and first_table %} 
            <div id="iframe_gallery_wrap"> 

             <a href="#p1" class="gallery"> 
              <div id="p1"> 
               <iframe class="lightbox-content" src="{% url 'projects:description' project.id %}report/" width="1200" height="800" id="p1" style="border:none;" webkitallowfullscreen="" mozallowfullscreen="" allowfullscreen=""></iframe> 
              </div> 
             </a> 
+0

을 그래서 당신을 : 파일에 관해서는

<ul> {% for data in data_list %} <li>{{ forloop.counter }}</li> {% endfor %} </ul> 

내 수정 (사용자 이름으로 표시) 작동합니다 희망 데이터베이스에서 데이터를 가져 와서 렌더링 할 때 행 번호를 나타내는 열을 추가 하시겠습니까? 디스플레이 목적으로 만 사용하는 것입니까? –

+0

예, 데이터베이스에서 데이터를 가져 오는 중입니다. 행 번호는 디스플레이 목적으로 만 사용됩니다. 명확히하기 위해 행 번호는 디스플레이 테이블의 행 번호 여야합니다. 데이터베이스에서 가져와서는 안됩니다. – someone2088

+0

그래서 @neverwalkaloner 대답은 실제로 갈 방법입니다. –

답변

5

Probaly forloop.counter 당신이 찾고있는 것입니다.

그냥 같이 템플릿에서 사용 : 제대로 이해하고,

{% load getters money_handling staticfiles utilities %} 

{% if projects %} 
    <div class="table-container m-t-lg"> 

     <table class="multisection pipeline left"> 
      <tr class="sub-summary"> 
       <th colspan="4"><a href="?detailed_status={{detailed_status}}"><h3 class="p-l-sm">{{detailed_status_str}}</h3></a></th> 
       {% if total_i %}<th>Initial exc VAT: {{total_i|money:"£"}}</th>{% endif %} 
       {% if total_u %}<th>Latest exc VAT: {{total_u|money:"£"}}</th>{% else %} 
       <th></th> 
       {% endif %} 
      </tr> 
     </table> 
     <table class="multisection pipeline left m-b-xl"> 
      <tr class="summary"> 
       <th style="width: 3em;"></th> 
       <th>Number</th> @someone2088 
       {% for field in fields %} 
        <th class="text-sm p-l-sm p-t-sm p-b-sm" style="width:{{widths|getval:forloop.counter0}}"> 
        {% if field.1 %} 
         {% if sort == field.0 and not reverse %} 
          <a href="?sort=-{{field.0}}&detailed_status={{detailed_status}}">{{field.0}}</a> 
         {% else %} 
          <a href="?sort={{field.0}}&detailed_status={{detailed_status}}">{{field.0}}</a> 
         {% endif %} 
        {% else %} 
         {{field.0}} 
        {% endif %} 
        </th> 
        {# Make all have the same number of columns (8) #} 
        {% if forloop.last %} 
         {% for i in ',,,,,,,,' %} 
          {% if forloop.counter|add:forloop.parentloop.counter0 < 11 %} 
           <th>&nbsp;</th> 

          {% endif %} 
         {% endfor %} 
         {% if detailed_status == "ds4"|ds %} 
          <th></th> 
         {% endif %} 
        {% endif %} 
       {% endfor %} 
      </tr> 
      {% with user.employee.full_name|is:'Nick Ross' as summary_link %} 

      {% for project in projects %} 
       <tr data-project-id="{{project.id}}" class="even {% if project.office == 2 %} col{% endif %}"> 
        {% with initial_details=project.initial_details survey=project.survey %} 
         {# Open lightbox #} 
         <td>{{ forloop.counter }}</td> @someone2088 
         <td> 
          {# ERF(22/11/2016 @ 1450) Add a counter to display table row numbers #} 
          {% if user.is_superuser %} 
           <a class="gallery-loader" data-project-id="{{project.id}}"><i class="icon info"></i></a> 

           {% if forloop.first and first_table %} 
            <div id="iframe_gallery_wrap"> 

             <a href="#p1" class="gallery"> 
              <div id="p1"> 
               <iframe class="lightbox-content" src="{% url 'projects:description' project.id %}report/" width="1200" height="800" id="p1" style="border:none;" webkitallowfullscreen="" mozallowfullscreen="" allowfullscreen=""></iframe> 
              </div> 
             </a> 
+0

답변을 주셔서 감사합니다. 확실히 원하는 것처럼 들리지만, HTML로 표시 할 수있는 방법이 확실하지 않습니다. 행/열 등이 아니라 전체적으로 각 테이블을 표시하고 있습니다 ... 테이블에 HTML이 표시되는 순간 OP가 업데이트되었습니다. – someone2088

+0

이미'for' 루프를'for' 루프 안에 중첩시킬 수 있습니까? '테이블의 행 (테이블의 행) {...}) 테이블의 – someone2088

+0

@ someone2088 잘 모르겠지만 다음과 같이 할 수 있습니다. {% % in table %}

{r for % 테이블 %} {{r}} {% endfor %} {% endfor %}. 파이프 라인보기의 모든 코드를 표시 할 수 있습니까? – neverwalkaloner