Django - A simple search view
This is a simple search view I've just made for my Django app:
+ List all the email logs the first time the user visit the page with pagination
+ User search email logs by email address with pagination
views.py:
======
def email_logs(request):
selected_nav = "Events"
request.session['selected_nav'] = 'events'
all_ptc_events = get_all_active_events()
current_selected_event_id = get_current_selected_event_id(request)
staff = request.user
time_range = datetime.now() - timedelta(days=30)
email_logs = MessageLog.objects.filter(when_added__gte=time_range).order_by('-when_added')
if request.method=='POST':
search_form = EmailLogSearchForm(request.POST, request.FILES)
if search_form.is_valid():
tmp = []
for el in email_logs:
if el.to_addresses:
if el.to_addresses[0]==search_form.cleaned_data['email']:
tmp.append(el)
email_logs = tmp
else:
search_form = EmailLogSearchForm()
# START Pagination
if email_logs:
e_list = []
for log in email_logs:
logid = log.pk
subject = log.subject
body = log.email.body
when_attempted = log.when_attempted
to_addresses = log.to_addresses
e_list.append({'logid': logid, 'to_addresses': to_addresses, 'subject': subject, 'body': body, 'when_attempted': when_attempted})
paginator = Paginator(e_list, 10)
page = request.GET.get('page')
try:
mail_logs = paginator.page(page)
except PageNotAnInteger:
mail_logs = paginator.page(1)
except EmtyPage:
mail_logs = paginator.page(paginator.num_pages)
# END Pagination
return render_to_response('staff/email_logs.html', locals(), context_instance=RequestContext(request))
+ List all the email logs the first time the user visit the page with pagination
+ User search email logs by email address with pagination
views.py:
======
def email_logs(request):
selected_nav = "Events"
request.session['selected_nav'] = 'events'
all_ptc_events = get_all_active_events()
current_selected_event_id = get_current_selected_event_id(request)
staff = request.user
time_range = datetime.now() - timedelta(days=30)
email_logs = MessageLog.objects.filter(when_added__gte=time_range).order_by('-when_added')
if request.method=='POST':
search_form = EmailLogSearchForm(request.POST, request.FILES)
if search_form.is_valid():
tmp = []
for el in email_logs:
if el.to_addresses:
if el.to_addresses[0]==search_form.cleaned_data['email']:
tmp.append(el)
email_logs = tmp
else:
search_form = EmailLogSearchForm()
# START Pagination
if email_logs:
e_list = []
for log in email_logs:
logid = log.pk
subject = log.subject
body = log.email.body
when_attempted = log.when_attempted
to_addresses = log.to_addresses
e_list.append({'logid': logid, 'to_addresses': to_addresses, 'subject': subject, 'body': body, 'when_attempted': when_attempted})
paginator = Paginator(e_list, 10)
page = request.GET.get('page')
try:
mail_logs = paginator.page(page)
except PageNotAnInteger:
mail_logs = paginator.page(1)
except EmtyPage:
mail_logs = paginator.page(paginator.num_pages)
# END Pagination
return render_to_response('staff/email_logs.html', locals(), context_instance=RequestContext(request))
Comments
Post a Comment