0

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>
  • Did you try using the table filtering example in http://django-tables2.readthedocs.io/en/latest/pages/filtering.html? Try using that, and then add any customization you need for your project. – Jieter Nov 27 '17 at 08:12
  • @Jieter i had started with http://django-tables2.readthedocs.io/en/latest/pages/generic-mixins.html and then found a more generic way to do it, which is what i listed in my original comment. your comment refers to filtering, where i can't even get the basic stuff working like just having the resort of the columns work. the url created by django-tables2 is incorrect. – Emmanuel Sifakis Nov 27 '17 at 08:57
  • Yes, but you use custom code which I did not test/run. So that's why I suggest using something not custom to start with. If you do not want the filtering yet, use the example like you linked to and please update the code in your question accordingly. – Jieter Nov 27 '17 at 09:07
  • @Jieter do i want to inherit from SingleTableMixin and FIlterView or just inherit from SingleTableView? i am confused as to what approach to take. i tried without any custom code using the simple example i gave and the same happened...no "ip_teams" in the url for sorting AND the filtering didn't work anymore. – Emmanuel Sifakis Nov 28 '17 at 03:14
  • First, try to inherit from `SingleTableView`. If that leads to working urls, try changing to inherit from `(SingleTableMixin, FilterView)` – Jieter Nov 28 '17 at 06:34
  • @Jieter i have stripped down all my code to look exactly like your SingleTableView example. still not working....the table draws but when i click the name column of the table, the url is still missing "ip_teams/". also, filtering is not working now because of these changes. there has to be something small/silly not being done here for this not to be working right out of the box. – Emmanuel Sifakis Nov 28 '17 at 09:03
  • @jieter do i have to do anything with using build_absolute_uri or reverse to get the url to be correct? – Emmanuel Sifakis Nov 28 '17 at 09:21
  • No, should work without that. The querystring template tag just compiles a querystring, the browser should attach that to the current path. Could you have a look at the HTML rendered for the header? What exact `` tag do you see? – Jieter Nov 28 '17 at 09:51
  • @Jieter not sure what you're looking for exactly. the table header html? i added that above. also, since there is no filter_class set in FoundationIPTypeList anymore, the filtering shouldn't be working anyway, right? – Emmanuel Sifakis Nov 28 '17 at 17:51
  • Thanks for adding the HTML. The hrefs look fine and should not result in a link to root in normal circumstances. Do you maybe have a `base` tag in the rest of your HTML? https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base – Jieter Nov 28 '17 at 19:31
  • @Jieter yes, that was it. had some html i had used for angular which i reused, and unfortunately this part was still left there. thanks for catching this. i converted the SimpleTableView to SimpleTableMixin and added FilterView, so now everything sorts, links, and filters properly. Thanks so much for your help! – Emmanuel Sifakis Nov 29 '17 at 03:06
  • Nice, you're welcome! – Jieter Nov 29 '17 at 11:11

1 Answers1

2

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.

Jieter
  • 4,101
  • 1
  • 19
  • 31