把xadmin包放到项目目录下,便于修改xadmin中的代码。
首先解压下载好的 xadmin-django2.zip 压缩包,拷贝子文件夹中的xadmin文件夹,到项目中新建extra_apps文件夹,放入xadmin。
虚拟环境中卸载 xadmin安装包:
pip uninstall xadmin
把extra_apps目录mark为sources root
到settings中设置extra_apps文件夹的跟路径:
sys.path.insert(0, os.path.join(BASE_DIR, 'extra_apps'))
这样访问xadmin时自动从配置文件中查找相应的路径。
如果对添加的用户进行修改时,会弹出错误:
IndexError at /xadmin/users/userprofile/2/update/list index out of rangeRequest Method: GETRequest URL: http://localhost:8000/xadmin/users/userprofile/2/update/Django Version: 2.0.5Exception Type: IndexErrorException Value: list index out of rangeException Location: D:\python\MXonline\extra_apps\xadmin\widgets.py in render, line 80Python Executable: C:\Users\Administrator\Envs\oneenv\Scripts\python.exePython Version: 3.6.5Python Path: ['D:\\python\\MXonline\\extra_apps', 'D:\\python\\MXonline\\apps', 'D:\\python\\MXonline', 'C:\\Program Files\\JetBrains\\PyCharm 2018.1.2\\helpers\\pydev', 'D:\\python\\MXonline', 'D:\\python\\MXonline\\apps', 'D:\\python\\MXonline\\extra_apps', 'C:\\Program Files\\JetBrains\\PyCharm 2018.1.2\\helpers\\pydev', 'C:\\Users\\Administrator\\.PyCharm2018.1\\system\\cythonExtensions', 'C:\\Users\\Administrator\\Envs\\oneenv\\Scripts\\python36.zip', 'C:\\Users\\Administrator\\Envs\\oneenv\\DLLs', 'C:\\Users\\Administrator\\Envs\\oneenv\\lib', 'C:\\Users\\Administrator\\Envs\\oneenv\\Scripts', 'c:\\users\\administrator\\appdata\\local\\programs\\python\\python36\\Lib', 'c:\\users\\administrator\\appdata\\local\\programs\\python\\python36\\DLLs', 'C:\\Users\\Administrator\\Envs\\oneenv', 'C:\\Users\\Administrator\\Envs\\oneenv\\lib\\site-packages', 'C:\\Program Files\\JetBrains\\PyCharm '
pycharm中的错误信息是:
File "C:\Users\Administrator\Envs\oneenv\lib\site-packages\django\template\base.py", line 910, in render_annotated return self.render(context) File "C:\Users\Administrator\Envs\oneenv\lib\site-packages\django\template\defaulttags.py", line 314, in render return nodelist.render(context) File "C:\Users\Administrator\Envs\oneenv\lib\site-packages\django\template\base.py", line 943, in render bit = node.render_annotated(context) File "C:\Users\Administrator\Envs\oneenv\lib\site-packages\django\template\base.py", line 910, in render_annotated return self.render(context) File "C:\Users\Administrator\Envs\oneenv\lib\site-packages\django\template\defaulttags.py", line 314, in render return nodelist.render(context) File "C:\Users\Administrator\Envs\oneenv\lib\site-packages\django\template\base.py", line 946, in render bits.append(str(bit)) File "C:\Users\Administrator\Envs\oneenv\lib\site-packages\django\utils\html.py", line 380, inklass.__str__ = lambda self: mark_safe(klass_str(self)) File "C:\Users\Administrator\Envs\oneenv\lib\site-packages\django\forms\boundfield.py", line 36, in __str__ return self.as_widget() File "C:\Users\Administrator\Envs\oneenv\lib\site-packages\django\forms\boundfield.py", line 118, in as_widget **kwargs File "D:\python\MXonline\extra_apps\xadmin\widgets.py", line 80, in render '%s ' % (input_html[0], _(u'Today'), input_html[1], _(u'Now')))IndexError: list index out of range[03/May/2018 15:11:36] "GET /xadmin/users/userprofile/2/update/ HTTP/1.1" 500 630373
具体解决办法是:https://blog.csdn.net/yuhan963/article/details/79167743
就是在xadmin中的 widgets.py文件的第 75行,
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).split('/><') if ht != '']input_html[0] = input_html[0] + "/>"input_html[1] = "<" + input_html[1]
ctrl + alt +f 格式化代码后,行号变了
再看浏览器,ok: