如何在另一个服务器上的另一个虚拟环境中运行Django文件?

huangapple go评论58阅读模式
英文:

How to run django files in another virtualenv environment on another server?

问题

我正在将一个使用Django创建的网站迁移到另一台服务器上的过程中。我已经将所有文件打包成一个zip包,然后在第二台服务器上创建了一个新的virtualenv环境,并将数据导出为一个.json文件。我已经将所有文件都传输到目标服务器上,但不幸的是,我不知道接下来该怎么做。

在新的virtualenv环境中如何运行Django?我应该将文件提取到新的virtualenv环境的文件夹中吗?如果是这样,是否需要首先激活virtualenv?

我还要补充说明迁移是从两个不同的系统进行的,即从Windows迁移到Ubuntu。

英文:

I am in the process of moving a site created in django to another server. I have packed all files into a zip package, created a new virtualenv environment on a second server, and dumpdate into a .json file. I already have all the files on the destination server, but unfortunately I don't know what to do next.

How to run django in the new virtualenv environment? Should I extract the files to a folder with the new virtualenv? If so, do I have to activate virtualenv first?

I will also add that migration takes place from two different systems. From windows on ubuntu.

答案1

得分: 4

  1. 使用命令 pip freeze > requirements.txt 将您的环境包保存到文件中。然后在新服务器上创建虚拟环境,并使用命令 pip install -r requirements.txt 安装所有包。

  2. 复制您的代码库到任何您喜欢的地方。

  3. 如果不是SQLite数据库,请创建您的数据库(空数据库)。进入项目文件夹,运行 python manage.py migrate 来创建数据库表。

  4. 运行 python manage.py loaddata dbdump.data 来迁移所有数据库中的数据。dbdump.data 是在旧系统上使用 python manage.py dumpdata 创建的文件。注意:如果先前的项目包含数据迁移,loaddata 将失败,因为数据库已经包含数据。在这种情况下,您需要使用原始 SQL 手动删除数据库中的所有数据。

  5. 如果在先前的系统上上传了文件,请将这些文件复制到 MEDIA_ROOT 文件夹中(如果需要,创建它)。在 settings.py 中查找 MEDIA_ROOT。

  6. 运行 python manage.py collectstatic

现在运行 manage.py runserver 并检查一切是否正常运行:curl -v http://127.0.0.1:8000 将显示您项目的响应。

英文:
  1. Save your env packages to a file with a command pip freeze > requirements.txt.
    Afterwards on your new server create virtualenv and install all packages with command pip install -r requirements.txt.

Do not copy your virtualenv manually, packages might work differently on another system.

activate your virtualenv and run python -m django —version to check that Django is installed correctly.

  1. Copy your code repository anywhere you like.

  2. Then create your database (empty database) if it’s not SQLite. cd into your project folder. Run python manage.py migrate to create the tables of your database.

  3. Run python manage.py loaddata dbdump.data to migrate all the data of your db. dbdump.data is the file created on the old system with python manage.py dumpdata. Note: if the previous project contained data migrations, loaddata will fail because the db will already contain data. In that case you’ll need to manually delete all data in the database first using raw sql.

  4. If you had uploaded files on your previous system, copy these files into the MEDIA_ROOT folder (create it if necessary). Check for MEDIA_ROOT in settings.py

  5. Run python manage.py collectstatic

Now run manage.py runserver and check everything works: curl -v http://127.0.0.1:8000 will show you your project's response.

huangapple
  • 本文由 发表于 2020年1月7日 01:23:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/59616382.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定