I'm attempting to create a website using Django, a framework I'm not very familiar with. Since I don't understand what Django forms do, I originally wrote my HTML file purely in standard HTML. Now I'm facing difficulties saving uploaded files because Django doesn't allow me to handle them manually. I tried to find a workaround but was unsuccessful. I believe I need to rewrite my HTML file using Django's templating system so that I can properly save the files. Here is the HTML file I have:
html
<div class="w3-container w3-white">
<select class="w3-select" name="request_function" id="request_function" >
<option id="faces_embedding" value="faces_embedding" style="" onclick="pagechange()">faces_embedding</option>
<!-- <option id="faces_model" value="faces_model" style="" onclick="pagechange()"> faces_model </option> -->
<option id="image_recognizer"value="image_recognizer" style="" onclick="pagechange()">image_recognizer</option>
<option id="video_recognizer" value="video_recognizer" style="" onclick="pagechange()">video_recognizer</option>
<option id="stream_recognition" value="stream_recognition" style="" onclick="pagechange()">stream_recognizer</option>
<option id="help" value="help" style="" onclick="pagechange()">help</option>
</select>
</div>
<div id="AddFaces" style="visibility: visible; display: block;">
<div class="w3-row-padding" style="margin:0 -16px;" >
<div class="w3-half">
<input type="radio" name="input_method" id="input_method_0" onclick="datachange()" checked value="input_method_0">
<label>Add A Database</label>
</div>
<div class="w3-half">
<input type="radio" name="input_method" id=input_method_1" onclick="datachange()" value="input_method_1">
<label>Add A Face</label>
</div>
</div>
<div class="w3-row-padding" style="margin:0px -10px;">
<div id="dataset" class="w3-half w3-margin-bottom" style="visibility: visible; display: block;">
<label>Dataset Path</label>
<input class="w3-input w3-border" type="text" placeholder="directory path" name="dataset_path" id="dataset_path" required >
</div>
<div id="face" style="visibility: hidden; display: none;">
<div class="w3-half w3-margin-bottom">
<label>Images Path</label>
<input type="file" id="face_files" name="face_files" multiple >
</div>
<div class="w3-half">
<label>Name</label>
<input class="w3-input w3-border" type="text" placeholder="person name" name="face_name" id="face_name" >
</div>
</div>
</div>
</div>
where "datachange()" is a javascript function that hides different dives that I don't need and my view.py
if request.method == "GET":
return render(request, 'index.html')
if request.method == "POST":
form = InputForm(request)
call_form_function(form)
return render(request, 'index.html')
where "InputForm.py" is a normal python class which takes forms id tags and use them as input for functions.
def __init__(self, request):
self.request_function = request.POST.get("request_function")
self.input_method = request.POST.get('input_method')
self.dataset_path = request.POST.get('dataset_path')
self.face_files = request.FILES.getlist['face_files']
self.face_name = request.POST.get('face_name')
from django documents, I used the this function:
def handle_uploaded_file(f):
with open('some/file/name.txt', 'wb+') as destination:
for chunk in f.chunks():
destination.write(chunk)
I’m having an issue where Django automatically closes the file before I have a chance to write anything.
Question: Do I have to use Django forms to save uploaded files? Is there a way to bypass this requirement? If not, how can I modify my HTML file to ensure it still looks the same on the webpage and that my JavaScript works with Django templates?
P.S. I also do not have a database or a models.py file since I don't need them.
Well, if you don't use forms and don't use database, why are you using Django :) As with all modern web frameworks that use MVC (Mode, View, Controller) or in Django case MTV (Model, Template, View) you will sooner or later need all three of those.
Model defines your database schema, Template defines html templates - what users will see, and View is actually where programming logic lives.
If the template has forms, you will need to define form in Django. It actually simplifies life a lot and it can be very simple definition if you use models. And information on your files (metadata) that someone can upload should be stored in a model (database). That will ease your handling of files quite a lot.
But in the end it really depends what is your application doing?