Как конвертировать в хрустящие формы просто и быстро?
У меня есть проект, в конце которого я понял, что мне нужно перейти на хрустящие формы. Я предоставлю код, возможно у вас есть простое и быстрое решение, не делая это снова.
Форма должна иметь две кнопки: просмотр и отправка.
Это upload_document.html
{% extends "base.html" %}
{% load static %}
{% block content %}
<!-- Upload form. Note enctype attribute! -->
<form action="{% url "upload_doc" %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ message }}
<p>{{ form.non_field_errors }}</p>
<p>{{ form.docfile.label_tag }} {{ form.docfile.help_text }}</p>
<p>
{{ form.docfile.errors }}
{{ form.docfile }}
</p>
<p><input type="submit" value="Upload and Download!"/></p>
</form>
{% endblock content %}
Это forms.py
class DocumentForm(forms.Form):
docfile = forms.FileField(label='Select a file')
Это views.py
def OnlyCAL(request):
if request.method == "POST":
form = DocumentForm(request.POST, request.FILES)
if form.is_valid():
output = io.BytesIO()
newdoc = request.FILES['docfile']
#pandas calculations
response = HttpResponse(
output, content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
response['Content-Disposition'] = 'attachment; filename=%s' % filename
return response
else:
form = DocumentForm()
return render(request, 'upload_document.html', { 'form': form, 'page_title':page_title })