django搭建个人博客08,添加访问权限
来源:互联网 发布:godaddy创建数据库 编辑:程序博客网 时间:2024/05/18 11:48
为publish页面添加访问权限
1.修改www/urls.py
a.在www应用的urls.py里添加django提供的一套验证框架
Authentication Views
vim www/urls.pyfrom django.conf.urls import includeurl('^',include('django.contrib.auth.urls')),
b.在www/templates/registration下编写这套框架需要的html模板文件
html模板
e.g login.html<!-- login.html -->{% extends "www/www_base.html" %}{% block content %}{% if form.errors %}<p>Your username and password didn't match. Please try again.</p>{% endif %}{% if next %} {% if user.is_authenticated %} <p>Your account doesn't have access to this page. To proceed, please login with an account that has access.</p> {% else %} <p>Please login to see this page.</p> {% endif %}{% endif %}<form method="post" action="{% url 'www:login' %}">{% csrf_token %} <table> <tr> <td>{{ form.username.label_tag }}</td> <td>{{ form.username }}</td> </tr> <tr> <td>{{ form.password.label_tag }}</td> <td>{{ form.password }}</td> </tr> </table> <input type="submit" value="login" /> <input type="hidden" name="next" value="{{ next }}" /></form>{# Assumes you setup the password_reset view in your URLconf #}<p><a href="{% url 'www:password_reset' %}">Lost password?</a></p>{% endblock %}<!-- end login.html -->
2.修改article/publish的view,使其需要staff权限
a.仿照login_required的源码,写一个staff_required
login_required
User
vim www/auths.pyfrom functools import wrapsfrom django.conf import settingsfrom django.contrib.auth import REDIRECT_FIELD_NAMEfrom django.core.exceptions import PermissionDeniedfrom django.shortcuts import resolve_urlfrom django.utils.decorators import available_attrsfrom django.utils.six.moves.urllib.parse import urlparsefrom django.contrib.auth.decorators import user_passes_testdef staff_required(function=None,redirect_field_name=REDIRECT_FIELD_NAME,login_url=None): actual_decorator=user_passes_test( lambda u:u.is_staff, login_url=login_url, redirect_field_name=redirect_field_name ) if function: return actual_decorator(function) return actual_decorator
b.参照示例让ArticlePublishView继承Staff_test
Mixins that wrap as_view()
vim www/auths.pyclass Staff_test(object): @classmethod def as_view(cls,**initkwargs): view=super(Staff_test,cls).as_view(**initkwargs) return staff_required(view,login_url='www:login') vim www/views.pyfrom .auths import *class ArticlePublishView(Staff_test,FormView): ....
登录*/blogs/article/publish查看
0 0
- django搭建个人博客08,添加访问权限
- Django搭建个人博客
- Django框架搭建个人博客网站实例
- django 个人博客系统开发 - 项目搭建
- 使用 django-blog-zinnia 搭建个人博客
- django搭建个人博客03,编写首页
- django搭建个人博客04,文章列表
- django搭建个人博客07,编辑文章
- django搭建个人博客遇到的常见问题
- 在 Github Pages 搭建的静态个人博客中添加 https 访问
- github搭建个人博客----------绑定域名访问
- 用django在SAE搭建个人博客(一)
- 用django在SAE搭建个人博客(二)
- 运用Django和MySQL搭建本地个人博客主页
- VirtualBox虚拟机ubuntu系统安装及django搭建个人博客
- python之django-blog-zinnia 搭建个人博客
- django搭建个人博客01,mysql连接配置
- django搭建个人博客02,表设计(models)
- SpringAPI手动创建代理对象——ProxyFactory
- 十四、排列2
- 数据结构之顺序查找
- HTML5 canvas之旋转
- (计算几何)判断一个点是否在线段上
- django搭建个人博客08,添加访问权限
- 【bzoj2002】【Hnoi2010】【Bounce 弹飞绵羊】【lct】
- 排序 选择问题 Partition函数
- Spring学习笔记一
- 【C++】 LeetCode 98. Validate Binary Search Tree
- 尺取法+POJ3320
- blender球体挖洞
- 基于WSN的智能节水灌溉系统设计方案
- 【项目记录】山东大学场馆管理系统之——需求分析、接口文档