Fixed back-end form issue

This commit is contained in:
Marcin Armacki 2020-04-23 02:52:56 +02:00
parent 0931fe73fa
commit 11bb40bc13
4 changed files with 23 additions and 5 deletions

View File

@ -4,4 +4,5 @@ from prototype.filehandler.models import Document
class DocumentForm(forms.ModelForm):
class Meta:
model = Document
fields = ('description', 'document', )
# fields = ('description', 'document', )
fields = ('file', )

View File

@ -3,5 +3,5 @@ from django.db import models
class Document(models.Model):
description = models.CharField(max_length=255, blank=True)
document = models.FileField(upload_to='documents/')
file = models.FileField(upload_to='documents/')
uploaded_at = models.DateTimeField(auto_now_add=True)

View File

@ -1,20 +1,31 @@
from django.shortcuts import render, redirect
from django.conf import settings
from django.core.files.storage import FileSystemStorage
from django.views.decorators.csrf import csrf_exempt
from django.http import HttpResponseRedirect
from prototype.filehandler.models import Document
from prototype.filehandler.forms import DocumentForm
def home(request):
documents = Document.objects.all()
return render(request, 'core/home.html', { 'documents': documents})
@csrf_exempt
def model_form_upload(request):
if request.method == 'POST':
form = DocumentForm(request.POST, request.FILES)
print("POST: " + str(request.POST))
print("FILES: " + str(request.FILES))
if form.is_valid():
form.save()
return redirect('home')
return HttpResponseRedirect('sucess')
else:
print("Error: " + str(form.errors.as_data()))
print("Content-type: " + str(request.content_type))
print("Body: " + str(request.FILES))
else:
form = DocumentForm()
return render(request, 'core/model_form_upload.html', {

View File

@ -25,7 +25,7 @@ SECRET_KEY = 'l@x1!0zws!($=!tz4n5f^p4z8o1j!r0wn8nz(jzg-k3i%b6(37'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = ['127.0.0.1']
ALLOWED_HOSTS = ['127.0.0.1', 'localhost']
# Application definition
@ -37,13 +37,14 @@ INSTALLED_APPS = [
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'prototype.filehandler',
'corsheaders',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
@ -125,3 +126,8 @@ STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
CORS_ALLOW_CREDENTIALS = True
CORS_ORIGIN_WHITELIST = [
'http://localhost:4200',
]