need help for displaying lists of files associated in employee

need help for displaying list of files associated with the employee model, I already use the generic views but it doesn’t display in the template, I don’t have problem uploading the file, my problem is that i cant render it in the html,

please help me

here is my document model

class Document(models.Model):
    employee = models.ForeignKey(Employee, on_delete=models.CASCADE)
    filename = models.CharField(max_length=250, blank=True)
    file = models.FileField(upload_to=employee_file_path)
    uploaded_at = models.DateTimeField(auto_now_add=True)

this is my

def employee_documents(request, pk):
    employee = get_object_or_404(Employee, pk=pk)
    documents = employee.document_set.all()
    context = {'employee': employee, 'documents': documents}
    return render(request, 'hris/document/employee_documents.html', context)

this was in my template

<div class="card mb-4">
    <div class="card-body">

        <h5 class="my-3 text-center">Uploaded Files</h5>

        {% if documents %}
            {% for docs in documents %}
                <div class="row">
                    <div class="col-sm-3">
                        <p class="mb-0">{{ docs.filename }}</p>
                    <div class="col-sm-9">
                        <p class="text-muted mb-0">{{ docs.file.url }}</p>
            {% empty %}
                <p>No Uploaded Documents</p>
            {% endfor %}
        {% endif %}
        <div class="d-flex justify-content-center mb-2">
            <a href="{% url 'document-upload' slug=employee.slug %}" class="btn btn-md btn-outline-primary ms-1">Upload File</a>

this is i want to look like
Screenshot 2023-05-16 201344

thanks a lot.

In your section:

Is the second lines of each pair supposed to be a link to download the file?

What is actually being rendered?

1 Like

the second line must be a link of the filename so that it can view to other page or download it.,

when I run the server, nothing happens in the html template

Side note: When posting code here, please remember to surround the code between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code (or template), then another line of ```. This forces the forum software to keep your code properly formatted. You don’t need to repost your code, but I would suggest you edit your original post to add those lines before and after each file’s block of code. (and template)

It looks like you’re showing two different views here. Which view is being executed? (What url is being invoked? What is the definition for that url?)

1 Like

sorry sir, it’s my first time here.

this is my that I’m using

def employee_document(request, pk):
    employee = get_object_or_404(Employee, pk=pk)
    documents = employee.document_set.all()
    context = {‘employee’: employee, ‘documents’: documents}
    return render(request, ‘hris/document/employee_documents.html’, context)

my html is an include html inside the employee detail, so i come up with this url

path('employee/<slug:slug>-<int:pk>/', employee_documents, name='employee-documents'),

this is my web app when running

I don’t see how that’s going to work at all.

Are you getting any error messages in the console where you’re running runserver?

You have:

But your url is defined as:

The view names don’t match.

I’ll assume at the moment that it’s a copy/paste error between the two.

You might try adding a couple print statements in that view to see what you’re getting for employee and documents before attempting to render it.

1 Like

this is my updated

def employee_documents(request, pk, slug):
    employee = get_object_or_404(Employee, pk=pk, slug=slug)
    documents = employee.document_set.all()
    context = {'documents': documents}
    return render(request, 'includes/_employee_documents.html', context)

I’m including the employee_documents.html in employee_detail.html

 {% include 'includes/_employee_documents.html' %}

this is my custom file path

def employee_file_path(instance, filename):  # WORKING
    # Get the employee's full_name
    full_name = instance.employee.fullname
    # Replace any spaces with underscores
    full_name = full_name.replace(' ', '_')
    # Construct the file path: 'files/<full_name>/<title><ext>'
    ext = os.path.splitext(filename)[1]
    file_path = f"files/{full_name}/{filename}"
    return file_path

I’m trying to use my code in rendering the ImageField to the template, but it didn’t push through.

I don’t know if this code is the problem

{% for docs in documents %}
    <li><a href="{{ docs.file.url }}" class="text-primary mb-3">{{ }}</a></li>
{% empty %}
    <li>No Documents Found</li>
{% endfor %}

Except you’re rendering _employee_documents.html directly in your view, and it sounds like _employee_documents.html is not a complete page?

I’m not sure I’m understanding what you’re trying to do here. Including a template in another template is not going to cause a view to be executed.

What is the view that is creating the page that you are looking at?

1 Like

sir, what is the best way I can do? I have trouble understanding the documentation regarding the File Handling

I just want to render the list of files(.pdf, .docs, .jpg) associated with the employee in the html like this
Screenshot 2023-05-16 201344

please help me.,

I’m trying to help you here, but I’m beginning to believe the problem is more fundamentally wrong than what you’re focusing on. We need to establish that what you’re trying to do here is correct.

1 Like

I update my code with this

    {% for document in employee.document_set.all %}
            <a href="{{ document.file.url }}" target="_blank">{{|basename }}</a>
    {% endfor %}

I successfully render it to the template

what I have done is I make a templatetag folder and make a custom filter and load it in my employee_detail html and added the basename tag

import os

from django import template

register = template.Library()

def basename(value):
	return os.path.basename(value)

Thank You very much sir for your time and effort to accommodate my concerns. God Bless You.