Reputation: 579
I have a method that has the task of redirecting the page in the admin panel from:
http://127.0.0.1:8000/admin/events/event/
to:
http://127.0.0.1:8000/admin/events/event/?date__lt=2018-6-11
I got an information instead of page:
The page isn’t redirecting properly
Here is my code: admin.py
First version of code:
def changelist_view(self, request, extra_context=None):
if not request.method != 'GET':
url = '{}?date__lt={}'.format(reverse('admin:events_event_changelist'), date.today())
return HttpResponseRedirect(url)
code after edit
def changelist_view(self, request, extra_context=None):
q = request.GET.get('date')
if not q:
url = '{}?date__lt={}'.format(reverse('admin:events_event_changelist'), date.today())
return HttpResponseRedirect(url)
Please some hint where can be a wrong code.
Upvotes: 0
Views: 518
Reputation: 308779
If you always want to restrict the events that you display, it might be easier to override the get_queryset
method instead of redirecting.
class EventAdmin(admin.modelAdmin):
def get_queryset(self, request):
qs = super(EventAdmin, self).get_queryset(request)
if request.user.is_superuser:
# Allow superusers to see all events
return qs
else:
return qs.filter(date__lt=date.today())
admin.site.register(Event, EventAdmin)
Upvotes: 1
Reputation: 29977
The error message your browser is showing indicates that your site is stuck in an infinite redirect loop. Looking at your condition, it is clear why:
if not request.method != 'GET':
is equivalent to
if request.method == 'GET':
A redirect always results in a GET
request, so your view will redirect again and again...
I assume what you actually want to achieve is to redirect only if there is no URL parameter. Those are stored in request.GET
, so your condition would look like this:
if not request.GET:
Upvotes: 1