django-crispy-forms is a Django application

django-crispy-forms is a Django application that lets you easily build, customize and reuse forms using your favorite CSS framework, without writing template code and without having to take care of annoying details. You are currently looking at the documentation of the development release.

http://django-crispy-forms.readthedocs.io/en/latest/install.html#installing-django-crispy-forms

python框架Django管理后台Xadmin设置

 python 下面的框架比较多,感兴趣的可以了解下,今天我给大家带来,python django 下用Xadmin来管理django的强大后台!

python安装方法,请移步!

  1. 如何将django模块注册到xadmin后台
  2. 如何重新定义xadmin 样式css,font
  3. 如果将新的user模块注册到已有的user中
  4. Dependency on app with no migrations解决办法
  5. Related model cannot be resolved 的解决办法
  6. static的设置方法

今天给大家带来django下xadmin的安装方法!

第一:首先命令安装django

#pip命令
pip install django
#pip3.6命令
pip3.6 install django

第二:命令安装xadmin

pip install xadmin
#pip3.6 命令
pip3.6 install xadmin

第三:setting.py中设置 INSTALLED_APPS

INSTALLED_APPS = (
    ...
    'xadmin',
    'crispy_forms',
    'reversion',
    ...
)

第四:setting.py中设置中文

# LANGUAGE_CODE = 'en-us'
LANGUAGE_CODE = 'zh-Hans'
# TIME_ZONE = 'UTC'
TIME_ZONE = 'CCT'
USE_I18N = True
USE_L10N = True
USE_TZ = True

第五:urls.py中设置 

import xadmin
xadmin.autodiscover()
from xadmin.plugins import xversion
xversion.register_models()
urlpatterns = [
    path(r'',xadmin.site.urls),
   # path('admin/',admin.site.urls),
]

 第五:数据库同步,命令行下输入

#数据库同步命令
pyhton manage.py migrate
#then
python manage.py makemigrations
#end
python manage.py migrate

第六:运行命令打开测试端口

打开端口测试命令:

python manage.py runserver
#or  修改端口
pyhton manage.py runserver 8888
#or IP加端口
python manage.py runserver 0.0.0.0:8888

如果提示port被占用可以用如下命令杀死:

sudo fuser -k 8000/tcp

MAC 下打开 http://0.0.0.0:8888  其他环境 http://127.0.0.1:8888

打开首页如下为样式:

 

如何将django模块注册到xadmin后台      回到顶部

 第一:将app注册到xadmin 

  1. 在models.py中创建新的表单 EmailVerifyRecord
    class EmailVerifyRecord(models.Model):
        email_choices = (
            ('register', u'注册'),
            ('forget', u'找回密码'),
        )
        code = models.CharField(max_length=20, verbose_name=u'验证码')
        email = models.EmailField(max_length=50, verbose_name=u'邮箱')
        send_type = models.CharField(choices=email_choices, max_length=10, verbose_name=u'验证码类型')
        send_time = models.DateTimeField(default=datetime.now, verbose_name=u'发送时间')
  2. app中新建adminx.py文件
    import xadmin
    from .models import EmailVerifyRecord
     
    class EmailVerifyRecordAdmin(object):
        list_display = ['code', 'email', 'send_type', 'send_time']
        search_fields = ['code', 'email', 'send_type']
        list_filter = ['code', 'email', 'send_type', 'send_time']
     
    xadmin.site.register(EmailVerifyRecord, EmailVerifyRecordAdmin)​
  3. 有十几种主题应用可供选择

第二:设置admix.py,在前面新建的adminx.py中添加如下代码 

from xadmin import views
 
class BaseSetting(object):
    enable_themes = True
    use_bootswatch = True
 
xadmin.site.register(views.BaseAdminView, BaseSetting)

第三:全局设置重定义后台的logo文本和底部的@

在adminx.py中添加

from xadmin import views
 
class GlobalSetting(object):
    site_title = "润格后台管理系统"
    site_footer = "http://www.runge.net/"
 
xadmin.site.register(views.CommAdminView, GlobalSetting)

第四:app左侧的列表紧缩 间隔

在上面的 adminx.py 中的site_footer 下面添加

menu_style = "accordion"

即可!

apps.py

一:增加如下代码:

from django.apps import AppConfig
 
 
class UsersConfig(AppConfig):
    name = 'user'
    verbose_name = '用户管理'

二:__init__.py文件添加代码:

default_app_config = "user.apps.UsersConfig"

第五:设置表名操作为中文

打开models.py

在要表的字段下面添加

class Mata:
    db_table='user_info' #表名
    verbose_name ='用户信息列表'
    verbose_name_plural='用户信息表管理'

完整的models.py代码:

from django.db import models
from _datetime import datetime

# Create your models here.
class UserInfo(models.Model):
    username = models.CharField(max_length=10)
    password = models.CharField(max_length=20)
    true_name = models.CharField(max_length=10)
    phone = models.IntegerField()
    create_time =models.DateTimeField()
    class Mata:
        db_table ='user_info'
        verbose_name ='用户信息'
        verbose_name_plural ='用户信息管理'
#注册表
class EmailVerifyRecord(models.Model):
    email_choices = (
        ('register', u'注册'),
        ('forget', u'找回密码'),
    )
    code = models.CharField(max_length=20, verbose_name=u'验证码')
    email = models.EmailField(max_length=50,unique=True, verbose_name=u'邮箱')
    send_type = models.CharField(choices=email_choices, max_length=10, verbose_name=u'验证码类型')
    send_time = models.DateTimeField(default=datetime.now, verbose_name=u'发送时间')
    class Meta:
        verbose_name ='用户邮箱表'
        verbose_name_plural ='用户邮箱管理'

如何引入自定义的ico 案例

第一:font awesome自定义

官网下载:http://fontawesome.io/

下载后覆盖css、fonts目录

在adminx.py文件中引入model_icon

class EmailVerifyRecordAdmin(object):
    list_display = ['code', 'email', 'send_type', 'send_time']
    search_fields = ['code', 'email', 'send_type']
    list_filter = ['code', 'email', 'send_type', 'send_time']
    model_icon = 'fa fa-address-book-o'
xadmin.site.register(EmailVerifyRecord, EmailVerifyRecordAdmin)

model_icon这个值,在http://fontawesome.io/icons/ 中选中的图标或class内容,如下图

第二:引用自定义的用户信息

这里应用的是源码安装 xadmin

下载:https://github.com/sshwsfc/xadmin

解压xadmin-master.zip文件

将xadmin-master文件中xadmin目录复制到自己的django项目下

在如上图的路径中添加代码

from django.contrib.auth import get_user_model
User = get_user_model()

如何把新建的user应用添加到已有的user中

为了便于管理和拓展我们将新建的user和原来的对接方法如下:

第一:在models.py中引入模型类

from django.db inport models
from django.contrib.auth.models import User  ##导入admin User

加入下面的命令:

class UserProfile(AbstractUser):
    nick_name = models.CharField(max_length=50, verbose_name=u"昵称", default="")
    birthday = models.DateField(verbose_name="生日", null=True, blank=True)
    gender = models.CharField(max_length=6, choices=(("male", "男"), ("female", "女")), default='female')
    address = models.CharField(max_length=100, default="")
    mobile = models.CharField(max_length=11, null=True, blank=True)
    image = models.ImageField(upload_to="image/%Y/%m", default="image/default.png", max_length=100)

    class Mata:
        verbose_name = "用户信息"
        verbose_name_plural =verbose_name
        def __unicode__(self):
            return self.username

第二:在setting.py中重载

AUTH_USER_MODEL = "users.UserProfile"

Dependency on app with no migrations 的解决办法

#运行命令
pyhton3.6 manage.py makemigrations
#即可

Related model cannot be resolved 的解决办法

删除app 目录下的migrations 目录下的文件 解决

static 设置

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)