-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
191 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
#!/usr/bin/python2.7 | ||
#coding=utf-8 | ||
|
||
import web | ||
import model | ||
from web.contrib.template import render_jinja | ||
|
||
urls = ( | ||
'/', 'Index', | ||
'/view/(\d+)', 'View', | ||
'/new', 'New', 'New', | ||
'/delete/(\d+)', 'Delete', | ||
'edit/(\d+)', 'Edit', | ||
) | ||
|
||
t_globals = { | ||
'datestr': web.datestr | ||
} | ||
|
||
render = render_jinja( | ||
'templates', # 模板路径 | ||
encoding='utf-8', #编码 | ||
) | ||
|
||
class Index: | ||
def GET(self): | ||
""" | ||
show index page | ||
""" | ||
posts = model.get_posts() | ||
for post in posts: | ||
print post['title'] | ||
print post['content'] | ||
return render.index(posts=posts) | ||
|
||
|
||
class view: | ||
|
||
def GET(self, id): | ||
""" | ||
show single post | ||
""" | ||
post = model.get_post(int(id)) | ||
return render.view(post=post) | ||
|
||
class New: | ||
|
||
form = web.form.Form( | ||
web.form.Textbox('title', web.form.notnull, size=30, description="Post content"), | ||
web.form.Textarea('content', web.form.notnull, rows=30, cols=80, description="Post Content"), | ||
web.form.Button('Post entry'), | ||
) | ||
|
||
def GET(self): | ||
form = self.form() | ||
return render.new(form=form) | ||
|
||
def POST(self): | ||
form = self.form() | ||
if not form.validates(): | ||
return render.new(form=form) | ||
model.new_post(form.d.title, form.d.content) | ||
raise web.seeother('/') | ||
|
||
class Delete: | ||
|
||
def POST(self, id): | ||
model.def_post(int(id)) | ||
raise web.seeother('/') | ||
|
||
class Edit: | ||
|
||
def GET(self, id): | ||
""" | ||
edit posts | ||
""" | ||
post = model.get_post(int(id)) | ||
form = New.form() | ||
form.fill(post) | ||
return render.edit(post, form) | ||
|
||
def Post(self, id): | ||
""" | ||
post posts edit | ||
""" | ||
form = New.form() | ||
post = model.get_post(int(id)) | ||
if not form.validates(): | ||
return render.edit(post, form) | ||
model.update_post(int(id), form.d.title, form.d.content) | ||
raise web.seeother('/') | ||
|
||
app = web.application(urls, globals()) | ||
|
||
if __name__ == "__main__": | ||
app.run() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{% extends "base.html" %} | ||
|
||
{% block title %} | ||
block edit page | ||
{% endblock %} | ||
|
||
{% block body %} | ||
<h1>Edit {{ form.d.title }} </h1> | ||
<form method="post" id="" action=""> | ||
{{ form.render() }} | ||
</form> | ||
|
||
<h2>Delete post</h2> | ||
<form method="aciton" action="/delete/{{ post.id }}"> | ||
<input type="submit" name="submit" value="Delete post" /> | ||
</form> | ||
{% endblock %} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{% extends "base.html" %} | ||
|
||
{% block title %} | ||
the blog view page | ||
{% endblock %} | ||
|
||
{% block body %} | ||
<h1>{{ post.title }} </h1> | ||
{{ post.posted_on }} | ||
{{ post.content }} | ||
{% endblock %} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<!DCOTYPE html> | ||
<html lang="zh-CN"> | ||
<head> | ||
<title> | ||
{% block title %} | ||
base html | ||
{% endblock %} | ||
</title> | ||
<meta /> | ||
</head> | ||
<body> | ||
{% block content %} | ||
|
||
{% endblock %} | ||
</body> | ||
</html> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<!DCOTYPE html> | ||
<html lang="zh-CN"> | ||
<head> | ||
<title> | ||
{% block title %} | ||
try jinja2 template | ||
{% endblock %} | ||
</title> | ||
<meta /> | ||
</head> | ||
<body> | ||
{{ }} | ||
|
||
</body> | ||
</html> | ||
|
||
|