Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
hackrole committed Jan 31, 2013
1 parent 1749aef commit 9a78478
Show file tree
Hide file tree
Showing 10 changed files with 191 additions and 11 deletions.
97 changes: 97 additions & 0 deletions blog/#blog.py#
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()

12 changes: 8 additions & 4 deletions blog/blog.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
import web
import model
from web.contrib.template import render_jinja
import pprint


urls = (
'/', 'Index',
'/view/(\d+)', 'View',
'/new', 'New', 'New',
'/delete/(\d+)', 'Delete',
'edit/(\d+)', 'Edit',
'/edit/(\d+)', 'Edit',
)

t_globals = {
Expand All @@ -34,13 +36,14 @@ def GET(self):
return render.index(posts=posts)


class view:
class View:

def GET(self, id):
"""
show single post
"""
post = model.get_post(int(id))
pprint.pprint(post)
return render.view(post=post)

class New:
Expand Down Expand Up @@ -77,9 +80,10 @@ def GET(self, id):
post = model.get_post(int(id))
form = New.form()
form.fill(post)
return render.edit(post, form)
pprint.pprint(form)
return render.edit(post=post, form=form)

def Post(self, id):
def POST(self, id):
"""
post posts edit
"""
Expand Down
Binary file modified blog/blog.pyc
Binary file not shown.
Binary file modified blog/sql/main.db
Binary file not shown.
20 changes: 20 additions & 0 deletions blog/templates/edit.html
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 %}



11 changes: 5 additions & 6 deletions blog/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
{% endblock %}

{% block body %}
{{ posts|pprint() }}
<ul id="menu">
<li><a href="/">Home</a> </li>
{% if posts %}
<a href=""> {{ posts.title }}</a>
<p>{{ posts.content }}</p>
{% else %}
<p>the post is now empty</p>
{% endif %}
{% for post in posts %}
<a href=""> {{ post.title }}</a>
<p>{{ post.content }}</p>
{% endfor %}
</ul>
{% endblock %}

Expand Down
14 changes: 14 additions & 0 deletions blog/templates/view.html
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 %}



14 changes: 13 additions & 1 deletion jinja2_template_try.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

from jinja2 import *
import sys,traceback
import time

def fileSystem_try(f, path, **kargs):

def fileSystem_try(file, path, **kargs):
env = Environment(loader=FileSystemLoader(path)))
try:
template = env.get_template(file)
Expand All @@ -16,4 +18,14 @@ def fileSystem_try(file, path, **kargs):
print "tempalte %s under %s is error" % (file, path)
print traceback.print_stack()

if __name__ == "__main__":
path = raw_input('please entry the template dir path:')
f = raw_input('please enter the file name:')
b = raw_input('if write the result to file:')
while b not in ['Y', 'N', 'y', 'n']:
b = raw_input('you must entry y or n:')

result = fileSystem_try(f, path)
if b in ['Y', 'y']:
tmp = '/tmp/%s' % (time.now()+f)
open(tmp, 'w').write(result)
17 changes: 17 additions & 0 deletions templates/base.html
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>

17 changes: 17 additions & 0 deletions templates/try.html
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>


0 comments on commit 9a78478

Please sign in to comment.