Django1.10 Extended User Property adds Avatar upload function

Source: Internet
Author: User

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 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 (User) (User, Useradmin)

Admin Background effect:

650) this.width=650; "Src=" Http:// -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 makemigrations Customuserpython 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

Seven, configuration

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" &GT;{{&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

Django1.10 Extended User Property adds Avatar upload function

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: 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.