29๐
โ
1nd approach
In urls.py:
urlpatterns = patterns('',
url(r'^$',
login_required(ListView.as_view(
queryset = Poll.objects.order_by('-pub_date')[:5],
context_object_name = 'latest_poll_list',
template_name = 'polls/index.html'), name='poll_lists')),
)
2nd approach
In views.py:
class IndexView(ListView):
queryset = Poll.objects.order_by('-pub_date')[:5]
context_object_name = 'latest_poll_list'
template_name = 'polls/index.html'
@method_decorator(login_required)
def dispatch(self, request, *args, **kwargs):
return super(IndexView, self).dispatch(request, *args, **kwargs)
then in urls.py
urlpatterns = patterns('',
url(r'^$',
IndexView.as_view(), name='poll_lists'),
)
๐คSan4ez
19๐
Just providing a potentially more up-to-date answer,
I would move them to a views file and use the LoginRequiredMixin,
from django.views.generic import (
ListView,
DetailView
)
from django.contrib.auth.mixins import LoginRequiredMixin
class PollsListView(LoginRequiredMixin, ListView):
model = Poll
template_name = 'polls/index.html'
๐คDylan Tonks
- [Django]-How to pass django rest framework response to html?
- [Django]-Sending post data from angularjs to django as JSON and not as raw content
- [Django]-Python 3 list(dictionary.keys()) raises error. What am I doing wrong?
Source:stackexchange.com