The Django user model does not have both avatar and phone attributes, so you need to extend the user to the desired effect, and according to the official documentation, the extension of the new field to user only requires a one-to-one (one-to-one) model.
First, create an app
Python manage.py Startapp Customuser
Second, create the user model inside the models, and use the one-to-one to relate user
From django.db import Models
# Create your models here.
From django.contrib.auth.models import User
class MyUser (models. Model):
user = models. Onetoonefield (User)
phone = models. Charfield (U ' mobile number ', max_length=13, Null=true, blank=true)
avatar = models. ImageField (U ' Administrator Avatar ', upload_to= ' images ', null=true, blank=true)
class Meta:
verbose_name = u ' Avatar Management '
Third, add admin configuration, Admin can fill in the background two new fields:
From django.contrib import Admin to
django.contrib.auth.admin import useradmin as Baseuseradmin
from Django.contrib.auth.models import User from
customuser.models import myuser
# Define A inline admin descriptor f or Employee model
# which acts a bit like a singleton
class Employeeinline (admin. Stackedinline):
model = myuser
can_delete = False
verbose_name_plural = U ' Admin center '
# Define a new User admin
class Useradmin (baseuseradmin):
inlines = (Employeeinline,)
# re-register Useradmin
Admin.site.unregister (user)
admin.site.register (user, useradmin)
Admin Background effect:
Four, modify the settings user point to:
Auth_user_module = ' Customuser.myuser '
V. Build the database:
Python manage.py makemigrations Customuser
Python manage.py Migrations
Six, upload avatar needs media configuration, so the settings need to increase the configuration:
Media_url = '/media/'
media_root = Os.path.join (base_dir, ' media ')
Create a new media directory on the same layer in manage.py
Seven, configure urls.py:
From django.conf Import settings
From django.conf.urls.static import static
Urlpatterns + = static (settings. Media_url, Document_root=settings. Media_root)
Eight, template on the use of head:
<ul class= "nav navbar-nav navbar-right" > <li class= "dropdown navbar-user" > <a href= "Javascrip" T:; "class=" Dropdown-toggle "data-toggle=" dropdown "> <span class= "Hidden-xs" >{{request.user.username}}</span> <b class= "caret" ></b>
</a> <ul class= "dropdown-menu animated Fadeinleft" > <li class= "Arrow" ></li> <li><a href= "javascript:;" > Editing Materials </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>