質問するログイン新規登録

質問編集履歴

1

現在作成中のコードを追加し、質問の内容を変更します。

2017/09/04 07:31

投稿

am120sec
am120sec

スコア14

title CHANGED
@@ -1,1 +1,1 @@
1
- Djangoでwysiwygを自作したい。
1
+ Djangoでwysiwyg(quill)使用する方法
body CHANGED
@@ -2,4 +2,134 @@
2
2
  テキスト、画像、テキストのように表示できません。
3
3
 
4
4
  wysiwygで作成した部分はHTMLタグを含めtextfieldに格納すればよいのでしょうか。
5
- 参考になるサイト等があれば教えてください。
5
+ 参考になるサイト等があれば教えてください。
6
+
7
+ 2017/9/4 追記
8
+ 現在作成中のコードを追加し、質問の内容を変更します。
9
+
10
+ 1. quill(リッチテキストエディタ)上でアップロードする画像をどのようにしてDjangoと連携すればよいか
11
+ 2. quillで作成するリッチテキストエディタのフィールドはtext = models.TextField()でよいか
12
+ 3. HTMLでフォームのデータを取得し、表示する場合、{{post.quill}}のようにして、リッチテキストフィールドを呼び出せるか
13
+
14
+ 現在はDjangoで作成した、ただのテキストフォーム、画像の保存、表示は可能な状態ですが
15
+ 以上の質問内容の段階で手間取っている状況となります。
16
+
17
+ ご教授のほどよろしくお願い致します。
18
+
19
+ ```HTML
20
+ <!--index.html -->
21
+
22
+ {% load staticfiles %}
23
+ <!DOCTYPE html>
24
+ <html lang="ja">
25
+ <html>
26
+ <head>
27
+ <meta charset=utf-8 />
28
+ <link rel="stylesheet" href="{% static 'css/jquery-ui-1.12.1.css' %}">
29
+ <link rel="stylesheet" href="https://cdn.quilljs.com/1.3.1/quill.snow.css"/>
30
+ </head>
31
+ <body>
32
+ <form action= "{% url 'app:index' %}" method="POST" class="post-form" enctype="multipart/form-data">{% csrf_token %}
33
+ <p>{{ form1.toukou }}</p>
34
+ <p>{{ form1.tensuu }}</p>
35
+ <div id="standalone-container">
36
+ <div id="toolbar-container">
37
+ <span class="ql-formats">
38
+ <button class="ql-header" value="2"></button>
39
+ <button class="ql-blockquote"></button>
40
+ </span>
41
+ <span class="ql-formats">
42
+ <button class="ql-link"></button>
43
+ <button class="ql-image"></button>
44
+ <button class="ql-video"></button>
45
+ </span>
46
+ </div>
47
+ <div id="editor-container"></div>
48
+ </div>
49
+
50
+ <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.7.1/katex.min.js"></script>
51
+ <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js"></script>
52
+ <script type="text/javascript" src="https://cdn.quilljs.com/1.3.1/quill.min.js"></script>
53
+ <script type="text/javascript">
54
+ var quill = new Quill('#editor-container', {
55
+ modules: {
56
+ formula: true,
57
+ syntax: true,
58
+ toolbar: '#toolbar-container'
59
+ },
60
+ placeholder: 'ここに入力',
61
+ theme: 'snow'
62
+ });
63
+ </script>
64
+
65
+ <p><button type="submit" class="save btn btn-default">Save</button></p>
66
+ </form>
67
+ </body>
68
+ ```
69
+
70
+ ```Django
71
+ # model.py
72
+
73
+ from django.db import models
74
+ from django.utils import timezone
75
+
76
+ TENSUU_CHOICES = [(i,str(i)) for i in range(1, 11)]
77
+ class Action(models.Model):
78
+ #target = models.ForeignKey('Title_Post')
79
+ text = models.TextField()
80
+ author = models.ForeignKey('auth.User')
81
+ tensuu = models.DecimalField(max_digits=2,decimal_places=0,choices=TENSUU_CHOICES)
82
+ thumnail = models.ImageField(upload_to='static/images/', null=True, blank=True)
83
+ published_date = models.DateTimeField(blank=True, null=True)
84
+ def publish(self):
85
+ self.published_date = timezone.now()
86
+ self.save()
87
+ def __str__(self):
88
+ return self.text
89
+ ```
90
+
91
+ ```Django
92
+ # view.py
93
+
94
+ from django.shortcuts import render,redirect,render, get_object_or_404
95
+ from django.contrib.auth.decorators import login_required
96
+ from django.views.decorators.http import require_POST
97
+ from django.utils import timezone
98
+ from .models import Action
99
+ from .forms import Action_Form
100
+ from django.http import HttpResponse
101
+ from django.http import JsonResponse
102
+ from django.core import serializers
103
+ import json
104
+
105
+ def index(request):
106
+ posts = Action.objects.filter(published_date__lte=timezone.now()).order_by('-published_date')
107
+ if request.method == "POST":
108
+ f = Action_Form(request.POST,request.FILES)
109
+ if f.is_valid():
110
+ post = f.save(commit=False)
111
+ post.author = request.user
112
+ post.published_date = timezone.now()
113
+ post.save()
114
+ return redirect('app:index')
115
+ else:
116
+ f = Action_Form()
117
+ return render(request, 'app/index.html', {'posts': posts,'form1':f})
118
+
119
+ ```
120
+
121
+ ```Django
122
+ # form.py
123
+
124
+ from django import forms
125
+ from .models import Action,Title_Post,Status
126
+
127
+ class Action_Form(forms.ModelForm):
128
+ thumnail = forms.FileField(label='Select a image.',required=False, )
129
+ class Meta:
130
+ model = Action
131
+ fields = ('tensuu','text','thumnail',)
132
+
133
+
134
+
135
+ ```