It is possible for Django to upload file without FORMS?
<p>I'm currently working with a custom HTML template not using forms on my Django app to upload an image in a specific path.</p>
<p><strong>app structure</strong></p>
<pre><code>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
</code></pre>
<p><strong>settings.py</strong></p>
<pre><code>MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
</code></pre>
<p><strong>models.py</strong></p>
<pre><code>class Document(models.Model):
    doc_file = models.FileField(upload_to='app/img')
</code></pre>
<p><strong>views.py</strong></p>
<pre><code>def app_save(request):
    if request.method == 'POST':
        newdoc = Document(doc_file=request.FILES.get('myfile'))
        newdoc.save()
</code></pre>
<p><strong>app_template.html</strong></p>
<pre><code>&lt;form id=&quot;myform&quot; method=&quot;POST&quot; action=&quot;{% url 'my_app:save' %}&quot; 
      enctype=&quot;multipart/form-data&quot;&gt;

    &lt;input type=&quot;file&quot; name=&quot;myfile&quot;&gt;
    &lt;input type=&quot;submit&quot;&gt; 
&lt;/form&gt;
</code></pre>
<p>#Result
After submitting the form I dont have any internal server error and no python traceback. But there was no image uploaded in the <code>app/img</code> path and in my database, I have a blank record inserted because of <code>newdoc.save()</code> query.</p>
<p><strong>It is possible to work with file managing without forms?</strong></p>
<hr />
<p>#UPDATE</p>
<p>i added this on <code>forms.py</code></p>
<pre><code>class UploadFileForm(forms.Form):
    file = forms.FileField()
</code></pre>
<p>and updated my <strong>app_template.html</strong> to:</p>
<pre><code>&lt;form id=&quot;myform&quot; method=&quot;POST&quot; action=&quot;{% url 'my_app:save' %}&quot; 
      enctype=&quot;multipart/form-data&quot;&gt;

    {{ form.file }}
    &lt;input type=&quot;submit&quot;&gt; 
&lt;/form&gt;
</code></pre>
<p>then i would like to include the function of loading the <strong>app_template.html</strong> in <code>views.py</code>:</p>
<pre><code>def index(request):
    form = UploadFileForm()
    return render(request, 'app_name/app_template.html', { 'form': form })
</code></pre>
<p>And also updated the <code>app_save()</code> function to:</p>
<pre><code> 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()
</code></pre>
<p><strong>The problem now is</strong></p>
<p>There is no error found in the application but there is no <code>media/app/img</code> path create with the uploaded image file. What went wrong? I think I missed something here.</p>