0

I am using django-datable-view for rendering data from django models.

Everything works fine before decorating the url, after i added login_required to the url, it threw weird error. According to the doc, it states that i can add login_required to the url.

Below is my code

from django_datatables_view.base_datatable_view import BaseDatatableView

class OrderListJson(BaseDatatableView):
    # The model we're going to show
    model = MyModel

    # define the columns that will be returned
    columns = ['number', 'user', 'state', 'created', 'modified']

    # define column names that will be used in sorting
    # order is important and should be same as order of columns
    # displayed by datatables. For non sortable columns use empty
    # value like ''
    order_columns = ['number', 'user', 'state', '', '']

    # set max limit of records returned, this is used to protect our site if someone tries to attack our site
    # and make it return huge amount of data
    max_display_length = 500

    def render_column(self, row, column):
        # We want to render user as a custom column
        if column == 'user':
            return '{0} {1}'.format(row.customer_firstname, row.customer_lastname)
        else:
            return super(OrderListJson, self).render_column(row, column)

    def filter_queryset(self, qs):
        # use parameters passed in GET request to filter queryset

        # simple example:
        search = self.request.GET.get(u'search[value]', None)
        if search:
            qs = qs.filter(name__istartswith=search)

        # more advanced example using extra parameters
        filter_customer = self.request.GET.get(u'customer', None)

        if filter_customer:
            customer_parts = filter_customer.split(' ')
            qs_params = None
            for part in customer_parts:
                q = Q(customer_firstname__istartswith=part)|Q(customer_lastname__istartswith=part)
                qs_params = qs_params | q if qs_params else q
            qs = qs.filter(qs_params)
        return qs

url

url(_(r'^users/all/?$'),
    login_required(dashboard.v1.views.OrderListJson.as_view()),
    name='all_users'),

i keep getting error 500, if i remove the login_required, everything works well. If i can get suggestions on how to decorate the class view, i will be glad since that is what am trying to achieve

  • Using `login_required` like that normally works. Check your logs or admin emails to see what is causing the 500 error. – Alasdair Oct 18 '17 at 13:46
  • As an aside, I would remove the question mark from `r'^users/all/?$'`, and let Django redirect `/users/all` to `/users/all/`. It's recommended not to have multiple urls that return the same content. – Alasdair Oct 18 '17 at 13:47
  • Make sure you imported the decorator before using it: `from django.contrib.auth.decorators import login_required`. – Alasdair Oct 18 '17 at 13:52
  • I imported it before using it. – Raheem Azeez Abiodun Oct 18 '17 at 13:58
  • In that case you need to find the traceback to see what the problem is. If it's a JSON request and `DEBUG=True`, you may be able to see the traceback with your browser tools. – Alasdair Oct 18 '17 at 14:08

0 Answers0