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?