国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

Django使用Profile擴(kuò)展User模塊方式

瀏覽:166日期:2024-10-07 15:57:02

首先創(chuàng)建Profile應(yīng)用

python manage.py startapp profiles

profiles/models.py

# -*- coding: utf-8 -*-from django.db import modelsfrom django.contrib.auth.models import Userfrom django.db.models.signals import post_saveclass UserProfile(models.Model): user = models.OneToOneField(User) nickname = models.CharField(max_length=16, default=’’, blank=True) sex = models.IntegerField(default=0) phone = models.CharField(max_length=16, default=’’, blank=True) def __str__(self): return self.nickname def __unicode__(self): return self.nicknamedef create_user_profile(sender, instance, created, **kwargs): if created: profile = UserProfile() profile.user = instance profile.save()post_save.connect(create_user_profile, sender=User)

profiles/admin.py

# -*- coding: utf-8 -*-from django.contrib import adminfrom django.contrib.auth.models import Userfrom django.contrib.auth.admin import UserAdminfrom .models import UserProfileclass ProfileInline(admin.StackedInline): model = UserProfile max_num = 1 can_delete = Falseclass UserProfileAdmin(UserAdmin): inlines = [ProfileInline, ]admin.site.unregister(User)admin.site.register(User, UserProfileAdmin)

settings.py

添加

AUTH_PROFILE_MODULE = ’profiles.UserProfile’

測試

$ python manage.py syncdb$ python manage.py shell>>> from django.contrib.auth.models import User>>> user = User()>>> user.username=’testuser’>>> user.save()>>> User.objects.all()[0].userprofile

補(bǔ)充知識:django中登錄到accounts/profile/的解決方案

Django使用Profile擴(kuò)展User模塊方式

在project的setting里加一句話就Okay!

LOGIN_REDIRECT_URL = ’/index’

以上這篇Django使用Profile擴(kuò)展User模塊方式就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Django
相關(guān)文章:
主站蜘蛛池模板: 宁河县| 腾冲县| 原阳县| 泊头市| 洛宁县| 嵊州市| 古田县| 长武县| 安吉县| 简阳市| 葫芦岛市| 墨脱县| 象山县| 郎溪县| 湘潭县| 临湘市| 开远市| 门源| 苍溪县| 平果县| 仁寿县| 乐昌市| 安宁市| 新源县| 封丘县| 澄迈县| 通河县| 梧州市| 北辰区| 东乌| 宜丰县| 清丰县| 峨眉山市| 滨海县| 宜春市| 大宁县| 绥阳县| 嵊泗县| 满城县| 盐源县| 广宗县|