Python 如何运行克隆的 Django 项目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37094032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:50:48  来源:igfitidea点击:

How to run cloned Django project?

pythondjangogitgithubdjango-admin

提问by allardbrain

I am a junior software engineer and am quite new to Django. I built this appand am working on a README to explain to others how to fork, clone and setup the app on their own machines. I've gotten stuck while trying to re-create the steps.

我是一名初级软件工程师,对 Django 还是很陌生。我构建了这个应用程序,正在编写一个自述文件,向其他人解释如何在他们自己的机器上分叉、克隆和设置应用程序。我在尝试重新创建这些步骤时遇到了困难。

This is the order in which I've drawn up the steps:

这是我制定步骤的顺序:

  1. Fork and clone the repo
  2. Source a virtual environment
  3. Pip install requirements.txt
  4. Obtain access_token and secret_key and store in secrets.sh
  5. Setup a Postgres DB, create user & database
  6. Migrate (?) - This is where I get stuck!
  1. 分叉并克隆 repo
  2. 获取虚拟环境
  3. pip安装需求.txt
  4. 获取 access_token 和 secret_key 并存储在 secrets.sh 中
  5. 设置 Postgres 数据库,创建用户和数据库
  6. 迁移 (?) - 这就是我卡住的地方!

I tried migrating the app but there are no migrations to apply.

我尝试迁移应用程序,但没有要应用的迁移。

I tried django-admin startproject ig_miner_app. but am getting this error code:

我试过了django-admin startproject ig_miner_app。但我收到此错误代码:

CommandError: /Users/Erin/Desktop/CodeByEAllard/project/instagram_miner/manage.py already exists, overlaying a project or app into an existing directory won't replace conflicting files

CommandError: /Users/Erin/Desktop/CodeByEAllard/project/instagram_miner/manage.py already exists, overlaying a project or app into an existing directory won't replace conflicting files

If I can get this sorted out, I should just be able to run the server like normal, right?

如果我能解决这个问题,我应该可以像往常一样运行服务器,对吗?

I'm sure I'm missing something (or many things) but don't know what they are. I feel silly because I was obviously able to create the app in the first place, but can't figure out how to explain to someone else to do the same! Does any have suggestions for how to get the server to run?

我确定我错过了一些东西(或很多东西),但不知道它们是什么。我觉得很傻,因为我显然能够首先创建应用程序,但无法弄清楚如何向其他人解释做同样的事情!有没有关于如何让服务器运行的建议?

Thank you!

谢谢!

回答by Bernard 'Beta Berlin' Parah

First off, you are getting that error because you are starting a project within the same directory as the cloned project, this directory already contains an app with the name ig_miner_apphence the name conflict.

首先,您会收到该错误,因为您正在与克隆项目相同的目录中启动一个项目,该目录已经包含同名的应用程序,ig_miner_app因此名称冲突。

As regards steps to running the project by other users , this should work.

至于其他用户运行项目的步骤,这应该可行。

clone the project

克隆项目

git clone https://github.com/erinallard/instagram_miner.git 

create and start a a virtual environment

创建并启动一个虚拟环境

virtualenv env --no-site-packages

source env/bin/activate

Install the project dependencies:

安装项目依赖:

pip install -r requirements.txt

create a file named "secrets.sh"

创建一个名为“secrets.sh”的文件

touch secrets.sh(mac and linux)

touch secrets.sh(mac 和 linux)

obtain a secret from MiniWebToolkey and add to secrets.sh

MiniWebToolkey 中获取一个 secret并添加到secrets.sh

export SECRET_KEY='<secret_key>'

add secrets.sh to .gitignore file

将 secrets.sh 添加到 .gitignore 文件

create a postgres db and add the credentials to settings.py

创建一个 postgres 数据库并将凭据添加到 settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'db_name',
        'USER': 'name',
        'PASSWORD': '',
        'HOST': 'localhost',
        'PORT': '',
    }
}

then run

然后运行

python manage.py migrate

create admin account

创建管理员帐户

python manage.py createsuperuser

then

然后

python manage.py makemigrations ig_miner_app

to makemigrations for the app

为应用程序进行迁移

then again run

然后再次运行

python manage.py migrate

to start the development server

启动开发服务器

python manage.py runserver

and open localhost:8000 on your browser to view the app.

并在浏览器上打开 localhost:8000 以查看该应用程序。

I believe this should get the app up and running on others' machines. Let me know if you get stuck on any of these steps so I make edits, if not, you can just use it and add any other relevant info I might not have added.

我相信这应该能让应用程序在其他人的机器上启动并运行。如果您遇到这些步骤中的任何一个,请告诉我,以便我进行编辑,如果没有,您可以使用它并添加我可能未添加的任何其他相关信息。

回答by Abraxas

Hey @allardbrain and welcome to the Wonderful World of Development ^_^

嘿@allardbrain,欢迎来到精彩的发展世界^_^

While my hope is that this issue has been resolved by now, if I may, let's revisit something real quick-- I'm sure others have made and,are currently making this mistake.

虽然我希望这个问题现在已经得到解决,但如果可以的话,让我们快速回顾一下 - 我相信其他人已经犯过并且目前正在犯这个错误。

I tried 'django-admin startproject ig_miner_app . but am getting this error code:

我试过 'django-admin startproject ig_miner_app 。但我收到此错误代码:

"CommandError: " yadda yadda yadda ...

“命令错误:” yadda yadda yadda ...

Django actually has AMAZING Documentation. The guys behind it were actually Writers and Journalists and not your typical CS guys.

Django 实际上有惊人的文档。它背后的人实际上是作家和记者,而不是典型的 CS 人。

What I'm trying to say is, when learning something new, read the Documentation; run through the To-Do App Tutorial. Here's why..

我想说的是,在学习新东西时,阅读文档;运行待办事项应用程序教程。这就是为什么..

django-admin startproject

This has already been satisfied if you are pulling a working copy of a pre-existing Application. Your concern should be with this file first and foremost ...

如果您正在提取预先存在的应用程序的工作副本,这已经得到满足。您应该首先关注这个文件......

requirements.txt

This is where the devDependencies state their demands,if you will , similar to your basic

这是 devDependencies 陈述他们的要求的地方,如果你愿意的话,类似于你的基本要求

package.json 

Anyway, I'm only saying this because I spent the first few years of my career stubborn and often catching myself skimming through docs and, at the end of the day creating headaches for myself and the poor bastards tasked with overseeing my bumbling arse :-) Great times,those ...

无论如何,我之所以这么说是因为我在职业生涯的最初几年很固执,经常让自己浏览文档,最终让自己和负责监督我笨拙屁股的可怜的混蛋头疼:- ) 美好的时光,那些……

Cheers Everyone

干杯大家

回答by Nidhi Garg

I take the following steps after cloning the project from GitHub:

从 GitHub 克隆项目后,我采取以下步骤:

pip3 install virtualenv
virtualenv -p python3 env
source env/bin/activate
pip install django
django-admin startproject <mysite>
python manage.py startapp polls 
python manage.py runserver
on terminal: python manage.py makemigrations <app_name>
python manage.py migrate

These work fine on my system.

这些在我的系统上运行良好。

回答by TechBrad

My IDE seems to auto-activate a venv if it resides inside the project directory which caused me to continually run into the error when I cloned from git.

如果 venv 驻留在项目目录中,我的 IDE 似乎会自动激活它,这导致我在从 git 克隆时不断遇到错误。

ModuleNotFoundError: No module named ''

ModuleNotFoundError: No module named ''

I had to manually delete the original venv and create a new virtual env then ran these commands. Worked like a PyCharm after :)

我必须手动删除原始 venv 并创建一个新的虚拟 env,然后运行这些命令。之后像 PyCharm 一样工作:)

pip install -r requirements.txt

pip install -r requirements.txt

python manage.py collectstatic

python manage.py collectstatic

回答by Debanjan Barman

  1. Create virtualenv and activate it.
  2. Install all dependencies.
  3. cd into the project's home directory.
  4. run $ python manage.py runserver.
  1. 创建 virtualenv 并激活它。
  2. 安装所有依赖项。
  3. cd 进入项目的主目录。
  4. 运行 $ python manage.py runserver。

回答by ahmed

try to pass the app name to the migratecommand:

尝试将应用程序名称传递给migrate命令:

manage.py migrate ig_miner_app

回答by sprksh

As you said, once you clone the repo, you have to install the requirements

正如你所说,一旦你克隆了 repo,你就必须安装需求

pip install -r requirements.txt

After that you have to configure the database. Add the details to settings.py

之后,您必须配置数据库。将详细信息添加到 settings.py

Once it has been done and you have the keys and secrets, you have to make migrations and then migrate.

一旦完成并且您拥有密钥和秘密,您必须进行迁移然后迁移。

cd into the app directory and run

cd 进入应用程序目录并运行

python manage.py makemigrations

to create migration files for the models already defined in the codes you have cloned. after that you have to run

为已在您克隆的代码中定义的模型创建迁移文件。之后你必须运行

python manage.py migrate

to apply the migrations which in effect creates the database tables.

应用实际上创建数据库表的迁移。

Now you make any changes in models or add extra models or fields, you have to run the last two commands again.

现在您对模型进行任何更改或添加额外的模型或字段,您必须再次运行最后两个命令。