0

When I fill out my form with its file, it apparently submits, but when checking it out when editing/uploading, all data in the form shows up except my file. So, I cannot access it at all. I guess it probably has something to do with my views.py (the function is for both, save a form for the first time and visualizing them), so here it goes #views.py

def contract_form(request, id=0):
    if request.method == "GET":
        if id == 0:
            form = ContractsForm(request.FILES)
        else:
            contratos = Contratos.objects.get(pk=id)
            form = ContractsForm( instance=contratos)
        return render(request,"contracts_form.html", {'form':form})
    else:
        if id == 0:
            form = ContractsForm(request.POST, request.FILES)
        else:
            contratos = Contratos.objects.get(pk=id)
            form = ContractsForm(request.POST, request.FILES, instance= contratos, )
        if form.is_valid():
            form.save()
            messages.success(request, "Form submission successful")
        else:
            messages.error(request, "Error, contract not submitted")

        return redirect('/contracts')

Just in case, every other instance that has something to do with the upload file:

#models.py
attached_file=models.FileField(upload_to="media", blank=True)

My form is stated with <form enctype="multipart/form-data" action="" method="post" autocomplete="off" class="row g-3">

#contracts_form.html
                        <label for="{{ form.subject.id_for_label }}">Attached File:</label>
                        {{form.attached_file}}
                        {% if Contracts.media %}
                        <a href="{{Contracts.media.url}}"></a>
                        {% endif %}
#settings.py
MEDIA_ROOT=os.path.join(BASE_DIR, 'uploads/')
MEDIA_URL="/contracts-media/"
    path('contracts/edit/<int:id>', views.contract_form, name='edit'), #edit/update form
    path('contracts/add', views.contract_form, name = 'new-contract'), #add a contract

] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
````
Mario A
  • 55
  • 3
  • I did not fully understand the problem. Can't reach the media when you post the form by uploading a media? – j4nus Jul 21 '22 at 23:30
  • @j4nus my bad. I have a CRUD app with a file column and when trying to view/edit an instance, all data is shown except my file. On the admin backend i have checked and it is uploaded, but doesn´t appear any kind of link to open it when visualizing the instance – Mario A Jul 21 '22 at 23:51

1 Answers1

0

There's no text in your link tag

<a href="{{Contracts.media.url}}"></a>

Try

<a href="{{Contracts.media.url}}">Wonderful Linkiness</a>
SamSparx
  • 4,629
  • 1
  • 4
  • 16