Created
December 16, 2018 20:04
-
-
Save modernNeo/5d2c203c8b98220d78b92ef8ad953faa to your computer and use it in GitHub Desktop.
files for file form
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.contrib import admin | |
from .models import Example, ExampleFile , Example2 | |
class ExampleFileInline(admin.TabularInline): | |
model = ExampleFile | |
class Example2Admin(admin.ModelAdmin): | |
inlines = [ExampleFileInline] | |
admin.site.register(Example) | |
admin.site.register(Example2, Example2Admin) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% extends 'base.html' %} | |
{% load staticfiles %} | |
{% block title %}Form example{% endblock %} | |
{% block head %} | |
{{ block.super }} | |
<link rel="stylesheet" href="{% static "ajaxuploader/css/fileuploader.css" %}"> | |
<link rel="stylesheet" href="{% static "example.css" %}"> | |
{% endblock %} | |
{% block body %} | |
<p>example_form</p> | |
<form | |
method="POST" | |
enctype="multipart/form-data" | |
id="example-form" | |
> | |
{% csrf_token %} | |
{{ form }} | |
<input class="btn btn-primary" type="submit" value="Save" /> | |
</form> | |
{% include 'django_file_form/upload_template.html' %} | |
<script src="{% static "jquery/dist/jquery.js" %}"></script> | |
<script src="{% static "ajaxuploader/js/fileuploader.js" %}"></script> | |
<script src="{% static "file_form/file_form.js" %}"></script> | |
<script> | |
$(function() { | |
initUploadFields($('#example-form')); | |
}); | |
</script> | |
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.urls import reverse | |
import django_bootstrap3_form | |
from django_file_form.forms import UploadedFileField, MultipleUploadedFileField, FileFormMixin | |
from .models import Example , Example2, ExampleFile | |
class BaseForm(FileFormMixin, django_bootstrap3_form.BootstrapForm): | |
title = django_bootstrap3_form.CharField() | |
class ExampleForm(BaseForm): | |
input_file = UploadedFileField() | |
def save(self): | |
Example.objects.create( | |
title=self.cleaned_data['title'], | |
input_file=self.cleaned_data['input_file'] | |
) | |
self.delete_temporary_files() | |
class MultipleFileExampleForm(BaseForm): | |
input_file = MultipleUploadedFileField() | |
def save(self): | |
example = Example2.objects.create( | |
title=self.cleaned_data['title'] | |
) | |
print("[MultipleFileExampleForm] self.cleaned_data="+str(self.cleaned_data)) | |
for f in self.cleaned_data['input_file']: | |
print("[MultipleFileExampleForm] input_file="+str(f)) | |
ExampleFile.objects.create( | |
example=example, | |
input_file=f | |
) | |
self.delete_temporary_files() | |
class ExistingFileForm(ExampleForm): | |
def get_upload_url(self): | |
return reverse('example_handle_upload') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import six | |
from django.db import models | |
from django.core.files.storage import FileSystemStorage | |
from django.conf import settings | |
class Example(models.Model): | |
fs = FileSystemStorage(location=settings.MEDIA_ROOT) | |
title = models.CharField(max_length=255) | |
input_file = models.FileField(max_length=255, upload_to='example', storage=fs) | |
class Example2(models.Model): | |
title = models.CharField(max_length=255) | |
class ExampleFile(models.Model): | |
fs = FileSystemStorage(location=settings.MEDIA_ROOT) | |
example = models.ForeignKey(Example2, related_name='files', on_delete=models.CASCADE) | |
input_file = models.FileField(max_length=255, upload_to='example', storage=fs) | |
def __str__(self): | |
return six.text_type(self.input_file) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.conf.urls import url | |
from . import views | |
urlpatterns = ( | |
url(r'^$', views.ExampleView.as_view(), name='example'), | |
url(r'^success$', views.ExampleSuccessView.as_view(), name='example_success'), | |
url(r'^multiple$', views.MultipleExampleView.as_view(), name='multiple_example'), | |
url(r'^existing/(?P<id>\d+)$', views.ExistingFileExampleView.as_view(), name='existing_file_example'), | |
url(r'^handle_upload$', views.handle_upload, name='example_handle_upload'), | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pathlib import Path | |
from django.views import generic | |
from django.urls import reverse | |
from django_file_form.forms import ExistingFile | |
from django_file_form.uploader import FileFormUploader | |
from . import forms | |
from .models import Example #, Example2, ExampleFile | |
class BaseFormView(generic.FormView): | |
template_name = 'example_form.html' | |
def get_success_url(self): | |
return reverse('example_success') | |
def form_valid(self, form): | |
form.save() | |
return super(BaseFormView, self).form_valid(form) | |
class ExampleView(BaseFormView): | |
form_class = forms.ExampleForm | |
class ExampleSuccessView(generic.TemplateView): | |
template_name = 'success.html' | |
class MultipleExampleView(BaseFormView): | |
form_class = forms.MultipleFileExampleForm | |
class ExistingFileExampleView(BaseFormView): | |
form_class = forms.ExistingFileForm | |
def get_form_kwargs(self): | |
form_kwargs = super(ExistingFileExampleView, self).get_form_kwargs() | |
example = Example.objects.get(id=self.kwargs['id']) | |
if example.input_file: | |
name = Path(example.input_file.name).name | |
form_kwargs['initial'] = dict( | |
input_file=ExistingFile(name) | |
) | |
return form_kwargs | |
handle_upload = FileFormUploader() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment