django杂篇

1密码加密

 对象.make_password(password)

例如

user_profile = UserProfile()

user_profile.password = make_password(pass_word)


2,判断密码与数据库相同

对象.check_password(password)

例如

  

user=UserProfile.objects.get(Q(username=username)|Q(email=username))

user.check_password(password)

3登录方法

   user = authenticate(username=user_name, password=pass_word) 

#对象,判断与数据库数据相同返回true,否则返回NONE
login(request, user)

4取外键的所有数据

  如下,course_set的course是外键

  

from courses.models import Course

course_org=CourseOrg.objects.get(id=int(org_id))
all_courses=course_org.course_set.all()[:3]
5.{{get_degree_display}}
degree = models.CharField(verbose_name=u"难度", choices=(("cj","初级"), ("zj","中级"), ("gj","高级")), max_length=2)
    如果html页面显示的话应该是{{对象.degree}},但输出结果是'cj,zj'是字母,想让他变成汉字即
'初级.中级'  应用 对象.{{get_degree_display}}



猜你喜欢

转载自blog.csdn.net/marraybug/article/details/79368883