文章目录
- 第10章 个人中心和全局搜索功能实现
- 全局搜索
- 修改头像
- 第11章 首页、全局功能细节和404以及500页面配置
- 第12章 常见web攻击及防范
- sql注入
- 12-2 xss攻击原理及防范
- 12-3 csrf攻击与防范
- 第13章 xadmin的进阶开发
- 13-1 userprofile注册以及django的权限管理
- 13-3 model_icon, 只读字段,默认排序设置,ajax加载
- 13-8 excel导入插件介绍
代码
第10章 个人中心和全局搜索功能实现
全局搜索
感觉这里可以修改下
//顶部搜索栏搜索方法
function search_click(){var type = $('#jsSelectOption').attr('data-value'),keywords = $('#search_keywords').val(),request_url = '';if(keywords == ""){return}if(type == "course"){request_url = "/course/list?keywords="+keywords}else if(type == "teacher"){request_url = "/org/teacher/list?keywords="+keywords}else if(type == "org"){request_url = "/org/list?keywords="+keywords}window.location.href = request_url
}
修改头像
class UploadImageForm(forms.ModelForm):'''用户更改图像'''class Meta:model = UserProfilefields = ['image']
class UploadImageView(LoginRequiredMixin,View):'''用户图像修改'''def post(self,request):#上传的文件都在request.FILES里面获取,所以这里要多传一个这个参数image_form = UploadImageForm(request.POST,request.FILES)if image_form.is_valid():# 调用参数image = image_form.cleaned_data['image']request.user.image = imagerequest.user.save()return HttpResponse('{"status":"success"}', content_type='application/json')else:return HttpResponse('{"status":"fail"}', content_type='application/json')
第11章 首页、全局功能细节和404以及500页面配置
# 全局404页面配置
handler404 = 'users.views.pag_not_found'
# 全局500页面配置
handler500 = 'users.views.page_error'
from django.shortcuts import render_to_response
def pag_not_found(request):# 全局404处理函数response = render_to_response('404.html', {})response.status_code = 404return responsedef page_error(request):# 全局500处理函数from django.shortcuts import render_to_responseresponse = render_to_response('500.html', {})response.status_code = 500return response
第12章 常见web攻击及防范
sql注入
Django自带的orm带有防止sql注入
#错误--不要直接格式化字符串
query = 'SELECT * FROM myapp_person WHERE last_name = %s' % lname
Person.objects.raw(query)#正确--使用Django raw函数 功能进行安全转义
name = 'Doe'
Person.objects.raw('SELECT * FROM myapp_person WHERE last_name = %s', [name])
请注意在cursor.execute() 的SQL语句中使用“%s”,而不要在SQL内直接添加参数。 如果你使用这项技术,数据库基础库将会自动添加引号,同时在必要的情况下转意你的参数。
12-2 xss攻击原理及防范
12-3 csrf攻击与防范
防护:{csrf_token}表单验证
第13章 xadmin的进阶开发
13-1 userprofile注册以及django的权限管理
因为xadmin使用的是自带的static, 所以会找不到路径
urls.py
# 静态文件# re_path(r'^static/(?P<path>.*)', serve, {"document_root": STATIC_ROOT }),
settings.py
STATIC_ROOT = os.path.join(BASE_DIR, '/static/')
13-3 model_icon, 只读字段,默认排序设置,ajax加载
apps/course/adminx.py
import xadmin
from .models import Course, Lesson, Video, CourseResource,BannerCourse
from organization.models import CourseOrgclass LessonInline(object):model = Lessonextra = 0class CourseResourceInline(object):model = CourseResourceextra = 0# Course的admin管理器
class CourseAdmin(object):'''课程'''list_display = [ 'name','desc','detail','degree','learn_times','students','get_zj_nums','go_to'] #显示的字段search_fields = ['name', 'desc', 'detail', 'degree', 'students'] #搜索list_filter = [ 'name','desc','detail','degree','learn_times','students'] #过滤model_icon = 'fa fa-book' #图标ordering = ['-click_nums'] #排序readonly_fields = ['click_nums'] #只读字段exclude = ['fav_nums'] #不显示的字段# list_editable = ['degree','desc']# refresh_times = [3,5] #自动刷新(里面是秒数范围)inlines = [LessonInline,CourseResourceInline] #增加章节和课程资源style_fields = {"detail": "ueditor"}def queryset(self):# 重载queryset方法,来过滤出我们想要的数据的qs = super(CourseAdmin, self).queryset()# 只显示is_banner=True的课程qs = qs.filter(is_banner=False)return qsdef save_models(self):# 在保存课程的时候统计课程机构的课程数# obj实际是一个course对象obj = self.new_obj# 如果这里不保存,新增课程,统计的课程数会少一个obj.save()# 确定课程的课程机构存在。if obj.course_org is not None:#找到添加的课程的课程机构course_org = obj.course_org#课程机构的课程数量等于添加课程后的数量course_org.course_nums = Course.objects.filter(course_org=course_org).count()course_org.save()# 将管理器与model进行注册关联
xadmin.site.register(Course, CourseAdmin)
- 将外键名称通过ajax加载,节省资源