Django:Templateで意図したものと別のファイルが読み込まれる時の原因と対策
Django:Templateで意図したものと別のファイルが読み込まれる時の原因と対策
この場合、多くがTemplateディレクトリに置いたファイルの読まれ方の理解不足が原因です。
そして、多くの場合『同一プロジェクトに複数アプリ』という構成で構築している事でしょう。
私もこの【Templateディレクトリ】のファイルの扱いには悩まされました。
以降は具体的な例で解説していきます。
Templateが意図した通りに動かなかった事ありませんか?
私が経験したのはこんな動作です。
- 複数アプリを起動し「それぞれのサイドナビ」を「それぞれのTemplate直下にsidenav.htmlとして保存」したのにアプリごとにサイドナビの切り替えができない。
- TemplateSyntaxError が出たけど読み込もうとしたページで利用していないはずの 引数に対してエラーを指摘された。
この画像は1の例です。実際に私が作成している「自分専用アプリ」のサイドメニュー画面になります。この例ではModeChange内にあるリストをクリックすると其々のページにJUMPし、専用メニューが出てくるようにしています。
店舗分析では店舗分析専用の遷移、パーソナルCMSではシステム屋のとしての遷移といったようにアプリ毎にメニュー構成を変えたいという要望は多くあると思います。
ちなみに、Cloverは娘の自由研究用のDBです。私のグラフ作成練習にちょうどいいのでパーソナルシステムに組み込んでしまいました。
こういったアプリ毎のサイドメニューの切り替えをしたかったのですが、何故か意図した通りに動いてくれなかったんですよね。
Templateディレクトリは全てのアプリで統合管理されている
私の経験した上の項目1-2ですが根本原因はTemplateディレクトリの理解不足でした。
意図したとおりに動くTemplateディレクトリ構成
教本などでは「アプリのディレクトリにTemplateディレクトリを作成する」と記載されていることが多いので、多くの方がこんな感じの構成(アプリごとにTemplateがある構成)を取っているのではないかと思います。
例えばこのような構成のTemplateディレクトリがあったとします。
├─ プロジェクト
│ └─ settings.py
(中略)
├─ アプリ1
│ ├─ __pycache__
│ ├─ migrations
│ ├─ templates
│ │ ├─ app01
│ │ │ ├─ index.html
│ │ │ ├─ edit.html
│ │ │ ├─ create.html
│ │ │ └─ sidnavi.html
│ │ └─ base.html
(中略)
├─ アプリ2
│ ├─ __pycache__
│ ├─ migrations
│ ├─ templates
│ │ ├─ app02
│ │ │ ├─ index.html
│ │ │ ├─ edit.html
│ │ │ ├─ create.html
│ │ │ └─ sidnavi.html
│ │ └─ footer.html
(中略)
├─ アプリ3
│ ├─ __pycache__
│ ├─ migrations
│ ├─ templates
│ │ ├─ app03
│ │ │ ├─ index.html
│ │ │ ├─ edit.html
│ │ │ ├─ create.html
│ │ │ └─ sidnavi.html
│ │ └─test.html
(中略)
└── manage.py
これ、Djangoの中では下のように見えています。
├─ プロジェクト
│ └─ settings.py
(中略)
├─ アプリ1
│ ├─ __pycache__
│ ├─ migrations
│ ├─ templates
│ │ ├─ app01
│ │ │ ├─ index.html
│ │ │ ├─ edit.html
│ │ │ ├─ create.html
│ │ │ └─ sidnavi.html
│ │ ├─ app02
│ │ │ ├─ index.html
│ │ │ ├─ edit.html
│ │ │ ├─ create.html
│ │ │ └─ sidnavi.html
│ │ ├─ app03
│ │ │ ├─ index.html
│ │ │ ├─ edit.html
│ │ │ ├─ create.html
│ │ │ └─ sidnavi.html
│ │ ├─ test.html
│ │ ├─ footer.html
│ │ └─ base.html
(中略)
├─ アプリ2
│ ├─ __pycache__
│ ├─ migrations
│ ├─ templates
│ │ ├─ app01
│ │ │ ├─ index.html
│ │ │ ├─ edit.html
│ │ │ ├─ create.html
│ │ │ └─ sidnavi.html
(中略)
├─ アプリ3
│ ├─ __pycache__
│ ├─ migrations
│ ├─ templates
│ │ ├─ app01
│ │ │ ├─ index.html
│ │ │ ├─ edit.html
│ │ │ ├─ create.html
│ │ │ └─ sidnavi.html
(中略)
└── manage.py
どのアプリから見ても『分割して書いたはず』のTemplateがまとめて置いてありますよね。
なので、共通して使う base.html のようなファイルとアプリを超えて流用しないファイルはディレクトリ名で区分けする必要があります。
Templateディレクトリの間違えた構成例
上記理由により、こんな書き方すると意図した場所を読み込まず「作ったはずのページじゃない」とか「書いてない引数がエラーを起こす」といった現象が発生します。
├─ プロジェクト
│ └─ settings.py
(中略)
├─ アプリ1
│ ├─ __pycache__
│ ├─ migrations
│ ├─ templates
│ │ ├─ CRUD
│ │ │ ├─ index.html
│ │ │ ├─ edit.html
│ │ │ └─ create.html
│ │ ├─ sidnavi.html
│ │ ├─ base.html
│ │ └─ footer.html
(中略)
├─ アプリ2
│ ├─ __pycache__
│ ├─ migrations
│ ├─ templates
│ │ ├─ CRUD
│ │ │ ├─ index.html
│ │ │ ├─ edit.html
│ │ │ └─ create.html
│ │ ├─ sidnavi.html
│ │ ├─ base.html
│ │ └─ footer.html
(中略)
├─ アプリ3
│ ├─ __pycache__
│ ├─ migrations
│ ├─ templates
│ │ ├─ CRUD
│ │ │ ├─ index.html
│ │ │ ├─ edit.html
│ │ │ └─ create.html
│ │ ├─ sidnavi.html
│ │ ├─ base.html
│ │ └─ footer.html
(中略)
└── manage.py
この機能はうまく使えば非常にありがたい
具体的に言えば【共通パーツ化】の実装が簡単です。
例えば「分析用のグラフ描写を外部パーツ化する」とか「内部アプリの基礎構成を共通化する」とか。
グラフ描写で具体的に流れを追ってみます。
グラフ描写の外部パーツ化について
Django:複数アプリで利用する関数を共通化する方法
この記事で書いた通り、関数はUSEすることでどのアプリからも呼び出すことができます。
そして、Templateディレクトリに配置するhtmlはどのアプリからも同一ディレクトリにあるように呼び出すことができます。例えば、アプリ1にのみGraphsディレクトリを作成した下のような構成を作ったとします。
├─ プロジェクト
│ └─ settings.py
(中略)
├─ アプリ1
│ ├─ __pycache__
│ ├─ migrations
│ ├─ templates
│ │ ├─ app01
│ │ │ ├─ index.html
(中略)
│ │ ├─ Graphs
│ │ │ ├─ flot_line.html
│ │ │ ├─ echart_2bar.html
│ │ │ ├─ echart_scatter.html
│ │ │ └─ echart_line.html
│ │ ├─ base.html
│ │ └─ footer.html
(中略)
├─ アプリ2
│ ├─ __pycache__
│ ├─ migrations
│ ├─ templates
│ │ ├─ app02
│ │ │ ├─ index.html
(中略)
├─ アプリ3
│ ├─ __pycache__
│ ├─ migrations
│ ├─ templates
│ │ ├─ app03
│ │ │ ├─ index.html
(中略)
└── manage.py
この構成でアプリ1/Template/Graphs/echart_scatter.htmlを呼び出すには下のようにIncludeします。
▼アプリ1/Template/app01/index.html でInclude
1 2 |
<!-- 分布図 --> {% include 'Graph/echart_scatter.html'%} |
▼アプリ2/Template/app02/index.html でInclude
1 2 |
<!-- 分布図 --> {% include 'Graph/echart_scatter.html'%} |
▼アプリ3/Template/app03/index.html でInclude
1 2 |
<!-- 分布図 --> {% include 'Graph/echart_scatter.html'%} |
全く同じですね。
描写する値は引数で JavaScript に渡すわけですからHTML的にはこんな感じで仕上がってしまいます。
■アプリ1/Template/Graphs/echart_scatter.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<!-- 必要なJS はページ上部で読み込ませましょう。 ページ下部で読み込ませると、echart_scatter.html 内のJS適用がエラーを発するため 外部パーツとして完結できません。 ※ 引数のJS受け渡しのScriptをincludeするファイル側に書き込む必要が出てきます。 --> <div class="x_content"> <!-- Graph --> <div class="echart_scatter" style="height:350px;"></div> <!-- Graph --> </div> <script> var Scatter_01 = {{ Scatter.0|safe }} var Scatter_02 = {{ Scatter.1|safe }} var Scatter_title01 = '{{Scatter.2}}' var Scatter_title02 = '{{Scatter.3}}' var Scatter_dataname01 = '{{Scatter.4}}' var Scatter_dataname02 = '{{Scatter.5}}' var Scatter_XUnit = '{{Scatter.6}}' var Scatter_YUnit = '{{Scatter.7}}' </script> |
EChartの分散図だったら下のJSで描写できます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
/* Echart */ function init_echarts() { if (typeof(echarts) === 'undefined') { return; } var theme = { color: [ '#26B99A', '#34495E', '#BDC3C7', '#3498DB', '#9B59B6', '#8abb6f', '#759c6a', '#bfd3b7' ], title: { itemGap: 8, textStyle: { fontWeight: 'normal', color: '#408829' } }, dataRange: { color: ['#1f610a', '#97b58d'] }, toolbox: { color: ['#408829', '#408829', '#408829', '#408829'] }, tooltip: { backgroundColor: 'rgba(0,0,0,0.5)', axisPointer: { type: 'line', lineStyle: { color: '#408829', type: 'dashed' }, crossStyle: { color: '#408829' }, shadowStyle: { color: 'rgba(200,200,200,0.3)' } } }, dataZoom: { dataBackgroundColor: '#eee', fillerColor: 'rgba(64,136,41,0.2)', handleColor: '#408829' }, grid: { borderWidth: 0 }, categoryAxis: { axisLine: { lineStyle: { color: '#408829' } }, splitLine: { lineStyle: { color: ['#eee'] } } }, valueAxis: { axisLine: { lineStyle: { color: '#408829' } }, splitArea: { show: true, areaStyle: { color: ['rgba(250,250,250,0.1)', 'rgba(200,200,200,0.1)'] } }, splitLine: { lineStyle: { color: ['#eee'] } } }, timeline: { lineStyle: { color: '#408829' }, controlStyle: { normal: { color: '#408829' }, emphasis: { color: '#408829' } } }, k: { itemStyle: { normal: { color: '#68a54a', color0: '#a9cba2', lineStyle: { width: 1, color: '#408829', color0: '#86b379' } } } }, map: { itemStyle: { normal: { areaStyle: { color: '#ddd' }, label: { textStyle: { color: '#c12e34' } } }, emphasis: { areaStyle: { color: '#99d2dd' }, label: { textStyle: { color: '#c12e34' } } } } }, force: { itemStyle: { normal: { linkStyle: { strokeColor: '#408829' } } } }, chord: { padding: 4, itemStyle: { normal: { lineStyle: { width: 1, color: 'rgba(128, 128, 128, 0.5)' }, chordStyle: { lineStyle: { width: 1, color: 'rgba(128, 128, 128, 0.5)' } } }, emphasis: { lineStyle: { width: 1, color: 'rgba(128, 128, 128, 0.5)' }, chordStyle: { lineStyle: { width: 1, color: 'rgba(128, 128, 128, 0.5)' } } } } }, gauge: { startAngle: 225, endAngle: -45, axisLine: { show: true, lineStyle: { color: [ [0.2, '#86b379'], [0.8, '#68a54a'], [1, '#408829'] ], width: 8 } }, axisTick: { splitNumber: 10, length: 12, lineStyle: { color: 'auto' } }, axisLabel: { textStyle: { color: 'auto' } }, splitLine: { length: 18, lineStyle: { color: 'auto' } }, pointer: { length: '90%', color: 'auto' }, title: { textStyle: { color: '#333' } }, detail: { textStyle: { color: 'auto' } } }, textStyle: { fontFamily: 'Arial, Verdana, sans-serif' } }; //echart Scatter if ($('.echart_scatter').length) { var echartScatter = echarts.init(document.getElementsByClassName('echart_scatter'), theme); echartScatter.setOption({ title: { text: Scatter_title01, subtext: Scatter_title02 }, tooltip: { trigger: 'axis', showDelay: 0, axisPointer: { type: 'cross', lineStyle: { type: 'dashed', width: 1 } } }, legend: { data: [Scatter_dataname02, Scatter_dataname01] }, toolbox: { show: true, feature: { saveAsImage: { show: true, title: "Save Image" } } }, xAxis: [{ type: 'value', scale: true, axisLabel: { formatter: '{value} '+ Scatter_XUnit } }], yAxis: [{ type: 'value', scale: true, axisLabel: { formatter: '{value} '+ Scatter_YUnit } }], series: [{ name: Scatter_dataname01, type: 'scatter', tooltip: { trigger: 'item', formatter: function(params) { if (params.value.length > 1) { return params.seriesName + ' :<br/>' + params.value[0] + Scatter_XUnit + ' ' + params.value[1] + Scatter_YUnit; } else { return params.seriesName + ' :<br/>' + params.name + ' : ' + params.value + Scatter_YUnit; } } }, data: Scatter_01, markPoint: { data: [{ type: 'max', name: 'Max' }, { type: 'min', name: 'Min' }] }, markLine: { data: [{ type: 'average', name: 'Mean' }] } }, { name: Scatter_dataname02, type: 'scatter', tooltip: { trigger: 'item', formatter: function(params) { if (params.value.length > 1) { return params.seriesName + ' :<br/>' + params.value[0] + Scatter_XUnit + ' ' + params.value[1] + Scatter_YUnit; } else { return params.seriesName + ' :<br/>' + params.name + ' : ' + params.value + Scatter_YUnit; } } }, data: Scatter_02, markPoint: { data: [{ type: 'max', name: 'Max' }, { type: 'min', name: 'Min' }] }, markLine: { data: [{ type: 'average', name: 'Mean' }] } }] }); } } $(document).ready(function() { init_echarts(); }); |
外部パーツ化のメリットとは
外部パーツ化は自分が楽になるために行う作業です。
単純な単体実装であれば、共通パーツなどは作成しない方が作業効率は増します。
理由は【このデータはこうなっている】というルールを実装で使えるからです。
例えば、X軸は日付データという確定した内容があれば『日付か確認して日付だったら表示できる姿に変更する』といった処理を踏む必要がありません。
ただし、この場合は同じようなSCRIPTを何回も書くことになります。
反復練習としてはとても良いのですが、プログラマは時間も商売道具です。
早く書き上げるに越したことはないとなれば、一回経験したもので外部パーツ化できるものは積極的にパーツ化して次の案件に生かしていけばOKですよね。
まとめ
先回の記事でグラフの外部パーツ化について触れておきながら、Templateのことは何も書かなかったなと思い記事化してみました。
さぁ、今日も頑張りましょう!
-
前の記事
Django:複数アプリで利用する関数を共通化する方法 2021.02.19
-
次の記事
Django:this querydict instance is immutable request.data エラーの原因と対策 2021.02.26
コメントを残す