Skip to content

Commit 14473f4

Browse files
committed
Initial first thoughts about how file uploading might work.
1 parent d622d95 commit 14473f4

File tree

5 files changed

+45
-1
lines changed

5 files changed

+45
-1
lines changed

django_unicorn/static/js/element.js

+10
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export class Element {
3636
this.key = null;
3737
this.events = [];
3838
this.errors = [];
39+
this.file = {};
3940

4041
if (!this.el.attributes) {
4142
return;
@@ -62,6 +63,15 @@ export class Element {
6263
this[key].debounceTime = attribute.modifiers.debounce
6364
? parseInt(attribute.modifiers.debounce, 10) || -1
6465
: -1;
66+
67+
if (this.el.type === "file") {
68+
this.file = {
69+
accept: this.el.accept,
70+
multiple: this.el.multiple,
71+
};
72+
73+
// console.log("this.file", this.file);
74+
}
6575
} else if (attribute.isDb) {
6676
this.db.name = attribute.value;
6777
} else if (attribute.isPK) {

example/coffee/models.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import uuid
22

3-
from django.db.models import SET_NULL, ForeignKey, Model
3+
from django.db.models import SET_NULL, FileField, ForeignKey, Model
44
from django.db.models.fields import (
55
CharField,
66
DateField,
@@ -27,3 +27,9 @@ class Flavor(Model):
2727

2828
def __str__(self):
2929
return self.name
30+
31+
32+
class Document(Model):
33+
description = CharField(max_length=255, blank=True)
34+
document = FileField()
35+
uploaded_at = DateTimeField(auto_now_add=True)

example/unicorn/components/js.py

+9
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@
22

33
from django_unicorn.components import UnicornView
44

5+
from ..forms import DocumentForm
6+
57

68
class JsView(UnicornView):
9+
form_class = DocumentForm
10+
11+
document = ""
12+
713
states = (
814
"Alabama",
915
"Alaska",
@@ -27,5 +33,8 @@ def select_state(self, val, idx):
2733
print("select_state called idx", idx)
2834
self.selected_state = val
2935

36+
def upload_file(self):
37+
print("UPLOAD")
38+
3039
class Meta:
3140
javascript_excludes = ("states",)

example/unicorn/forms.py

+11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
11
from django import forms
22

3+
from example.coffee.models import Document
4+
35

46
class ValidationForm(forms.Form):
57
text = forms.CharField(min_length=3, max_length=10)
68
date_time = forms.DateTimeField()
79
number = forms.IntegerField()
10+
11+
12+
class DocumentForm(forms.ModelForm):
13+
class Meta:
14+
model = Document
15+
fields = (
16+
"description",
17+
"document",
18+
)

example/unicorn/templates/unicorn/js.html

+8
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,12 @@ <h2>Select2</h2>
5454
select2_datetime: {{ select2_datetime }}
5555
</div>
5656
</div>
57+
58+
<div>
59+
<form wire:submit.prevent="upload_file">
60+
<input id="blob" type="file" u:model="document" />
61+
62+
<button type="submit">Upload</button>
63+
</form>
64+
</div>
5765
</div>

0 commit comments

Comments
 (0)