Posts

Showing posts with the label view

Generate excel file with openpyxl in Django

I find this python module quite interesting and useful for my Django project: openpyxl :  https://openpyxl.readthedocs.org/en/default/ Here is a sample snippet: It allows the user to generate the excel file and download it.

Django - object has no attribute 'user' error

I got this error this morning when trying to test my Django app: Internal Server Error: /staff/acts/email/ Traceback (most recent call last):   File "/path/to/virtualenv/myenv/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 111, in get_response     response = wrapped_callback(request, *callback_args, **callback_kwargs)   File "/path/to/virtualenv/myenv/local/lib/python2.7/site-packages/django/contrib/auth/decorators.py", line 22, in _wrapped_view     return view_func(request, *args, **kwargs)   File "/path/to/app/views.py", line 519, in email_main     confirm_sent = send_email(confirm_email_form, 'confirm', request)   File "/path/to/virtualenv/myenv/local/lib/python2.7/site-packages/django/contrib/auth/decorators.py", line 21, in _wrapped_view     if test_func(request.user): AttributeError: 'EmailTemplateForm' object has no attribute 'user' I figured out the reason causing this issue is...

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()...