The Django-brought user model does not have both the Avatar and the phone properties, so we need to extend the user to achieve the effect we want, and according to the official documentation, you only need to use the one-to-one model to extend the new field to the user.
First, create an app
Python manage.py Startapp Customuser
Second, create user model in models, and use one-to-one to associate users
From __future__ import unicode_literalsfrom django.db import models# Create your models Here.from django.contrib.auth.mod Els import UserClass MyUser (models. Model): User = models. Onetoonefield (User) phone = models. Charfield (max_length=20, Null=true, blank=true) avatar = models. ImageField (upload_to= ' photo ', Null=true, Blank=true)
Third, add admin configuration, admin background can fill in these two new fields:
From Django.contrib import adminfrom django.contrib.auth.admin import useradminfrom django.contrib.auth.models Import Userfrom customuser.models Import myuser# Define An inline admin descriptor for Employee model# which acts a bit like a Si Ngletonclass employeeinline (admin. Stackedinline): Model = MyUser Can_delete = False verbose_name_plural = ' MyUser ' # Define a new User adminclass Us Eradmin (useradmin): Inlines = (employeeinline,) # Re-register UserAdminadmin.site.unregister (User) Admin.site.register (User, Useradmin)
Admin Background effect:
650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M02/85/B0/wKiom1esQOuxz_30AABMQnPFPhw935.png-wh_500x0-wm_3 -wmp_4-s_2328521364.png "title=" 1.png "alt=" Wkiom1esqouxz_30aabmqnpfphw935.png-wh_50 "/>
Four, modify settings user point:
Auth_profile_module = ' Customuser.myuser '
V. Build the database:
Python manage.py makemigrations Customuserpython manage.py migrations
Six, upload avatar needs media configuration, so settings need to add configuration:
Media_url = '/media/' media_root = Os.path.join (base_dir, ' media ')
Create a new media directory on the same layer manage.py
Seven, configuration urls.py:
From django.conf import settingsfrom django.conf.urls.static import Staticurlpatterns + = static (settings. Media_url, Document_root=settings. Media_root)
Eight, the template on the use of Avatar:
<ul class= "Nav navbar-nav navbar-right" > <li class= "dropdown navbar-user "> <a href=" javascript:; " class= "Dropdown-toggle" data-toggle= "dropdown" > <span class= "Hidden-xs" >{{&NBSP;REQUEST.USER.USERNAME&NBSP;}} </span> <b class= "Caret" ></b> </a> <ul class= "Dropdown-menu animated fadeinleft" > <li class= "Arrow" ></li> <li><a href= "javascript:;" > Editorial Information </a></li> <li><a href= "javascript:;" > Reset Password </a></li> <li class= "divider" ></li> <li><a href= "{{ url (' Logout ') }} "> Exit system </a></li> </ul> </li></ul>
This article is from the "Operations Notes" blog, make sure to keep this source http://lihuipeng.blog.51cto.com/3064864/1836996
Django1.10 Extended User Property adds Avatar upload function