It is possible for Django to upload file without FORMS?
<p>Im currently working with a custom html template not using forms on my django app to upload an img 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="myform" method="POST" action="{% url 'my_app:save' %}" 
      enctype="multipart/form-data"&gt;

    &lt;input type="file" name="myfile"&gt;
    &lt;input type="submit"&gt; 
&lt;/form&gt;
</code></pre>

<h1>Result</h1>

<p>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 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>

<h1>UPDATE</h1>

<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="myform" method="POST" action="{% url 'my_app:save' %}" 
      enctype="multipart/form-data"&gt;

    {{ form.file }}
    &lt;input type="submit"&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. Want went wrong? I think i missed something here.</p>

<p>Your backend code is fine. The problem is with a frontend. You can't submit file input with jQuery.ajax like other fields. Use <a href="https://developer.mozilla.org/en/docs/Web/API/FormData" rel="nofollow">FormData</a>:</p>

<pre><code>$('#myform').submit(function(event) {
    if(window.FormData !== undefined) {
        event.preventDefault();

        var formData = new FormData(this);
        var xhr = new XMLHttpRequest();
        xhr.open('POST', $(this).attr('action'), true);
        xhr.setRequestHeader('X-REQUESTED-WITH', 'XMLHttpRequest')            
        xhr.onreadystatechange = function() {
            if(xhr.readyState == 4) {
                if(xhr.status == 200) {
                    result = JSON.parse(xhr.responseText);
                    // Code for success upload
                }
                else {
                    // Code for error
                }
            }
        };
        xhr.send(formData);
    }
});
</code></pre>

python django file django-models