皆様いつもお世話になっております。
シンプルなブログをBottleで作ろうとしているのですが、
/post_articleにpost以外でアクセスすると、テンプレートファイルpost.html(投稿画面)を表示する様にしたいのですが、アクセスするとError: 405 Method Not Allowedが出てしまいます。
どうしたらテンプレートpostを表示できるように出来るでしょうか?
ご指摘のほどよろしくお願いします。
Python
1from bottle import route, run, template, request, redirect 2import sqlite3 3 4 5@route('/') 6def index(): 7 conn = sqlite3.connect('blog.db') 8 c = conn.cursor() 9 c.execute("SELECT id, title, text, time FROM blog ORDER BY id DESC") 10 blog_list = [] 11 for row in c.fetchall(): 12 blog_list.append({ 13 "id": row[0], 14 "title": row[1], 15 "text": row[2], 16 "time": row[3] 17 }) 18 return template('index', blog_list=blog_list) 19 20 21@route('/post_article', method=["GET, POST"]) 22def post_article(): 23 if request.method == "POST": 24 article_title = request.POST.getunicode('title') 25 article_text = request.POST.getunicode('text') 26 print(article_title) 27 print(article_text) 28 conn = sqlite3.connect('blog.db') 29 c = conn.cursor() 30 new_id = c.execute("SELECT max(id) + 1 FROM blog").fetchone()[0] 31 c.execute("INSERT INTO blog VALUES (?, ?, ?, datetime('now', 'localtime'))", (new_id, article_title, article_text)) 32 conn.commit() 33 conn.close() 34 return redirect("/") 35 else: 36 return template('post') 37 38 39if __name__ == '__main__': 40 run(debug=True, Reloader=True)
html
1index.html 2 3<!DOCTYPE html> 4<html lang="ja"> 5<head> 6 <meta charset="UTF-8"> 7 <title>記事一覧</title> 8</head> 9<a href="/post_article">投稿</a> 10<body> 11% for blog in blog_list: 12 13 <h1>{{ blog['title'] }}</h1> 14 <p>{{ blog['time'] }}</p> 15 <p>{{ blog['text'] }}</p> 16% end 17</body> 18</html>
html
1post.html 2 3<!DOCTYPE html> 4<html lang="en"> 5<head> 6 <meta charset="UTF-8"> 7 <title>投稿ページ</title> 8</head> 9<body> 10<form action="" method="post"> 11 タイトル:<input type="text" name="title"> 12 <br> 13 <br> 14 <p>投稿内容:</p> 15 <textarea name="text" id="" cols="30" rows="10"></textarea> 16 <br> 17 <input type="submit" value="投稿"> 18</form> 19</body> 20</html>
sql
1 2blog.db 3 41|なっとうを買う|ねばねばしてた|2017-08-21 17:46:19 52|みかんを買う|手が黄色くなった|2017-08-21 17:46:19 63|豆腐を買う|男前豆腐!!|2017-08-21 17:46:19 74|なっとうを買う|ねばねばしてた|2017-08-21 17:46:19 85|みかんを買う|手が黄色くなった|2017-08-21 17:46:19 96|豆腐を買う|男前豆腐!!|2017-08-21 17:46:19 107|なっとうを買う|ねばねばしてた|2017-08-21 17:46:19 118|みかんを買う|手が黄色くなった|2017-08-21 17:46:19 129|豆腐を買う|男前豆腐!!|2017-08-21 17:46:19 1310|なっとうを買う|ねばねばしてた|2017-08-21 17:46:19 1411|みかんを買う|手が黄色くなった|2017-08-21 17:46:19 1512|豆腐を買う|男前豆腐!!|2017-08-21 17:46:19 16

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/08/28 11:53