Playing around with FileFiled
and trying to update a form. I think my problem comes from the views.py.
I have a template where I can see a product, and I have the option to update the product by clicking on the update button.
When clicking on the update button, I am redirected to a form where I can update the product.
template
{% for TermsAndConditions in commercial_list %}
<a class="btn btn-outline-secondary" href="{% url 'update-commercial' TermsAndConditions.id %}">Update
</a>
{% endfor %}
templateform.py
<form action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
<h6>Upload terms</h6>
{{ form.attachment| as_crispy_field}}
{% if url %}
<p>Uploaded file <a href="{{url}}">{{url}}</a></p>
{%endif%}
<button type="submit" value="Submit" id="profile-btn" class="btn btn-primary custom-btn">Update</button>
</form>
My normal upload form (without FileField
) looks like this:
views (without FileField
)
def update_commercial(request, termsandconditions_id):
commercials = TermsAndConditions.objects.get(pk=termsandconditions_id)
form = TermsAndConditionsForm(request.POST or None, instance=commercials )
if form.is_valid():
form.save()
return redirect('list-commercial')
return render(request, 'main/update_commercial.html',{'form':form,'commercials':commercials})
With the addition of the FileField
, I thought I would do something like this:
views (with FileField
)
def update_commercial(request, termsandconditions_id):
commercials = TermsAndConditions.objects.get(pk=termsandconditions_id)
form = TermsAndConditionsForm(request.POST or None, request.FILES ,instance=commercials ) #the change being request.FILES
if form.is_valid():
form.save()
return redirect('list-commercial')
return render(request, 'main/update_commercial.html',{'form':form,'commercials':commercials})
Problem is, when I do that the update button in the template becomes invalid (it doesn't redirect to the update form).
I think my problem is how to handle the or None
and request.FILES
together. I tried different combinations but none have worked. Hoping someone might be able to shed some light.
(I have add the models and url files, as I dont think these are the problem and didnt want to make this post longer than it should be, but feel free to let me know)