回答編集履歴

1

追記

2021/12/21 01:06

投稿

quickquip
quickquip

スコア11040

test CHANGED
@@ -23,3 +23,173 @@
23
23
 
24
24
 
25
25
  [https://flask.palletsprojects.com/en/2.0.x/blueprints/#building-urls](https://flask.palletsprojects.com/en/2.0.x/blueprints/#building-urls)
26
+
27
+
28
+
29
+ ----
30
+
31
+
32
+
33
+ ![イメージ説明](27403129f38683d8c3b1fa6876865191.png)
34
+
35
+
36
+
37
+ application.html
38
+
39
+ ```html
40
+
41
+ <!DOCTYPE html>
42
+
43
+ <html lang="ja">
44
+
45
+ <head>
46
+
47
+ <meta charset="UTF-8">
48
+
49
+ <title>{{title}}</title>
50
+
51
+ </head>
52
+
53
+
54
+
55
+ <body>
56
+
57
+ {% block body %}
58
+
59
+ {% endblock %}
60
+
61
+ </body>
62
+
63
+ </html>
64
+
65
+ ```
66
+
67
+
68
+
69
+ index.html
70
+
71
+ ```html
72
+
73
+ {% extends 'application.html' %}
74
+
75
+
76
+
77
+ {% block body %}
78
+
79
+ {% include 'nav.html' %}
80
+
81
+ {% endblock %}
82
+
83
+ ```
84
+
85
+
86
+
87
+ nav.html
88
+
89
+ ```html
90
+
91
+ <nav id="admin_nav">
92
+
93
+ <h1>
94
+
95
+ </h1>
96
+
97
+ <ul class="admin_nav">
98
+
99
+ <li class="admin_nav_home">
100
+
101
+ <a href="{{ url_for('views.home') }}">Home</a>
102
+
103
+ </li>
104
+
105
+ </ul>
106
+
107
+ </nav>
108
+
109
+ ```
110
+
111
+
112
+
113
+ app.py
114
+
115
+ ```python
116
+
117
+ from flask import Flask
118
+
119
+
120
+
121
+ from views import skip_view
122
+
123
+
124
+
125
+ app = Flask(__name__)
126
+
127
+ app.register_blueprint(skip_view.app)
128
+
129
+
130
+
131
+ if __name__ == "__main__":
132
+
133
+ app.run(debug=True)
134
+
135
+ ```
136
+
137
+
138
+
139
+ skip_view.py
140
+
141
+ ```python
142
+
143
+ from flask import Blueprint, render_template
144
+
145
+
146
+
147
+ # Blueprintのオブジェクトを生成する
148
+
149
+ app = Blueprint('views', __name__)
150
+
151
+
152
+
153
+
154
+
155
+ @app.route('/')
156
+
157
+ def home():
158
+
159
+ title = "管理画面"
160
+
161
+ return render_template('index.html', title=title)
162
+
163
+ ````
164
+
165
+
166
+
167
+ で実行を確認できました。
168
+
169
+
170
+
171
+ nav.html
172
+
173
+ ```html
174
+
175
+ <a href="{{ url_for('.home') }}">Home</a>
176
+
177
+ ```
178
+
179
+ でも動きます。
180
+
181
+
182
+
183
+
184
+
185
+ nav.html
186
+
187
+ ```html
188
+
189
+ <a href="{{ url_for('home') }}">Home</a>
190
+
191
+ ```
192
+
193
+
194
+
195
+ で`werkzeug.routing.BuildError: Could not build url for endpoint 'home'. Did you mean 'views.home' instead?`が再現できます。