ing

Angel's Blog.

软件开发,程序设计,前端设计。

给micolog增加日志密码功能(口令:caiming)

很奇怪,部署到空间发现session完全不起作用啊,不能存储值。。。不过本地部署的时候是没有问题的。。。搞不清楚为什么。。

这段代码实现方式本身是通过浏览器访问使用get方法,如果当前entry有属性password(entry.password):如果django获取到的参数“entry_password”不为空,则将当前获取到的参数“entry_password”存入到session中;如果当前的session中有“entry_password”这个键,但是不相等,则返回个not equal的表单;如果session中没有“entry_password”这个键,则返回个session为空的表单;否则pass就ok了。。。

代码如下:

修改blog.py脚本:

找到entry = entries[0],然后加入以下代码(缩进):

 

		entry_password = self.param("entry_password")
		sess = Session(self, timeout = 1800)#session invalid at half-hours
		entry_form = ""
		if entry.password:
			if entry_password != "":
				sess['entry_password'] = entry_password
				sess.save()
				if sess['entry_password'] != entry.password:
					entry_form = "<form name='password_not_equal'><input type='password' name='entry_password' class='entry_password' /><br /><input type='submit' class='formsubmit' /></form>"
			elif entry_password == "":
				#self.write("not param")
				if 'entry_password' in sess.keys():
					if sess['entry_password'] != entry.password:
						entry_form = "<form name='password_not_equal'><input type='password' name='entry_password' class='entry_password' /><br /><input type='submit' class='formsubmit' /></form>"
				if 'entry_password' not in sess.keys():
					entry_form = "<form name='session_empty'><input type='password' name='entry_password' class='entry_password' /><br /><input type='submit' class='formsubmit' /></form>"
			else:
				pass

 

接着找到“if entry.entrytype == 'post':”

 

		if entry.entrytype == 'post':
			self.render('single',
						{
'entry_form': entry_form,#只添加这行代码
'entry':entry,
'relateposts':entry.relateposts,
'comments':comments,
#省略...})

 

在模板下的single.html添加如下控制变量:

 

{% if entry_form %}

	{{entry_form}}

{% else %}

	<!-- 正常没有加密的内容 -->
	{{entry.content}}

{% endif %}

如果你使用的是django 1.2的话,建议在输出的{{entry_form}}和{{entry.content}}修改成:{{entry_form|safe}}和{{entry.content|safe}}

哦。。差点忘记了,还需要修改“view/admin/entry.html”模板:

找到“entry.password”:

将包裹他的div标签“<div style="display:none">”的style属性删除:

 

<div>
<div class="clear"></div>
<div class="stuffbox" id="tag">
<h3>{% trans "Password" %}</h3>
<div class="inside">
	<input type="text" id="password" value="{{entry.password}}" tabindex="1" size="30" name="password"/><br/>
</div>
</div>
</div>

 

本人对django只懂得一点,也是简单的加密日志实现,时间仓促就写这些吧。

补充:

session其实是有效的,找到了问题的原因:因为我使用的是“CheckImg”,对应的get方法有判断session是否为旧的session,如果为旧的则销毁掉旧的session然后再创建新的session。。。。这个步骤也影响到之前存到session中的key -- value存储。。。。(还有,如果留言成功,也会将提交留言前的session销毁掉);现在只能想到的是可以使用cookie,不过对于django的cookie实在不会用。。。大家如果想使用其中的session效果,建议更换验证码的显示为“计算”。

或者通过RSS关注本站的更新 ^ O ^ 。” — by Angel. @ 2011-10-02 - 09:02。 转载请注明:来自 Angel's Blog.
本文永久链接: http://www.q86.net/2011/10/2/given-micolog-add-diary-password.html
Angel.  Tag(s):  micologpython编程session,   
  • QQ书签
  • Del.icio.us
  • 百度收藏
  • Google
  • 饭否
  • FriendFeed

Comments Trackbacks
9 Comments Contributed by Visitors
查看更多评论,请点击这里
Leave Comments Here...

:) lol 8) :-o evil :( wink :-| :-x twisted 8-O :-P oops idea :-D cry :-? ?

(required)

(required)

随您("http://"可以省略)

(Ctrl+Enter)

我显示验证码...

  • 本站不会泄露您的 Email 地址, 敬请放心.
  • 本站已开启缓存, 请不要重复提交评论.
  • 请注意: 带有 《广告(链)、不健康、或者 产品名字》 的评论一律视为spam.
  • 支持常用 HTML 标签调用, 插入 HTML 标签需注意: < == &lt;> == &gt;.
  • 仅当某人针对您的评论做出回应时, 您才会收到由本站发出的邮件通知.
  • 如您希望以相同方式通知某位网友, 请点击 按钮进行回复.
  • 允许在评论中使用如下代码: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> .
  • 鼠标悬浮到评论中的 @someone 区域即可自动显示 someone 的评论内容.