Ctrl+c终止进程等快捷键:https://blog.csdn.net/qq_37884273/article/details/89110840
一、model归类:goods、trade、users、user_operation
激活环境+python manage.py runserver 启动服务+
一app-model
二、django 模型models 常用字段:https://blog.csdn.net/weixin_37773766/article/details/80330221
三、verbose_name理解:https://www.cnblogs.com/yum777/articles/8992640.html
四、连接MySQL表
1、python manage.py makemigrations#生成文件
2、python manage.py migrate#建表(修改表最好用代码实现)
遇到报错:models.ForeignKey()中缺少了on_delete参数,on_delete之前默认为on_delete=models.CASCADE,详解参考:https://blog.csdn.net/m0_38109046/article/details/82660038
五、Xadmin管理后台
1、xadmin安装以及依赖:https://github.com/sshwsfc/xadmin/blob/master/requirements.txt
2、项目setting中添加 ‘crispy_forms’, ‘xadmin’,
3、python manage.py makemigrations 建立数据库迁移文件
4、在url.py中配置xadmin的访问路径
5、创建超级用户:python manage.py createsuperuser
六、Django版本跟新遇到的问题:
1、six直接import;
2、django.core.urlresolvers包跟新后需要全部换成django.urls;
3、python_2_unicode_compatible存在于包six而不是django.utils.encoding
4、from django.contrib.staticfiles.templatetags.staticfiles import static替换成from django.templatetags.static import static
6、forms.Field.init(self, required, widget, label, initial, help_text, *args, **kwargs) 改为forms.Field.init(self)
7、from django.contrib.auth.views import login 替换为
from django.contrib.auth import login
8、from django.db.models.fields import FieldDoesNotExist改为from django.core.exceptions import FieldDoesNotExist
9、from django.db.models.sql.query import LOOKUP_SEP, QUERY_TERMS修改为:
from django.db.models.sql.query import LOOKUP_SEP, Query
10、from django.db.models import FieldDoesNotExist替换为from django.core.exceptions import FieldDoesNotExist
11、password_reset_confirm替换为PasswordResetForm
12、‘MIDDLEWARE_CLASSES’改为了’MIDDLEWARE’
13、path()变成path(r’’, xadmin.site.urls)
14、‘CharField’ object has no attribute ‘rel’:rel改为remote_field
15、list.py中的FieldDoesNotExist问题:from django.core.exceptions import FieldDoesNotExist
16、添加系统日志时报异常: ‘ManyToOneRel’ object has no attribute ‘to’
修改 xadmin\plugins\quickform.py 中80行
self.add_url, (_(‘Create New %s’) % self.rel.to.meta.verbose_name), name,
修改为self.add_url, ((‘Create New %s’) % self.rel), name,
所有Unable to lookup‘ ’问题都是因为自己敲错字母了,看英文解释即可解决!
最后改过models后一定要重新makemigrations!!!!
七、后台报错进去因为website.py不配套了:
1、TypeError: login() got an unexpected keyword argument ‘extra_context’
#login和logout改为如下,不改会报错'function' object has no attribute 'as_view'
from django.contrib.auth.views import LoginView as login
from django.contrib.auth.views import LogoutView as logout
#注释掉以下两行代码
# 'current_app': self.admin_site.name,
# 'current_app': self.admin_site.name,
#替换两个return
return logout.as_view(**defaults)(request)
return login.as_view(**defaults)(request)
之后会报错:‘Media’ object has no attribute ‘add_css’
#解决方案:
#将util.py 中的86行 def vendor(*tags):方法体改为:
css = {'screen': []}
js = []
for tag in tags:
file_type = tag.split('.')[-1]
files = xstatic(tag)
if file_type == 'js':
js.extend(files)
elif file_type == 'css':
css['screen'] += files
return Media(css=css, js=js)
八、中英文修改setting:
#设置时区
LANGUAGE_CODE = 'zh-hans' #中文支持,django1.8以后支持;1.8以前是zh-cn
TIME_ZONE = 'Asia/Shanghai'
USE_I18N = True
USE_L10N = True
USE_TZ = False #默认是Ture,时间是utc时间,由于我们要用本地时间,所用手动修改为false!!!!
// apps.py里可以单独修改如下:
class GoodsConfig(AppConfig):
name = 'goods'
verbose_name = "中文名"
IndexError at /goods/goods/52/update/
list index out of range报错解决办法:
input_html = [ht for ht in super(AdminSplitDateTime, self).render(name, value, attrs).split(’\n’) if ht != ‘’]
改为
input_html = [ht for ht in super(AdminSplitDateTime, self).render(name, value, attrs).replace(’><input’, ‘>< input’).split(’/’) if ht != ‘’]
九、数据导入
db_tool中以数据列表导入代码后补
十、未解决问题:专业版中setting-language里没有Django导致tools里没有manage.py