django-tables2 header row urls wrong…can't sort

筅森魡賤 提交于 2019-12-11 14:59:46

问题


I can't figure out how to solve this issue about sorting my table: table is rendered at this url: 127.0.0.1:8000/ip_teams/

but when I click on the name of the column to sort the table, url became: 127.0.0.1:8000/?sort=name it excludes the "ip_teams/" path.

This is my view:

class FoundationIPTypeList(SingleTableView):

    model = tracker_models.FoundationIPType
    table_class = tables.FoundationIPTypeTable

this is my table:

class FoundationIPTypeTable(django_tables.Table):
    class Meta:
        model = FoundationIPType
        attrs = {'class': "table table-striped table-condensed table-hover"}
        fields = ["name", "help_contact_email"]

this is my urls:

urlpatterns = [

#    url(r'^$', views.HomePageView.as_view(), name='home'), # Notice the URL has been named
    url(r'^about/$', views.AboutPageView.as_view(), name='about'),
    url(r'^ip_teams/$', views.FoundationIPTypeList.as_view(), name="ip_team_list"),

this is my template:

{% extends "base.html" %}
{% load render_table from django_tables2 %}


{% block pagecontent %}
<h1><center>Foundation IP Teams</center></h1>

<div class="container">
  <div class="panel panel-primary">
       {% render_table table %} 
  </div>
</div>

{% endblock %}

any ideas what is wrong? can't seem to find the answer anywhere. i thought this feature would work right out of the box.

table header row:

    <thead>
    <tr>


        <th class="name orderable"><a href="?sort=name&amp;name=unknown">Name</a></th>



        <th class="help_contact_email orderable"><a href="?sort=help_contact_email&amp;name=unknown">Help Contact Email</a></th>


    </tr>
</thead>

回答1:


Django-tables2 uses relative urls which add the sorting parameter. This works as long as the document does not contain a base tags, which change the base of any relative urls in the page.

Removing the <base> tag will fix your issue.



来源:https://stackoverflow.com/questions/47499220/django-tables2-header-row-urls-wrong-cant-sort

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!