Django1.11 extended user attribute to increase avatar upload function

Source: Internet
Author: User
Tags auth

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> 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.