Django开发笔记六

Posted 懒企鹅

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Django开发笔记六相关的知识,希望对你有一定的参考价值。

1、登录功能完善

登录成功应该是重定向到首页,而不是转发,当前用户信息保存到session:

class LoginView(View):
    def get(self, request):
        return render(request, "login.html", {})

    def post(self, request):
        user_name = request.POST.get("username", "")
        pass_word = request.POST.get("password", "")
        user = authenticate(username=user_name, password=pass_word)

        if user is not None:
            dj_login(request, user)
            request.session[‘currentUser‘] = json.dumps(user, default=UserProfile.user2json)
            return HttpResponseRedirect("/")
        else:
            return render(request, "login.html", {‘msg‘: None})

 用户需要提供序列化方法:

class UserProfile(AbstractUser):
    nickname = models.CharField(max_length=50, verbose_name=u"昵称")
    birthday = models.DateField(max_length=50, null=True, blank=True, verbose_name=u"生日")
    gender = models.CharField(choices=(("male", u"男"), ("female", u"女"), ("secret", u"保密")), default=u"secret",
                              max_length=10)
    address = models.CharField(max_length=100, default=u"")
    mobile = models.CharField(max_length=11, null=True, blank=True)
    image = models.ImageField(upload_to="user/%y/%m", default=u"images/default/user_default.png",
                              max_length=100)
    add_time = models.DateTimeField(default=datetime.now)

    class Meta:
        verbose_name = u"用户"
        verbose_name_plural = verbose_name
        ordering = ["-id"]

    def __unicode__(self):
        return self.username

    def user2json(self):
        return {‘username‘: self.username, ‘nickname‘: self.nickname, ‘id‘: self.id, ‘image‘: self.image.url}

 

以上是关于Django开发笔记六的主要内容,如果未能解决你的问题,请参考以下文章

CLR via C#深解笔记六 - 泛型

django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE的解决办法(转)(代码片段

django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE的解决办法(转)(代码片段

JavaWeb学习笔记六 JSP

第六模块:WEB框架开发 第1章·Django框架开发

Django项目笔记——聊天功能的实现