1. 程式人生 > >超越村後端開發(2:新建models.py+xadmin的引入)

超越村後端開發(2:新建models.py+xadmin的引入)

正文 one ssa reat sco 數據 分享 ESS bsp

1.新建Model

1.users數據

1.在apps/users/models.py中:

技術分享圖片
from datetime import datetime
from django.db import models
from django.contrib.auth.models import AbstractUser

# Create your models here.


class UserProfile(AbstractUser):
    """
    管理員
    """
    name=models.CharField(max_length=30,null=True,blank=True,verbose_name=
姓名) mobile=models.CharField(max_length=11,null=True,blank=True,verbose_name=電話) class Meta: verbose_name=管理員 verbose_name_plural = verbose_name def __str__(self): return self.username class WxUsers(models.Model): """ 微信用戶 """ open_id
=models.CharField(max_length=64,null=True,blank=True,verbose_name=OpenId) token=models.CharField(max_length=64,null=True,blank=True,verbose_name=token) nickName=models.CharField(max_length=30,null=True,blank=True,verbose_name=微信名) avatarUrl=models.CharField(max_length=200,null=True,blank=True,verbose_name=
用戶頭像URL) gender = models.CharField(max_length=4, choices=((0, 未知),(1, ), (2, )), default=0, verbose_name=性別) province=models.CharField(max_length=20,null=True,blank=True,verbose_name=用戶所在省) city=models.CharField(max_length=20,null=True,blank=True,verbose_name=用戶所在市) country=models.CharField(max_length=20,null=True,blank=True,verbose_name=用戶所在旗縣區) longitude=models.CharField(max_length=10,null=True,blank=True,verbose_name=經度) latitude=models.CharField(max_length=10,null=True,blank=True,verbose_name=緯度) last_login_time = models.DateTimeField(default=datetime.now, verbose_name="最後登錄時間") add_time = models.DateTimeField(default=datetime.now, verbose_name="添加時間") class Meta: verbose_name=微信用戶 verbose_name_plural=verbose_name def __str__(self): return self.nickName
View Code

2.在settings.py中增加配置項:

AUTH_USER_MODEL=users.UserProfile

技術分享圖片

3.執行數據更新命令:

python manage.py makemigrations
python manage.py migrate

2.wish數據

1.apps/wish/models.py中:

技術分享圖片
from django.db import models
from users.models import WxUsers,UserProfile
from datetime import datetime
from django.utils.safestring import mark_safe
# Create your models here.


class Wish(models.Model):
    """
    願望表
    """
    wxuser=models.ForeignKey(WxUsers,on_delete=models.CASCADE)
    content=models.CharField(max_length=200,default=‘‘,verbose_name=願望內容,help_text=最多200字)
    status=models.CharField(max_length=8, choices=((0, 進行中),(1, 放棄了), (2, 超時了)), default=0,
                              verbose_name=階段)
    pv_num=models.IntegerField(default=0,verbose_name=瀏覽數)
    up_num=models.IntegerField(default=0,verbose_name=點贊數)
    share_num=models.IntegerField(default=0,verbose_name=轉發數)
    bb_num=models.IntegerField(default=0,verbose_name=評論數)
    end_time = models.DateTimeField(default=datetime.now, verbose_name="添加時間")
    add_time = models.DateTimeField(default=datetime.now, verbose_name="添加時間")

    class Meta:
        verbose_name=願望表
        verbose_name_plural=verbose_name

    def __str__(self):
        return self.content


class News(models.Model):
    """
    新聞表
    """
    user=models.ForeignKey(UserProfile,on_delete=models.CASCADE)
    banner=models.ImageField(max_length=200, upload_to=banner/,verbose_name=輪播圖)
    title=models.CharField(max_length=15,default=‘‘,verbose_name=新聞標題,help_text=最多15字)
    content=models.CharField(max_length=800,default=‘‘,verbose_name=新聞正文,help_text=最多800字)
    add_time = models.DateTimeField(default=datetime.now, verbose_name="添加時間")

    class Meta:
        verbose_name=新聞表
        verbose_name_plural=verbose_name

    def __str__(self):
        return self.title

    def image_url(self):
        return mark_safe(<img src="/media/{0}" class="field_img">.format(self.banner))

    image_url.short_description = 輪播圖


class Images(models.Model):
    """
    新聞圖片
    """
    image = models.ImageField(max_length=200, upload_to=images/)
    news=models.ForeignKey(News, verbose_name="新聞", blank=True,null=True,on_delete=models.CASCADE)
    add_time = models.DateTimeField(default=datetime.now, verbose_name=添加時間)

    class Meta:
        verbose_name = "新聞圖片"
        verbose_name_plural = verbose_name

    def image_url(self):
        return mark_safe(<img src="/media/{0}" class="field_img">.format(self.image))

    image_url.short_description = 圖片
View Code

2.安裝 pillow包(做圖片處理的)

pip install pillow

3.執行數據更新命令:

python manage.py makemigrations
python manage.py migrate

3.user_operation數據

1.apps/user_operation/models.py中:

技術分享圖片
from django.db import models
from datetime import datetime
from users.models import WxUsers,UserProfile
from wish.models import Wish
# Create your models here.


class Message(models.Model):
    """
    評論表,消息表
    """
    user_send=models.ForeignKey(WxUsers,on_delete=models.CASCADE,verbose_name=發出方,related_name=send)
    user_receive = models.ForeignKey(WxUsers, on_delete=models.CASCADE, verbose_name=接收方, related_name=receive)
    wish=models.ForeignKey(Wish,on_delete=models.CASCADE,verbose_name=被評論的願望)
    content = models.CharField(max_length=30, default=‘‘, verbose_name=內容, help_text=最多30字)
    is_read=models.BooleanField(default=False,verbose_name=是否已讀)
    add_time = models.DateTimeField(default=datetime.now, verbose_name="添加時間")

    class Meta:
        verbose_name=評論表
        verbose_name_plural=verbose_name

    def __str__(self):
        return self.content


class Pick(models.Model):
    """
    點贊表
    """
    user = models.ForeignKey(WxUsers, on_delete=models.CASCADE, verbose_name=點贊者)
    wish = models.ForeignKey(Wish, on_delete=models.CASCADE, verbose_name=被點贊的願望)
    action=models.BooleanField(default=True,verbose_name=行為,help_text=true是點贊false是取消點贊)

    add_time = models.DateTimeField(default=datetime.now, verbose_name="添加時間")

    class Meta:
        verbose_name=點贊表
        verbose_name_plural=verbose_name

    def __str__(self):
        return self.user


class Share(models.Model):
    """
    分享表
    """
    user = models.ForeignKey(WxUsers, on_delete=models.CASCADE, verbose_name=分享者)
    wish = models.ForeignKey(Wish, on_delete=models.CASCADE, verbose_name=被分享的願望)
    add_time = models.DateTimeField(default=datetime.now, verbose_name="添加時間")

    class Meta:
        verbose_name=分享表
        verbose_name_plural=verbose_name

    def __str__(self):
        return self.user


class Notice(models.Model):
    """
    系統通知表(超越私信)
    """
    admin=models.ForeignKey(UserProfile,on_delete=models.CASCADE)
    content = models.CharField(max_length=200, default=‘‘, verbose_name=內容, help_text=最多200字)
    add_time = models.DateTimeField(default=datetime.now, verbose_name="添加時間")

    class Meta:
        verbose_name=系統通知表
        verbose_name_plural=verbose_name

    def __str__(self):
        return self.content
View Code

2.執行數據更新命令:

python manage.py makemigrations
python manage.py migrate

2.引入xadmin

1.將xadmin文件夾放入extra_apps目錄下:

技術分享圖片

2.在每個app下新建adminx.py

1.apps/users/目錄下新建adminx.py:

技術分享圖片
import xadmin
from xadmin import views
from .models import WxUsers


class BaseSetting(object):
    enable_themes = True
    use_bootswatch = True


class GlobalSettings(object):
    site_title = "超越村後臺管理系統"
    site_footer = "chaoyuecun"
    # menu_style = "accordion"


class WxUsersAdmin(object):
    list_display = ["open_id", "token", "nickName", "gender","province","city","country","last_login_time","add_time"]
    list_filter = [ "gender","province","city","country","last_login_time","add_time"]
    search_fields = ["gender","province","city","country" ]


xadmin.site.register(WxUsers, WxUsersAdmin)
xadmin.site.register(views.BaseAdminView, BaseSetting)
xadmin.site.register(views.CommAdminView, GlobalSettings)
View Code

2.apps/wish/目錄下新建adminx.py:

技術分享圖片
import xadmin
from .models import Wish, News


class WishAdmin(object):
    list_display = [wxuser, content,"status","pv_num","up_num", "share_num","bb_num","end_time","add_time"]
    list_filter =[wxuser, content,"status","pv_num","up_num", "share_num","bb_num","end_time","add_time"]
    search_fields=[wxuser, content,"status","pv_num","up_num", "share_num","bb_num"]


class NewsAdmin(object):
    list_display = [user, banner,"title","content","add_time"]
    list_filter =[user, banner,"title","content","add_time"]
    search_fields=[user, banner,"title","content"]


xadmin.site.register(Wish, WishAdmin)
xadmin.site.register(News, NewsAdmin)
View Code

3.apps/user_operation/目錄下新建adminx.py:

技術分享圖片
import xadmin
from .models import Message,Pick,Share,Notice


class MessageAdmin(object):
    list_display = [user_send, user_receive,"wish","content","is_read","add_time"]
    list_filter =[user_send, user_receive,"wish","content","is_read","add_time"]
    search_fields=[user_send, user_receive,"wish","content","is_read"]


class PickAdmin(object):
    list_display = [user, wish,"action","add_time"]
    list_filter =[user, wish,"action","add_time"]
    search_fields=[user, wish]


class ShareAdmin(object):
    list_display = [user, wish,"add_time"]
    list_filter =[user, wish,"add_time"]
    search_fields=[user, wish]


class NoticeAdmin(object):
    list_display = [admin, content,"add_time"]
    list_filter =[admin, content,"add_time"]
    search_fields=[admin, content]


xadmin.site.register(Message, MessageAdmin)
xadmin.site.register(Pick, PickAdmin)
xadmin.site.register(Share, ShareAdmin)
xadmin.site.register(Notice, NoticeAdmin)
View Code

3.安裝和配置xadmin依賴包

1.安裝依賴包 django-crispy-forms django-reversion django-formtools future httplib2 six django-import-export

pip install django-crispy-forms django-reversion django-formtools future httplib2 six django-import-export

2.在settings中註冊

INSTALLED_APPS = [
    django.contrib.admin,
    django.contrib.auth,
    django.contrib.contenttypes,
    django.contrib.sessions,
    django.contrib.messages,
    django.contrib.staticfiles,
    users.apps.UsersConfig,
    wish.apps.WishConfig,
    user_operation.apps.UserOperationConfig,
    crispy_forms,
    xadmin
]

3.安裝用來操作和導出Excel文件的包 xlwt xlsxwriter

pip install xlwt xlsxwriter

4.打開終端,執行數據更新命令,生成xadmin默認的表

python manage.py makemigrations
python manage.py migrate

5.配置url,在chaoyuecun/urls.py:

技術分享圖片
from django.contrib import admin
from django.urls import path
import xadmin

urlpatterns = [
    # path(‘admin/‘, admin.site.urls),
    path(‘xadmin/‘, xadmin.site.urls),
]

6.創建超級用戶

python manage.py createsuperuser
Username: admin
郵箱:
Password:
Password (again):

創建完畢,啟動項目,然後訪問

http://127.0.0.1:8000/xadmin/

就可以登錄xadmin後臺了,但是此時後臺還是英文的。

7.將語言設定為中文

1.在settings中修改

LANGUAGE_CODE = zh-hans

TIME_ZONE = Asia/Shanghai

USE_I18N = True

USE_L10N = True

USE_TZ = False

技術分享圖片

2.在每個app下的apps.py中做修改,加上verbose_name

1.apps/users/apps.py:

from django.apps import AppConfig


class UsersConfig(AppConfig):
    name = ‘users‘
    verbose_name = ‘用戶‘

技術分享圖片

1.apps/user_operation/apps.py:

from django.apps import AppConfig


class UserOperationConfig(AppConfig):
    name = ‘user_operation‘
    verbose_name = ‘用戶操作‘

1.apps/wish/apps.py:

from django.apps import AppConfig


class WishConfig(AppConfig):
    name = ‘wish‘
    verbose_name = ‘願望‘

這時再看xadmin界面,已經變成中文的了。

技術分享圖片

超越村後端開發(2:新建models.py+xadmin的引入)