I'm currently working with a custom HTML template not using forms on my Django app to upload an image in a specific path.
app structure
src/
media/
app/
img/
app_name/
templates/
app_name/
app_template.html
__init__.py
admin.py
apps.py
models.py
tests.py
urls.py
views.py
proj_name/
__init__.py
settings.py
urls.py
wsgi.py
manage.py
settings.py
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
models.py
class Document(models.Model):
doc_file = models.FileField(upload_to='app/img')
views.py
def app_save(request):
if request.method == 'POST':
newdoc = Document(doc_file=request.FILES.get('myfile'))
newdoc.save()
app_template.html
<form id="myform" method="POST" action="{% url 'my_app:save' %}"
enctype="multipart/form-data">
<input type="file" name="myfile">
<input type="submit">
</form>
#Result
After submitting the form I dont have any internal server error and no python traceback. But there was no image uploaded in the app/img
path and in my database, I have a blank record inserted because of newdoc.save()
query.
It is possible to work with file managing without forms?
#UPDATE
i added this on forms.py
class UploadFileForm(forms.Form):
file = forms.FileField()
and updated my app_template.html to:
<form id="myform" method="POST" action="{% url 'my_app:save' %}"
enctype="multipart/form-data">
{{ form.file }}
<input type="submit">
</form>
then i would like to include the function of loading the app_template.html in views.py
:
def index(request):
form = UploadFileForm()
return render(request, 'app_name/app_template.html', { 'form': form })
And also updated the app_save()
function to:
def app_save(request):
form = UploadFileForm(request.POST, request.FILES)
if request.method == 'POST':
if form.is_valid():
newdoc = Document(doc_file=request.FILES.get('file')) # note 'file' is the name of input file of a form.
newdoc.save()
The problem now is
There is no error found in the application but there is no media/app/img
path create with the uploaded image file. What went wrong? I think I missed something here.