Django网址-使用{%include%}

时间:2018-11-02 17:32:33

标签: python django datatables

在django 2.0.2模板中,我包含了数据表(来自https://datatables.net/

使用

{% include 'ExchangesList.html' %}

在home.html

一切正常。

但是当我在浏览器中打开/ exchanges / exchanges_list_json /时,有用于表的数据可见。

urls.py

urlpatterns = [
#url(r'^$', ExchangesList.as_view(), name="exchanges"),
url(r'^exchanges_list_json/$', ExchangesListJson.as_view(), name="exchanges_list_json"),] 

exchanges_list.js

$(document).ready(function() {
    var table = $('#exchanges_table').DataTable( {
       "ajax": EXCHANGES_LIST_JSON_URL,
...

home.html

...
<script type="text/javascript">
var EXCHANGES_LIST_JSON_URL = '{% url "exchanges_list_json" %}';
</script>
...

views.py

class ExchangesList(TemplateView):
template_name = 'ExchangesList.html'

class ExchangesListJson(BaseDatatableView): 
    model = Exchanges
    columns = [
        'exchange_sort_id',
        'exchange_name',
        'exchange_country',
        'exchange_url',
        'exchange_trade_volume_24h_btc',
        'exchange_last_updated',
        ]
    order_columns = columns

def filter_queryset(self, qs):
    qs = Exchanges.objects.all()

    sSearch = self.request.GET.get('sSearch', None)
    if sSearch:
        qs = qs.filter(Q(exchange_name__istartswith=sSearch) | Q(exchange_description__istartswith=sSearch) | Q(exchange_id__istartswith=sSearch))
    return qs

def prepare_results(self, qs):
    qs = Exchanges.objects.all()

    json_data = []
    for item in qs:                
        json_data.append({
            'exchange_sort_id': escape(item.exchange_sort_id),
            'exchange_name': escape(item.exchange_name),  # escape HTML for security reasons
            'exchange_country': escape(item.exchange_country),
            'exchange_url': escape(item.exchange_url),
            'exchange_trade_volume_24h_btc': escape(item.exchange_trade_volume_24h_btc),
            'exchange_last_updated': item.exchange_last_updated.strftime("%H:%M:%S "),

        })

    #print(json_data)
    #print('len qs ' , len(qs))
    return json_data

如何阻止直接开放的exchanges_list_json.html使其包含在内?

谢谢您的帮助!

0 个答案:

没有答案