Linkcolumn不能正常工作django-tables2

时间:2014-08-23 13:02:34

标签: python-2.7 django-tables2

我需要你的帮助。我是django初学者,我无法使用django-tables2中的linkcolumn。我真的很失望,因为我确信这很简单,但直到现在,它还不起作用。我提前感谢你给我的问题所给予的时间,如果它是简单的愚蠢请求你原谅; - )

总结:

  1. index.html返回一个django-tables2表,一切正常
  2. 我想更改列的数据" name"超链接(使用linkcolumn)与目标" ... / Audit / 1 /"其中1来自点击行的pk
  3. 发生错误:

    NoReverseMatch at /fr/Audit/
    
    Reverse for 'detail' with arguments '(1,)' and keyword arguments '{}' not found. 0 pattern(s) tried: []
    
    Request Method:     GET
    Request URL:    http://localhost:8000/fr/Audit/
    Django Version:     1.6.5
    Exception Type:     NoReverseMatch
    Exception Value:    
    
    Reverse for 'detail' with arguments '(1,)' and keyword arguments '{}' not found. 0 pattern(s) tried: []
    
    Exception Location:     /opt/myenv/local/lib/python2.7/site-packages/django/core/urlresolvers.py in _reverse_with_prefix, line 452
    Python Executable:  /opt/myenv/bin/python2.7
    Python Version:     2.7.6
    
  4. 审核/ models.py

    # -*- coding: utf-8 -*-
    from django.db import models
    from django.utils.translation import ugettext_lazy as _
    
    from MyParam.models import MyModel, MyMptt
    from mptt.models import TreeForeignKey
    
    from organization.models import Org
    class Grid(MyMptt):
    name = models.CharField(max_length = 45, verbose_name=_(u"grille d'audit"))
    parent = TreeForeignKey('self', limit_choices_to={'act': True}, related_name='children', null=True, blank=True, verbose_name=_(u"parent"))
    
    class MPTTMeta:
        order_insertion_by = ['name']
    
    class Meta:
        verbose_name = _(u"grille d'audit")
        verbose_name_plural = _(u"grilles des audits")
    
    def __unicode__(self):
        return unicode(self.name)
    

    审核/ urls.py

    # -*- coding: utf-8 -*-
    from django.conf.urls import patterns, url
    
    from Audit import views
    
    
    urlpatterns = patterns('',
        url(r'^$', views.index, name='index'),
        url(r'^(?P<grid_id>\d+)/$', views.detail, name='detail'),
        url(r'^(?P<grid_id>\d+)/vote/$', views.vote, name='vote'),
        url(r'^(?P<grid_id>\d+)/report/(?P<report_id>\d+)$', views.report, name='report'),
    )
    

    审核/ tables.py

    # -*- coding: utf-8 -*-
    import django_tables2 as tables
    from django_tables2.utils import A
    
    from Audit.models import Grid
    
    class GridTable(tables.Table):
        name = tables.LinkColumn('detail', args=[A('pk')])
    
        class Meta  :
            model = Grid
            # add class="paleblue" to <table> tag
            attrs = {"class": "paleblue"}
    
    
    
            VISU = ("id", "name", "parent")
    
            sequence = VISU
            fields = VISU
    

    审核/ views.py

    # -*- coding: utf-8 -*-
    from django.http import HttpResponse, HttpResponseRedirect
    from django.shortcuts import render, get_object_or_404
    
    from django.core.urlresolvers import reverse
    
    from django_tables2   import RequestConfig
    
    """
    from django.views import generic
    """
    from Audit.models import Grid, Item, Assessment, Report, ReportContent
    from Audit.tables import GridTable
    from organization.models import Org
    
    def index(request):
        """
        audit_grid_list = Grid.objects.all().order_by('name')[:5]
        context = {'audit_grid_list': audit_grid_list}
        return render(request, 'Audit/index.html', context)
        """
        table = GridTable(Grid.objects.all())
        RequestConfig(request).configure(table)
        RequestConfig(request, paginate={"per_page": 25}).configure(table)
        return render(request, "Audit/index.html", {"table": table})
    
    
    def detail(request, grid_id):
        org_list = Org.objects.all().order_by('name')[:5]
        grid = get_object_or_404(Grid, pk=grid_id)
        return render(request, 'Audit/detail.html', {'grid': grid,'org_list': org_list})
    

    审核/模板/ index.html中

    {# Audit/templates/index.html #}
    {% load render_table from django_tables2 %}
    <!doctype html>
    <html>
        <head>
            <link rel="stylesheet" href="{{ STATIC_URL }}django_tables2/themes/paleblue/css/screen.css" />
        </head>
        <body>
            {% render_table table %}
        </body>
    </html>
    

    审核/模板/ detail.html

    <h1>{{ grid.name }}</h1>
    
    {% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
    
    <form action="{% url 'Audit:vote' grid.id %}" method="post">
    {% csrf_token %}
    
    <!-- test pour choix du service -->
    
    <label for="org">Choix de la cible de l'audit</label>
    <select name="org" id="org">
        {% for firm in org_list %}
        <option value="{{ firm.id }}">{{ firm.name }}</option>
        {% endfor %}
    </select><br>
    
    
    
    {% for item in grid.Item_grid.all %}
        <h2>{{ item.name }}</h2>
    
        {% for assess in item.Assessment_item.all %}
            <input type="checkbox" name="item{{ forloop.parentloop.counter }}[]" id="item{{ forloop.parentloop.counter }}[]" value="{{ assess.id }}" />
            <label for="item{{ forloop.parentloop.counter }}">{{ assess.name }}</label><br />
        {% endfor %}
    
    {% endfor %}
    
    <input type="submit" value="Vote" />
    </form>
    

1 个答案:

答案 0 :(得分:0)

您的网址正则表达式有一个命名组,因此grid_id应该是一个关键字参数。使用

name = tables.LinkColumn('detail', args={'grid_id': A('pk')})