学习Django过程踩的坑
适用于Django2版本的Xadmin下载地址
其他配置:基础配置
由于版本问题,加入xadmin后各种报错,基本上每个文件都修改过!超级生气!!!
下面说明几处错误修改办法:
版本描述:
Django 3.1.1
django-crispy-forms 1.9.2
django-formtools 2.2
django-import-export 2.3.0
django-reversion 3.0.8
future 0.18.2
Pillow 7.2.0
PyMySQL 0.10.1
six 1.15.0
添加环境变量
# 在setting中添加
sys.path.insert(0, os.path.join(BASE_DIR, 'extra_apps'))
1
2
中INSTALLED_APPS中添加:
'xadmin',
'crispy_forms',
1
2
在urls中添加
import xadmin
path('xadmin/', xadmin.site.urls),
1
2
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module. Did you install mysqlclient?:
import pymysql
pymysql.install_as_MySQLdb()
1
2
TypeError: init() missing 1 required positional argument: ‘on_delete’:
# 添加
把content_type = models.ForeignKey(ContentType)修改为content_type = models.ForeignKey(ContentType,on_delete=models.CASCADE)
1
2
django.core.exceptions.ImproperlyConfigured: Specifying a namespace in include() without providing an app_name is not supported. Set the app_name attribute in the included module, or pass a 2-tuple containing the list of patterns and app_name instead:
# namespace的语法:
# path(‘url地址’,include((‘app的url文件’,‘命名’),namespace=‘命名’))
path('user/', include('user.urls', 'urls'), namespace='user)
1
2
3
ImportError: cannot import name ‘FieldDoesNotExist’ from ‘django.db.models.fields’:
from django.core.exceptions import FieldDoesNotExist
1
TypeError: init() takes 1 positional argument but 6 were given:
把forms.Field.__init__(self, required, widget, label, initial, help_text, *args, **kwargs) 修改成forms.Field.__init__(self)
1
AttributeError: ‘Settings’ object has no attribute ‘MIDDLEWARE_CLASSES’:
把:
if settings.LANGUAGES and 'django.middleware.locale.LocaleMiddleware' in settings.MIDDLEWARE_ClASSES:
改成:
if settings.LANGUAGES and 'django.middleware.locale.LocaleMiddleware' in settings.MIDDLEWARE:
1
2
3
4
ImportError: cannot import name ‘QUERY_TERMS’:
把from django.db.models.sql.query import LOOKUP_SEP, QUERY_TERMS修改为:
from django.db.models.sql.query import LOOKUP_SEP, Query
1
2
AttributeError: ‘CharField’ object has no attribute ‘rel’:
if isinstance(field.rel, models.ManyToOneRel):
related_fields.append(field_name)
修改为
if isinstance(field.remote_field, models.ManyToOneRel):
related_fields.append(field_name)
1
2
3
4
5
IndexError: list index out of range:
把 input_html = [ht for ht in super(AdminSplitDateTime, self).render(name, value, attrs).replace(
'/><input', '/>\n<input').split('\n') if ht != '']
替换为
input_html = [ht for ht in super(AdminSplitDateTime, self).render(name, value, attrs).replace(
'><input', '>\n<input').split('\n') if ht != '']
1
2
3
4
5
‘ManyToOneRel’ object has no attribute ‘to’ :
self.add_url, (_('Create New %s') % self.rel.to._meta.verbose_name), name,修改为self.add_url, (_('Create New %s') % self.rel), name,
1
AttributeError: ‘ForeignKey’ object has no attribute ‘re’:
#data_list =filter_field_obj.rel.to.objects.all()这个不行换成以下代码
data_list = filter_field_obj.remote_field.model.objects.all()
1
2
3
参考博客1
参考博客2
问题太多,不一一列举!
————————————————
版权声明:本文为CSDN博主「If I could Tell Yang」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_44177600/article/details/108740718