windows 作为 django 开发环境下踩坑


之前一直都是在 linux 服务器下做开发的,但是总要远程进去开始觉得有点麻烦,就想着干脆直接把自己的 windows 作为开发环境好了,所以就把原来跑在 linux 下的 django 程序放在 Windows 下,下面记录一下过程中踩的坑


建立 Windows 虚拟环境

参考:link

首先确保自己的 python 为 py3,如果不是,自己去编译下载

  • 建立虚拟环境
python -m venv tm_venv
  • 开启环境
tm_venv\Scripts\activate
  • 关闭环境
deactivate

然后在虚拟环境下安装 requirement.txt 中所有需要的模块包


使用不同的 setting.py 配置文件

python manage.py runserver --settings=tracking_management.debug_setting

无法识别静态文件路径

成功启动之后在 localhost 访问发现所有的静态文件都无法加载出来,在启动终端不报错,但在控制台所有静态文件的加载都显示 404 not found, 于是去自己的 debug_setting.py 中设置

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

STATIC_URL 的路径包含进 STATICFILES_DIRS

参考:link


django manage.py 下 uwsgi ImportError

已解决,参考本人另一篇博文 link

这里我是直接

try:
    import uwsgi
except ImportError:
    pass

psycopg2.OperationalError: cursor "_django_curs_19960_36" does not exist

在自己的 windows 开发环境下运行 python manage.py test --settings=tracking_management.debug_setting 发生了下面的 error

(tm_venv) E:\code\SVN\Hugin\TrackingManagement\branches\db_refactor\sourcecode\tracking_management\tracking_management>python manage.py test --settings=tracking_management.debug_setting
Creating test database for alias 'default'...
Got an error creating the test database: database "test_hugin_refactor" already exists

Type 'yes' if you would like to try deleting the test database 'test_hugin_refactor', or 'no' to cancel: yes
Destroying old test database for alias 'default'...
Traceback (most recent call last):
  File "E:\my_virtualvenv\tm_venv\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: relation "CurrentTest" does not exist
LINE 1: ...entTestRecordID", "CurrentTest"."CreateTime" FROM "CurrentTe...
                                                             ^


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

Traceback (most recent call last):
  File "E:\my_virtualvenv\tm_venv\lib\site-packages\django\db\models\sql\compiler.py", line 1065, in execute_sql
    cursor.execute(sql, params)
  File "E:\my_virtualvenv\tm_venv\lib\site-packages\django\db\backends\utils.py", line 68, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "E:\my_virtualvenv\tm_venv\lib\site-packages\django\db\backends\utils.py", line 77, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "E:\my_virtualvenv\tm_venv\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "E:\my_virtualvenv\tm_venv\lib\site-packages\django\db\utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "E:\my_virtualvenv\tm_venv\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "CurrentTest" does not exist
LINE 1: ...entTestRecordID", "CurrentTest"."CreateTime" FROM "CurrentTe...
                                                             ^


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "E:\my_virtualvenv\tm_venv\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "E:\my_virtualvenv\tm_venv\lib\site-packages\django\core\management\__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "E:\my_virtualvenv\tm_venv\lib\site-packages\django\core\management\commands\test.py", line 26, in run_from_argv
    super().run_from_argv(argv)
  File "E:\my_virtualvenv\tm_venv\lib\site-packages\django\core\management\base.py", line 316, in run_from_argv
    self.execute(*args, **cmd_options)
  File "E:\my_virtualvenv\tm_venv\lib\site-packages\django\core\management\base.py", line 353, in execute
    output = self.handle(*args, **options)
  File "E:\my_virtualvenv\tm_venv\lib\site-packages\django\core\management\commands\test.py", line 56, in handle
    failures = test_runner.run_tests(test_labels)
  File "E:\my_virtualvenv\tm_venv\lib\site-packages\django\test\runner.py", line 604, in run_tests
    old_config = self.setup_databases()
  File "E:\my_virtualvenv\tm_venv\lib\site-packages\django\test\runner.py", line 551, in setup_databases
    self.parallel, **kwargs
  File "E:\my_virtualvenv\tm_venv\lib\site-packages\django\test\utils.py", line 174, in setup_databases
    serialize=connection.settings_dict.get('TEST', {}).get('SERIALIZE', True),
  File "E:\my_virtualvenv\tm_venv\lib\site-packages\django\db\backends\base\creation.py", line 76, in create_test_db
    self.connection._test_serialized_contents = self.serialize_db_to_string()
  File "E:\my_virtualvenv\tm_venv\lib\site-packages\django\db\backends\base\creation.py", line 119, in serialize_db_to_string
    serializers.serialize("json", get_objects(), indent=None, stream=out)
  File "E:\my_virtualvenv\tm_venv\lib\site-packages\django\core\serializers\__init__.py", line 128, in serialize
    s.serialize(queryset, **options)
  File "E:\my_virtualvenv\tm_venv\lib\site-packages\django\core\serializers\base.py", line 87, in serialize
    for count, obj in enumerate(queryset, start=1):
  File "E:\my_virtualvenv\tm_venv\lib\site-packages\django\db\backends\base\creation.py", line 116, in get_objects
    yield from queryset.iterator()
  File "E:\my_virtualvenv\tm_venv\lib\site-packages\django\db\models\query.py", line 332, in _iterator
    yield from self._iterable_class(self, chunked_fetch=use_chunked_fetch, chunk_size=chunk_size)
  File "E:\my_virtualvenv\tm_venv\lib\site-packages\django\db\models\query.py", line 54, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
  File "E:\my_virtualvenv\tm_venv\lib\site-packages\django\db\models\sql\compiler.py", line 1068, in execute_sql
    cursor.close()
psycopg2.OperationalError: cursor "_django_curs_19960_36" does not exist

一看就是 DB 跟现在的 model 不同步的问题。

但是看了 model 跟 setting.py 的设置,都是没有问题的,后来找了很久都没找到问题就试着下 makemigrations 看看,结果报出了迁移文件

(tm_venv) E:\code\SVN\Hugin\TrackingManagement\branches\db_refactor\sourcecode\tracking_management\tracking_management>python manage.py makemigrations --settings=tracking_management.debug_setting
Migrations for 'database':
  E:\code\SVN\Hugin\WebService\branches\db_refactor\sourcecode\hugin_webservice\hugin_webservice\database\migrations\0034_currenttest_currenttestcache_currenttestitemrecord_currenttestrecord.py
    - Create model CurrentTest
    - Create model CurrentTestCache
    - Create model CurrentTestItemRecord
    - Create model CurrentTestRecord

看了一下 model 没问题,那就是迁移文件没有同步。
是因为之前都是在 linux 的环境下做的迁移,用另外一台 linux 做了迁移,migration 文件生成在了那台 linux 服务器下,没有同步到现在的 windows,所以把 migration 文件同步过来再下 manage.py test 就可以正常做 unittest 了

posted @ 2020-06-18 14:47  Janey91  阅读(520)  评论(0编辑  收藏  举报