1

I can upload file and images from admin panel and it looks in the specified directory, but when I do so from the HTML form everything gets saved except the file. From Django admin panel, the file gets saved to media folder and I can access it from the template. But when I try to upload from HTML form in my template, it doesn't get uploaded but everything else like comment, category, and status does.

My models:

from django.db import models
from datetime import datetime
class File(models.Model):
    title=models.CharField(max_length=200)
    document=models.FileField(upload_to='media')
    comments=models.TextField()
    uploaded_at=models.DateTimeField(default=datetime.now,blank=True)
    category=models.CharField(max_length=200,default="Education")
    status=models.CharField(max_length=200,default="Completed")

    def __str__(self):
    return self.title

My app.urls:

from django.conf.urls import url,include
from . import views
urlpatterns=[
  url(r'^$',views.index,name="index"),
  url(r'^search/$',views.search,name="search"), 
  url(r'^searchdate/$',views.searchdate,name="searchdate"),
  url(r'^addfile/$',views.addfile,name="addfile"),
  url(r'^delete-entry/(?P<pk>\d+)$', views.DeleteView, name='delete_view')
]

my urls:

from django.contrib import admin
from django.urls import path
from django.conf.urls import url,include
from django.conf.urls.static import static
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf import settings 

urlpatterns = [
  url(r'admin/', admin.site.urls),
  url(r'^$',include('domsapp.urls')),
  url(r'^files/',include('domsapp.urls'))
]

urlpatterns+=staticfiles_urlpatterns()
urlpatterns+=static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)

My settings:

STATIC_URL = '/static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR,'media')

My html form:

<form class="addForm" method="POST" action="/files/addfile/">
        {% csrf_token %}
        <input type="file" name="fileadded" id="fileinput" placeholder="Choose File">
        <hr>

        <label id="#titleLabel">Title</label>
        <hr>
        <input type="textarea" name="title" placeholder="Enter Title" class="addinput" required>
        <hr>

        <label id="#commentLabel">Comments</label>
        <hr>
        <textarea name="comments" class="addinput comment-input" required=""></textarea>

        <hr>
        <label id="#categoryLabel">Category</label>

        <select name="category" class="category-class" required>
            <option>General</option>
            <option>Education</option>
            <option>Ward</option>
            <option>District</option>

        </select>
        <hr>
        <label id="#status">Work Status</label>

        <select name="status" class="status-class" required>
            <option>Completed</option>
            <option>Pending</option>

        </select>
        <hr>
        <button id="addfileBtn" type="submit">Add File</button>

    </form>

My views.py

def addfile(request):
    if(request.method == 'POST'):
        title=request.POST['title']
        comments=request.POST['comments']
        fileadded=request.POST['fileadded']
        category=request.POST['category']
        status=request.POST['status']
        fileobj=File(title=title,comments=comments,document=fileadded(),category=category,status=status)
        fileobj.save(); 
        return redirect('/')          

What am I doing wrong?

Sergey
  • 995
  • 4
  • 14
  • 33

2 Answers2

1

Add the encType attribute to the form to allow the image data to be sent with the POST.

<form class="addForm" encType="multipart/form-data" method="POST" action="/files/addfile/">
ChadNC
  • 2,528
  • 4
  • 25
  • 39
  • 5
    Please add explanation as well along with the code, to make it understandable how it will solve the problem. – Arun Vinoth-Precog Tech - MVP Dec 09 '19 at 22:55
  • At first I am sorry I had a lesson so I didnt write a message . For example you have post there is a form. When you upload a form a picture in form didn't show in database. With Enctype . You can upload in database what you want a picture – Mehmet Kutan Dec 10 '19 at 14:51
  • An explanation of the preferred way of answering a question can be found [HERE](https://stackoverflow.com/help/how-to-answer) – ChadNC Dec 10 '19 at 17:31
0

See problem is with your html code, if you are uploading a media file you should add enctype="multipart/form-data" to form tag, try this-

<form class="addForm" method="POST" action="/files/addfile/" enctype="multipart/form-data">
    {% csrf_token %}
    <input type="file" name="fileadded" id="fileinput" placeholder="Choose File">
    <hr>

    <label id="#titleLabel">Title</label>
    <hr>
    <input type="textarea" name="title" placeholder="Enter Title" class="addinput" required>
    <hr>

    <label id="#commentLabel">Comments</label>
    <hr>
    <textarea name="comments" class="addinput comment-input" required=""></textarea>

    <hr>
    <label id="#categoryLabel">Category</label>

    <select name="category" class="category-class" required>
        <option>General</option>
        <option>Education</option>
        <option>Ward</option>
        <option>District</option>

    </select>
    <hr>
    <label id="#status">Work Status</label>

    <select name="status" class="status-class" required>
        <option>Completed</option>
        <option>Pending</option>

    </select>
    <hr>
    <button id="addfileBtn" type="submit">Add File</button>

</form>

Hope this will help.

Pankaj Sharma
  • 2,185
  • 2
  • 24
  • 50