「Django」学习之路,持续更改

一、setting设置

1、设置 局域网可以部署连接

ALLOWED_HOSTS = ['*.besttome.com','192.168.1.100']

2、static配置

STATIC_URL = '/static/'

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

3、后台中文配置

LANGUAGE_CODE = 'zh-hans'

TIME_ZONE = 'Asia/Shanghai'

4、数据库配置

详见https://www.cnblogs.com/wrxblog/p/10030741.html

5、session设置

# SESSION_ENGINE = 'django.contrib.sessions.backends.db'  # 引擎(默认)
#
# SESSION_COOKIE_NAME = "sessionid" # Session的cookie保存在浏览器上时的key,即:sessionid=随机字符串(默认)
# SESSION_COOKIE_PATH = "/" # Session的cookie保存的路径(默认)
# SESSION_COOKIE_DOMAIN = None # Session的cookie保存的域名(默认)
# SESSION_COOKIE_SECURE = False # 是否Https传输cookie(默认)
# SESSION_COOKIE_HTTPONLY = True # 是否Session的cookie只支持http传输(默认)
# SESSION_COOKIE_AGE = 1209600 # Session的cookie失效日期(2周)(默认)
# SESSION_EXPIRE_AT_BROWSER_CLOSE = False # 是否关闭浏览器使得Session过期(默认)
# SESSION_SAVE_EVERY_REQUEST = False # 是否每次请求都保存Session,默认修改之后才保存(默认)可设置不操作10秒后失效退出

二、model设置

1、choices 设置及显示

class User(models.Model):
nich = models.CharField(max_length=16,verbose_name='昵称')
yhm = models.CharField(max_length=32,verbose_name='用户名')
mima = models.CharField(verbose_name='密码',max_length=32)
youx = models.EmailField(verbose_name='邮箱')
bm_choice = (
(0,'信息科'),
(1, '农水科'),
(2, '灌溉科'),
(3, '财务科'),
)
bum = models.IntegerField(verbose_name='用户部门',choices=bm_choice,default=0)
shouj = models.IntegerField(verbose_name='手机号码')
toux = models.ImageField(verbose_name='用户头像',upload_to = "static/dist/img/")
shij = models.DateTimeField(verbose_name='注册时间',auto_now=True)

class Meta:
verbose_name = '用户管理'
verbose_name_plural = verbose_name

def __str__(self):
return self.yhm
html设置:
<small>部门:{{ yhxinx.get_bum_display }}</small>

三、admin设置

1、数据库操作添加到后台

from jfapp import models

# Register your models here.
admin.site.register(models.User)

2、默认admin用户名及密码设置方式

pycharm自带的manage.py createsuperuser是不好使的
需用用终端输入命令python3 manage.py createsuperuser

四、数据库数据迁移

dumpdata和loaddata问题

1、建议dumpdata时候加上

dumpdata --exclude=contenttypes --exclude=auth.Permission > initial_data.json
有可能还会有下面的问题

再次导入时,提示:

  File "D:\Program Files\Python\Python35.3\lib\site-packages\django\db\backends\
utils.py", line 65, in execute
    return self.cursor.execute(sql, params)

sqlite3.IntegrityError: UNIQUE constraint failed: auth_user.username

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "D:\Program Files\Python\Python35.3\lib\site-packages\django\core\managem
ent\__init__.py", line 363, in execute_from_command_line
    utility.execute()

  ......

  File "D:\Program Files\Python\Python35.3\lib\site-packages\django\db\backends\
sqlite3\base.py", line 328, in execute
    return Database.Cursor.execute(self, query, params)

django.db.utils.IntegrityError: Problem installing fixture 'E:\workSpace\python\
horoscope_web\initial_data.json': Could not load auth.User(pk=4): UNIQUE constra
int failed: auth_user.username

删除"model": "auth.user"后再次loaddata,提示:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "D:\Program Files\Python\Python35.3\lib\site-packages\django\core\managem
ent\__init__.py", line 363, in execute_from_command_line
    utility.execute()
  
  ......

    bad_row[1], referenced_table_name, referenced_column_name,
django.db.utils.IntegrityError: Problem installing fixtures: The row in table 'd
jango_admin_log' with primary key '3' has an invalid foreign key: django_admin_log.content_type_id contains a value '30' that does not have a corresponding value in django_content_type.id.

django_admin_log表中找不到主键为3的记录。
删除json中"model": "admin.logentry"的数据后,重试后成功导入。

记录例子:

{
    "model": "admin.logentry",
    "pk": 3,
    "fields": {
      "action_time": "2017-06-19T03:05:25.793Z",
      "user": 4,
      "content_type": 30,
      "object_id": "1",
      "object_repr": "Andrew-袁良锋",
      "action_flag": 2,
      "change_message": "[]"
    }
  },
  {
    "model": "admin.logentry",
    "pk": 4,
    "fields": {
      "action_time": "2017-06-19T03:05:43.471Z",
      "user": 4,
      "content_type": 30,
      "object_id": "1",
      "object_repr": "Andrew-袁良锋",
      "action_flag": 2,
      "change_message": "[{\"changed\": {\"fields\": [\"fortunes_keyword\"]}}]"
    }
  }

如果不想手动更改json数据(如删除"model": "admin.logentry"数据),可使用分应用导出/导入:

导出app数据 python manage.py dumpdata {appname}>
导入app数据 python manage.py loaddata {appname}>

这样就只导入了应用数据,避免导入容易产生异常报错而无法正常的公共数据(如 user, auth等)。

2、其他问题

打开导出的JSON文件,有可能会有下面的问题

手动删除掉最上面一行,保存后加载成功







posted @ 2018-11-28 11:08  子谦0618  阅读(297)  评论(0编辑  收藏  举报