我们假设我有以下表类:
class TestTable(tables.Table):
id = tables.Column()
description = tables.Column()
def render_description(self, value):
return mark_safe('''<a href=%s>%s</a>''' % (???, value))
Run Code Online (Sandbox Code Playgroud)
是否可以在render方法中访问列"id"的值,以便我可以建立一个导致id的链接,但是显示依赖于'description'字段的文本?
提前致谢!
我在django-tables2表中有一个TemplateColumn,我想使用自定义模板过滤器(名为int_to_time)来转换数据.当我使用内置过滤器时,它工作正常.
我到目前为止所做的是我将模板\ django_tables2\table.html从django-tables2复制到我的项目,并将我的标签库包含到table.html中.
但是,当我尝试渲染我的视图时,我收到以下错误:
TemplateSyntaxError at /details_show/2012/3/13/2 Invalid filter: 'int_to_time'
该错误似乎在table.html的第28行
{% for column, cell in row.items %}
我可以确认我的标签库正在加载,因为如果我写错了标签库的名称,那么我将得到一个模板库找不到错误.
请帮忙 !
django django-templates django-template-filters django-tables2
我有2个模型属性 - model.name和model.url我需要创建一个linkColumn,列名= model.name并链接到model.url中指定的url
有可能实现这样的事情吗?
谢谢
我用django-tables2创建了我的表输出,但我想在表格列中为复选框指定一个特定的名称.
我怎样才能做到这一点 ?
这是我的表绘图类,我修改了列的顺序,以便我的复选框是第一个.
class SimpleTable(tables.Table):
amend = tables.CheckBoxColumn(verbose_name=('Amend'), accessor='pk')
class Meta:
model = SysimpReleaseTracker
attrs = {"class": "paleblue"}
listAttrs = list()
listAttr = list()
listAttrs = SysimpReleaseTracker._meta.fields
listAttr.append('amend')
for x in listAttrs:
listAttr.append('%s' %x.name)
sequence = listAttr
Run Code Online (Sandbox Code Playgroud) 我使用django_tables2呈现我的表。对于一张表,我只想查看最近的5个条目。因此,我需要在将查询集传递给表对象之前对其进行排序:
qset = myObject.objects.order_by('-start_time').all()[:5]
table = MyTableClass(qset, template='path/to/template.html')
Run Code Online (Sandbox Code Playgroud)
这将产生以下错误消息:
AssertionError: Cannot reorder a query once a slice has been taken.
Run Code Online (Sandbox Code Playgroud)
我可以orderable=False为每个设置django_tables.Column,但是由于MyTableClass继承自另一个表类,因此我想避免这种情况。
提前致谢
我正在尝试使用 django-tables2 在表中发出 get 请求后显示所有模型对象。它当前显示所有内容,但我无法弄清楚如何根据我的视图中的模型 pk 过滤查询集:
视图.py
class ViewJob(LoginRequiredMixin, SingleTableView):
model = JobResults
table_class = JobResultsTable
template_name = 'app/viewjobresults.html'
paginator_class = LazyPaginator
table_pagination = {"per_page": 30}
def get_context_data(self, **kwargs):
""" ViewJob get_context_data request """
context = super(ViewJob, self).get_context_data(**kwargs)
print("ViewJob >get_context_data()")
context['job_obj'] = Job.objects.get(pk=self.kwargs.get('pk'))
# context['object_list'] = context['object_list'].filter(job_id=context['job_obj'].id)
return context
Run Code Online (Sandbox Code Playgroud)
模板 - app/viewjobresults.html
{% extends "base.html" %}
{% load render_table from django_tables2 %}
{% render_table table %}
{% endblock %}
Run Code Online (Sandbox Code Playgroud)
表.py
class JobResultsTable(tables.Table):
job = tables.Column(
accessor='job_id.job_name',
verbose_name='Job')
results = …Run Code Online (Sandbox Code Playgroud) 我有一个表,在其中比较所选数据的两个版本。该数据实际上存储了多个版本,因此在我的表中我有一列:
class ver_compare(tables.Table):
new_db = tables.CheckBoxColumn()
data = tables.Column()
current_rev = tables.Column()
next_rev = tables.Column()*
Run Code Online (Sandbox Code Playgroud)
现在,我希望每个单元格都有一个可供选择的版本下拉列表,类似于 choicefield 的最后一个字段。有什么方法可以一起走吗??
提前致谢!!
使用 djt2 v0.15/dj1.6/pyth2.6.6
多个表的 djt2 doc 视图文件示例:
def people_listing(request) :
config = RequestConfig(request)
table1 = PeopleTable(Person.objects.all(), prefix="1-")
table2 = PeopleTable(Person.objects.all(), prefix="2-")
config.configure(table1)
config.configure(table2)
return render(request, "people_listing.html",
{"table1": table1, "table2": table2})
Run Code Online (Sandbox Code Playgroud)
首先,该示例对于引用的“table1”、“table2”参数似乎不正确。我的测试显示定义名称“people_list”需要在引号中使用,至少在单个表上。此外,为什么有人想将同一张表显示两次?这是一个坏例子吗?这是我的应用程序尝试使用这种结构:
def AvLabVw(request):
config = RequestConfig(request)
cmutbl = CmuTable(CmuVersion.objects.all(), prefix="1-")
simtbl = SimTable(Simulator.objects.all(), prefix="2-")
config.configure(cmutbl)
config.configure(simtbl)
return render(request, "AvRelInfo.html", {"AvLabVw":cmutbl, "AvLabVw":simtbl})
Run Code Online (Sandbox Code Playgroud)
url 文件在 AvLabVw 上选取,html 模板使用 render_table。
{% render_table AvLabVw %}
Run Code Online (Sandbox Code Playgroud)
此代码所发生的情况是仅仍然显示一个表,以返回渲染行上的最后一个表为准。
在文档的其他地方,它说需要使用具有 get_context_data 的 SingleTableView ,我还没有弄清楚......
我尝试过这种风格的实现,我认为它需要一个表对象和一个列表对象?
从 django_tables2 导入视图 从 django_tables2 导入 SingleTableView 从 django_tables2 导入 SingleTableMixin …
我目前正在尝试将 a 添加Link column到我已经使用 Django 表 2 创建的表中。
我正在使用文档中的以下代码
class PeopleTable(tables.Table):
name = tables.LinkColumn('people_detail', text='static text', args=[A('pk')])
Run Code Online (Sandbox Code Playgroud)
视图.py
urlpatterns = patterns('',
url('people/(\d+)/', views.people_detail, name='people_detail')
)
Run Code Online (Sandbox Code Playgroud)
问题是,当我尝试加载网页时,出现以下错误:
Reverse for 'people_detail' with arguments '(1,)' and keyword arguments '{}' not found. 0 pattern(s) tried: []
有人能看到这里的问题吗?
编辑:我的 url.py 如下所示:
urlpatterns = [
url(r'^$', views.IndexView, name='index'),
url(r'^Search/$', views.SearchView, name='Search'),
url(r'^people/(\d+)/$', views.myview,{}, name='people_detail'),
url(r'^comment/$', views.LicenseComment, name='comment'),
url(r'^copyLicense/$', views.copyLicense, name='Copy'),
url(r'^download/$', views.download, name='Download'),
url(r'^AddMod/$', views.addModule, name='addMod'),
url(r'^removeMod/$', views.removeModule, name='removeMod'),
url(r'^login/$', views.Login.as_view(), name='login'),
url(r'^logout/$', views.LogOut, …Run Code Online (Sandbox Code Playgroud) 在我的 django 项目中,我有很多返回模型的表。最后一列主要是操作列,用户可以在其中编辑或删除实例。如果我想将其他参数传递给 TemplateColumn,如果在某些表中我需要编辑和删除按钮,而在其他表中我只需要编辑和信息按钮,我该如何继续?我想使用相同的 template.html 但其中有条件。这是我在表中的内容:
import django_tables2 as tables
from select_tool.models import DefactoCapability
class DefactoCapabilityTable(tables.Table):
my_column = tables.TemplateColumn(verbose_name='Actions', template_name='core/actionColumnTable.html')
class Meta:
model = DefactoCapability
template_name = 'django_tables2/bootstrap-responsive.html'
attrs = {'class': 'table table-xss table-hover'}
exclude = ( 'body', )
orderable = False
Run Code Online (Sandbox Code Playgroud)
如何检查操作的权限以便显示或不显示按钮?