## Overview ##
This template has been roughly set up around a 'Model View Controller' or MVC design. This splits the functions of your site into three distinct categories:
- Models: Handles the program logic
- Views: Handles the returned HTML pages
- Controllers: Handles the requests for pages that the user sends
Typically the user will request a page (from the controller), the request will be interpreted and then passed to the program logic (the model) which will generate a new page to return to the user (the view).
This setup has been used to keep the logic of the code and the logic of the site separate.
If your site starts sprawling enough, you may wish to create distinct folders for each of these categories and then split the code into separate files within these folders.
## Controllers ##
This is the handler for your server requests. Your get and post decorators will go here, before calling a model to handle any actual lifting.
All of your bottle relevant code should live here. The rest of your code should not be exposed to anything to do with bottle. Similarly, no SQL or other 'database' or logical code should live up here. You have been provided with static file loads for javascript, css and image files from the javascript, css and img directories respectively.
## Models ##
The brains of the operation, here we perform whatever actual code we need, before calling our view object to return the templated HTML. This area should make calls to any 'databases' or other persistent storage that is handling user or other data.
## HTTPS ##
The library bottle used in the template does not support ssl. To enable bottle server in the template support https/ssl in python, one python library of WSGI that Bottle supports is needed. There are many choices, such as Gunicorn, CherryPy, etc. If you are still unclear, just google sth like how to enable Bottle server support HTTPS python. It is definitely fine if you use some libraries other than Bottle.
## Views ##
Simply loads our HTML files and renders any elements of the template.
It might be helpful to have an explanation of the View class. You do not actually need this to use bottle at all, but it's a primitive method of automating loading and rendering HTML templates.If you already have your own method of managing this, please feel free to disregard the explanation below. If you don't like how parts of this have been implemented, you are more than free to modify it for your own use.
The template has been modified to be more explicit and verbose in what it is doing rather than strictly the most efficient or Pythonesque method.
If you're completely lost: the point of this code is to "render HTML", all this really means is that we're going to take a string, modify it a bit and return it. HTML is effectively just some specially formatted text. I would suggest starting by looking at and building the polling site in tutorial 3.
All the code below is just to read text from a file, replace a few keywords and then return it.
Once you've completed the polling site you might wonder if hard coding all the HTML responses is the most efficient method. Depending on the size of the project it might or it might not be. It is entirely possible to hard code all the pages required for this assignment. However, one method of managing HTML is to store it in a separate file, then read and return it when required.
This is performed by the load_template method. It opens a file given by the filename argument that is found at the filename path (here it's "/template") and with the template extension (here it's ".html").
```python
def load_template(self, filename):
path = self.template_path + filename + self.template_extension
file = open(path, 'r')
text = ""
for line in file:
text += line
file.close()
return text
```
Of course some times you want dynamically generated HTML (for example, displaying a username after logging in). In order to do this we're going to need to do some string operations.
```python
def string_format(string, format_dict):
for keyword in format_dict:
string = string.replace('{' + keyword + '}', format_dict[keyword])
return string
string = "Thanks for logging in {user}."
format_dict = {"user":"Anon"}
formatted_string = string_format(string, format_dict)
print(formatted_string)
```
Or instead we can use the Python format function:
```python
string = "My hovercraft is full of {things}"
formatted_string = string.format(things="eels")
print(formatted_string)
```
The simple_render method within View calls the Python safe_substitute function on a given template, this works in a very similar fashion to the format function we saw previously.
```python
def simple_render(self, template, **kwargs):
template = string.Template(template)
template = template.safe_substitute(**kwargs)
return template
```
\*\*kwargs is a Python default method of passing arbitrary keyword arguments (see tutorial 1!) as a dictionary, this lets us pass our dictionary around without actually having to worry about the contents.
Now let's say that there are some "global" dynamic template options we want to use, things that we can just pass into a template when it's called. For this we'll follow exactly the same method as above, but store these "global renders" as a member variable.
```python
def __init__(self,
template_path="templates/",
template_extension=".html",
**kwargs):
self.template_path = template_path
self.template_extension = template_extension
self.global_renders = kwargs
```
Here we're using \*\*kwargs again for the "global renders"...
```python
def render(self, template, **kwargs):
'''
render
A more complex render that joins global settings with local settings
:: template :: The template to use
:: kwargs :: The local key value pairs to pass to the template
'''
# Construct the head, body and tail separately
rendered_body = self.simple_render(body_template, **kwargs)
rendered_head = self.simple_render(header_template, **kwargs)
rendered_tail = self.simple_render(tailer_template, **kwargs)
# Join them
rendered_template = rendered_head + rendered_body + rendered_tail
# Apply any global templating that needs to occur
rendered_template = self.global_render(rendered_template)
# Return the template
return rendered_template
```
...and here we build the header, tailer and body, join them together and then apply any global replacements we might need
Lastly let's consider some generic headers we can add to every page on the site (in the case of Drawing Straws, this is the menu bar and the image on every page), and the "tailer" to properly enclose the HTML. This can be more efficiently managed using proper rendering calls but this template was not built for flexibility so much as for ease of use on a few small sites.
Putting it all together we now get our load and render method:
```python
def load_and_render(self, filename, header="header", tailer="tailer", **kwargs):
'''
Loads and renders templates
:: filename :: Name of the template to load
:: header :: Header template to use, swap this out for multiple headers
:: tailer :: Tailer template to use
:: kwargs :: Keyword arguments to pass
'''
body_template = self.load_template(filename)
header_template = self.load_template(header)
tailer_template = self.load_template(tailer)
rendered_template = self.render(
body_template=body_template,
header_template=header_template,
tailer_template=tailer_template,
**kwargs)
return rendered_template
```
And that's the View class. It's quite a simple templ
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
info2222.zip 用python实现网络安全 (224个子文件)
02e2b8525127a76aa7c5a1f5c29116bc8b1766 189KB
0ce403ef9790b6b6bccb01784cad6d50525910 233B
0f72170df615680e24c1f8ad5650945da3121f 3KB
112734bd3c028e3ec91e33128d234610dd996e 98B
282f5c55bb9fa6e574542cf4313ae08007ab00 1KB
3c396c5032c4aad293eb6f86a2eb3ded9c2e70 172B
3ea3f5135fbc96f122f6e4072232cab8496cb0 310B
443d7b51bd719bb71433b73c73c23b03921ddd 1KB
4781528b94a698ec07ab5353f30fef2c5be543 31B
505733c5e166a810016055fb957ad47b84535e 1KB
5718ec1c0e821358deba919dfb4c8403f8851c 1KB
5975f0ca043801f8c02ec87e599b7b4f7e7241 4KB
5ed4ce4f33df990c724cd2ca32f74a4c76e1c3 311B
5ee54173f10df0d70d2a394d69c257ef6a511c 81B
70ef7320fc37d2e857a5d9d1e7f6691554b0be 802B
79633d2141b14e35f329b1fef086907be3887e 43B
8e916709989ecb929186a39f02c20aeb8ccdbf 232B
914cf0ba93009408cceb1e3a81c17d221d555f 195B
9293e87be816ac348e6fb5e079ee59483fdac4 88B
988a29859f3229c68b442476caaa610cbbbe27 130B
9de29bb2d1d6434b8b29ae775ad8c2e48c5391 15B
._01 642B
._02e2b8525127a76aa7c5a1f5c29116bc8b1766 644B
._0b 642B
._0ce403ef9790b6b6bccb01784cad6d50525910 644B
._0f72170df615680e24c1f8ad5650945da3121f 644B
._10 642B
._112734bd3c028e3ec91e33128d234610dd996e 644B
._16 642B
._18 642B
._1c 642B
._24 642B
._27 642B
._282f5c55bb9fa6e574542cf4313ae08007ab00 644B
._29 642B
._2a 642B
._2e 642B
._36 642B
._3c396c5032c4aad293eb6f86a2eb3ded9c2e70 644B
._3ea3f5135fbc96f122f6e4072232cab8496cb0 644B
._43 642B
._443d7b51bd719bb71433b73c73c23b03921ddd 644B
._4781528b94a698ec07ab5353f30fef2c5be543 644B
._505733c5e166a810016055fb957ad47b84535e 644B
._5718ec1c0e821358deba919dfb4c8403f8851c 644B
._5975f0ca043801f8c02ec87e599b7b4f7e7241 644B
._5ed4ce4f33df990c724cd2ca32f74a4c76e1c3 644B
._5ee54173f10df0d70d2a394d69c257ef6a511c 644B
._63 642B
._67 642B
._68 642B
._6b 642B
._70ef7320fc37d2e857a5d9d1e7f6691554b0be 644B
._79633d2141b14e35f329b1fef086907be3887e 644B
._8e916709989ecb929186a39f02c20aeb8ccdbf 644B
._914cf0ba93009408cceb1e3a81c17d221d555f 644B
._9293e87be816ac348e6fb5e079ee59483fdac4 644B
._93 642B
._988a29859f3229c68b442476caaa610cbbbe27 644B
._9d 642B
._9de29bb2d1d6434b8b29ae775ad8c2e48c5391 644B
.___pycache__ 642B
._a5 642B
._a625f6a21c611712ba0a51fbad54d2c485b78f 644B
._ab 642B
._b1ddd10922138b8c17607fd155246d9215a8d4 644B
._b7 642B
._ba 642B
._bbd7f12c20d23c7a96c37f7a0ed71ff4a52cab 644B
._branches 642B
._c3a197361c5e4a0f22bb217eee7016ff039cb5 644B
._ca 642B
._cc 642B
._COMMIT_EDITMSG 644B
._config 644B
._css 642B
._d17298f5871ccade5a0482cc13d6452637c434 644B
._d9 642B
._de 642B
._description 644B
._e6 642B
._e9 642B
._ebb71eb22c1dcdc267cade3d2511db68b73638 644B
._ed93ad314901cad409a754f0b4bac3efeb86d1 644B
._ee1b60ca96717c6493ffd9efdb314b2dc407cb 644B
._effc4792efbec6e0d2ead68ec405106125c33e 644B
._exclude 644B
._f68976115ecb6c774adaa10ff6c0ee8013d725 644B
._fb 642B
._fb9a45d6524ff7fc9c72c8ec574004093d8166 644B
._fd 642B
._FETCH_HEAD 644B
._ff3ebbade53c66eccd69a10f41f6c4d5aebcd0 644B
._HEAD 644B
._HEAD 644B
._heads 642B
._heads 642B
._hooks 642B
._img 642B
._index 644B
共 224 条
- 1
- 2
- 3
资源评论
0and1调研
- 粉丝: 15
- 资源: 1203
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 使用ASP.NET Core和Entity Framework Core来构建一个基本的进销存系统.rar
- 深度学习经典数据集+FER2013面部表情识别+附带使用方法的python代码
- Python中,要实现连接多个相机并识别多个二维码.rar
- 使用FFT算法对一个信号进行分析.rar
- 171cms游戏应用下载系统源码.zip
- 基于jsp+servlet+mysql蛋糕甜品店购物网站源码+数据库(期末大作业).zip
- Java项目:在线蛋糕商城系统(java+jsp+mysql)源码+数据库+期末大作业.zip
- ZapyaClient10_7-1.apk
- 织梦cms站长导航网站源码.zip
- 基于SSM+MySQL的网络投票调查问卷系统源码+数据库(java期末大作业).zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功