Добавил обработку 404 ошибок +поправил index page.

This commit is contained in:
Lulzette 2021-09-07 07:14:31 +03:00
parent 488ee80e09
commit cca16d69b5
2 changed files with 21 additions and 4 deletions

View File

@ -3,9 +3,20 @@
DB: pythoncms DB: pythoncms
Table: posts Table: posts
Format: Format:
```
{ {
"_id": "0123456789", "_id": "0123456789",
"name": "Title", "name": "Title",
"text": "Hello, this is post" "text": "Hello, this is post"
} }
```
Index page:
```
{
"_id": "0123456789",
"name": "/",
"text": "Hello, this is index page"
}
```

12
main.py
View File

@ -1,5 +1,5 @@
#!/usr/bin/python3 #!/usr/bin/python3
from bottle import route, run, template, debug, request from bottle import abort, route, run, template, debug, request
import pymongo import pymongo
mongoclient = pymongo.MongoClient('localhost', 27017) mongoclient = pymongo.MongoClient('localhost', 27017)
@ -12,7 +12,10 @@ def post(name):
''' '''
Get post Get post
''' '''
return posts.find_one({'name':name})['text'] try:
return posts.find_one({'name':name})['text']
except TypeError:
return abort(404, 'No such page')
@route('/post/<name>', method='POST') @route('/post/<name>', method='POST')
@ -52,7 +55,10 @@ def all_posts():
@route('/') @route('/')
def index(): def index():
return "Hello" try:
return posts.find_one({'name':'/'})['text']
except TypeError:
return abort(404, 'No such page')
if __name__ == '__main__': if __name__ == '__main__':