-->

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

2019-08-19 17:58发布

问题:

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.