Logging a button click

31 views
Skip to first unread message

Jim Lamb

unread,
Nov 26, 2018, 6:40:58 PM11/26/18
to Django users
I am trying to log a button click from a template.
Can someone please help???
view.py

from django.shortcuts import render
from .models import UploadFile
from django.contrib.auth.decorators import login_required
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
import logging
logger = logging.getLogger('download_user')


@login_required
def index(request):
    client_ip = request.META['REMOTE_ADDR']
    upload_list = UploadFile.objects.all().order_by('file')
    filename = ''
    for filename in upload_list:
        print(filename.file)

    if 'download' in request.POST:
        logger.debug('user: {} |ip address: {} |downloaded file: {}'.format(request.user, client_ip, filename.file))

    # for f in request.FILES.getlist('upload_list.file'):
    #     filename = f.name

    page = request.GET.get('page', 1)
    paginator = Paginator(upload_list, 20)
    try:
        uploads = paginator.page(page)
    except PageNotAnInteger:
        uploads = paginator.page(1)
    except EmptyPage:
        uploads = paginator.page(paginator.num_pages)
    context = {
        'uploads': uploads,
    }
    return render(request, 'uploads/index.html', context)

index.html

{% extends 'base.html' %}
{% block title %}Download Source Code{% endblock %}

{% block content %}
    {% if user.is_authenticated %}

    <div class="container">
        <div class="jumbotron">
            <h2 class="text-center">Download Source Code</h2>
            <div class="row">
                <div class="col-lg-12">
                    <table class="table table-striped table-hover table-responsive">
                        <thead class="thead-dark">
                            <tr>
                                <th style="width: 400px" class="col-md-4">Product</th>
                                <th style="width: 400px" class="col-md-4">Name</th>
                                <th style="width: 400px" class="col-md-4">File</th>
                                <th style="width: 250px" class="col-md-4">Description</th>
                                <th style="width: 400px" class="col-md-4">MD5 Hash</th>
                                <th style="width: 400px" class="col-md-4">SHA256</th>
                                <th style="width: 400px" class="col-md-4">Download</th>
                            </tr>
                        </thead>
                        <tbody>

                            {% for upload in uploads %}
                                {% if upload.verified_by %}
                                <tr>
                                    <td style="width: 400px" class="col-md-4">{{ upload.products }}</td>
                                    <td style="width: 400px" class="col-md-4">{{ upload.name }}</td>
                                    <td style="width: 400px" class="col-md-4"><a href="{{ MEDIA_URL }}{{ upload.file }}">{{ upload.file }}</a></td>
                                    <td style="width: 250px" class="col-md-4">{{ upload.description }}</td>
                                    <td style="width: 400px" class="col-md-4">{{ upload.md5sum }}</td>
                                    <td style="width: 400px" class="col-md-4">{{ upload.sha256sum }}</td>
                                    <form method="GET">
                                        {% csrf_token %}
                                        <td>
{#                                            <input type="button" id="soft" class="btn btn-primary btn-sm" name="download" value="Download Software">#}
                                        <a href="{{ upload.file.url }}" class="btn btn-primary btn-sm" name="download">Download Software</a>
                                        </td>
                                    </form>
                                </tr>
                                {% endif %}
                            {% endfor %}
                        </tbody>
                    </table>
                    {% if uploads.has_other_pages %}
                         <ul class="pagination">
                        {% if uploads.has_previous %}
                            <li><a href="?page={{ uploads.previous_page_number }}">&laquo;</a></li>
                        {% else %}
                            <li class="disabled"><span>&laquo;</span></li>
                    {% endif %}
                    {% for i in uploads.paginator.page_range %}
                    {% if uploads.number == i %}
                    <li class="active"><span>{{ i }} <span class="sr-only">(current)</span></span></li>
                    {% else %}
                    <li><a href="?page={{ i }}">{{ i }}</a></li>
                    {% endif %}
                    {% endfor %}
                    {% if downloads.has_next %}
                    <li><a href="?page={{ uploads.next_page_number }}">&raquo;</a></li>
                    {% else %}
                    <li class="disabled"><span>&raquo;</span></li>
                    {% endif %}
                    </ul>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>

    {% else %}
        <div class="container">
            <div class="jumbotron">
                <p>You are logged in.</p>
            </div>
        </div>

    {% endif %}


Nelson Varela

unread,
Nov 27, 2018, 8:16:31 AM11/27/18
to Django users
Look good but what is going wrong? Do you see an error or ...?
Reply all
Reply to author
Forward
0 new messages