Python 如何在 PyCharm 中设置 Django 项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23870365/
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 03:34:35  来源:igfitidea点击:

How to set up a Django project in PyCharm

pythondjangopycharm

提问by user3676356

I'm new in this area so I have a question. Recently, I started working with Python and Django. I installed PyCharm Community edition as my IDE, but I'm unable to create a Django project.

我是这个领域的新手,所以我有一个问题。最近,我开始使用 Python 和 Django。我安装了 PyCharm 社区版作为我的 IDE,但我无法创建 Django 项目。

I looked for some tutorials, and there is an option to select "project type", but in the latest version this option is missing. Can someone tell me how to do this?

我找了一些教程,有一个选项可以选择“项目类型”,但在最新版本中缺少这个选项。有人能告诉我怎么做吗?

回答by Padraic Cunningham

If you look at the features edition comparison matrix, you will see that only PyCharmProfessionalsupports the Django Framework.

如果您查看功能版本比较矩阵,您会看到只有PyCharm Professional支持Django 框架

You can set up a project from the command line using the manage.py script and just open it in PyCharm. Then use the terminal to sync, start server etc...

您可以使用 manage.py 脚本从命令行设置项目,然后在 PyCharm 中打开它。然后使用终端同步,启动服务器等...

You can use Eclipse PyDev with https://pydev.org/manual_adv_django.html. I have not tried it so I cannot say how good or bad it is.

您可以将 Eclipse PyDev 与https://pydev.org/manual_adv_django.html 一起使用。我没有尝试过,所以我不能说它有多好或多坏。

There is a tutorial on setting up a new project here.

对于设置了一个新的项目的教程在这里

Wingwarealso seems to have some Django support.

Wingware似乎也有一些 Django 支持。

回答by Shantanu

You can simply go to:

你可以简单地去:

In Run -> Edit Configurations create new configuration
Script: path_to/manage.py
Script parameters: runserver

回答by onebraveman

I have met the problems today. At last, I finished it by:

我今天遇到了问题。最后,我完成了它:

  1. Create project in command line
  2. Create app in command line
  3. Just open the existing files and code in pycharm
  1. 在命令行中创建项目
  2. 在命令行中创建应用程序
  3. 只需在pycharm中打开现有文件和代码

The way I use have the benefits:

我使用的方式有以下好处:

  • don't need by professional pycharm
  • code django project in pycharm
  • 专业pycharm不需要
  • 在 pycharm 中编写 django 项目

回答by jitsm555

Pycharm integration with Django. Please try below steps for integration.

Pycharm 与 Django 的集成。请尝试以下步骤进行集成。

1. Add Django plugin inside your python interpreter

1. 在你的 Python 解释器中添加 Django 插件

enter image description here

在此处输入图片说明

Click + icon for installation of Django

单击 + 图标安装 Django

enter image description hereClick on the install package, it will take some time for installation.

在此处输入图片说明点击安装包,安装需要一些时间。

2. Check Django installation:

2.检查Django安装:

create a sample.py file

创建一个 sample.py 文件

  import django
  print(django.get_version()) 

3. Open the terminal and entered

3.打开终端,进入

django-admin startproject mysite(name of your project)

4. Run Server

4. 运行服务器

cd mysite
python manage.py runserver

You are able to start Django server at http://127.0.0.1:8000/

您可以在http://127.0.0.1:8000/启动 Django 服务器

5. Create the application

5. 创建应用程序

Open the another terminal and entered

打开另一个终端并输入

 cd mysite
 python manage.py startapp webapp(name of your app)

Please refer for more detail: https://www.youtube.com/watch?v=RUeLWSrtcFc

详情请参考:https: //www.youtube.com/watch?v=RUeLWSrtcFc

回答by lmiguelvargasf

Note:I use pipenvto install dependencies, so I will assume you have it installed. Otherwise, follow these instructions. I use Python 3, so I will also use Django 2.

注意:pipenv用来安装依赖项,所以我假设你已经安装了它。否则,请按照这些说明进行操作。我使用 Python 3,所以我也将使用 Django 2。

Using PyCharm 2019.1, 2018.3.1

使用 PyCharm 2019.1、2018.3.1

I can state that it has become pretty simple the way to start a Django project.

我可以说,启动 Django 项目的方式已经变得非常简单。

  1. Open PyCharm, and select Create New Project.
  1. 打开 PyCharm,然后选择Create New Project

enter image description here

在此处输入图片说明

  1. Choose Django in the left menu, provide a path and a name for your project. I strongly recommend using pipenvto manage your packages. Click on Create.
  1. 在左侧菜单中选择 Django,为您的项目提供路径和名称。我强烈建议使用它pipenv来管理您的包。单击创建。

enter image description here

在此处输入图片说明

That's it. Wait to PyCharm to create a virtual environment, install Django, and and index files. Now you are all set. Click on the play button in upper run corner and you will see the Django launch page you can see at the end of this answer.

就是这样。等待 PyCharm 创建虚拟环境,安装 Django,并索引文件。现在你已经准备好了。单击上运行角的播放按钮,您将看到 Django 启动页面,您可以在此答案的末尾看到。

!######################################################!

!############################################## #####!

Previous versions of PyCharm

以前版本的 PyCharm

First, let's create a project that will have the name of our Django project. I will call this directory mysite, and I will navigate inside of it.

首先,让我们创建一个项目,该项目将具有我们的 Django 项目的名称。我将调用此目录mysite,并在其中导航。

$ mkdir mysite && cd mysite

Second, let's create a virtual environment using pipenv:

其次,让我们使用pipenv以下方法创建一个虚拟环境:

$ pipenv --three

Third, open up PyCharm, and click on Open. Browse to mysitedirectory, and click on Open.

第三,打开 PyCharm,然后点击 Open。浏览到mysite目录,然后单击打开。

enter image description hereenter image description here

在此处输入图片说明在此处输入图片说明

PyCharm will automatically recognize the virtual environment that you previously created, so if you open a terminal inside PyCharm, you will see the interpreter is actually set.

PyCharm 会自动识别你之前创建的虚拟环境,所以如果你在 PyCharm 里面打开一个终端,你会看到解释器实际上已经设置好了。

enter image description here

在此处输入图片说明

Fourth, from the terminal, install Django using pipenv and create a new project called mysite

四、在终端中,使用pipenv安装Django并创建一个名为的新项目 mysite

$ pipenv install django
$ django-admin startproject mysite .

After running this commands your project directory should look like this:

运行此命令后,您的项目目录应如下所示:

enter image description here

在此处输入图片说明

mysite # this is the root project
├── Pipfile
├── Pipfile.lock
├── manage.py # this is the Manage script
└── mysite
    ├── __init__.py
    ├── settings.py # these are the settings
    ├── urls.py
    └── wsgi.py

Fifth, enable the Django support in Preferences -> Languages & Frameworks -> Django.

第五,在Preferences -> Languages & Frameworks -> Django.

enter image description here

在此处输入图片说明

Check Enable Django Support, and provide the Django project root, Settings, and Manage script. Click on Apply and then OK.

选中启用 Django 支持,并提供Django 项目根目录设置管理脚本。单击应用,然后单击确定。

enter image description here

在此处输入图片说明

Finally, you have to configure a Django server. Click on Add Configuration ...in the upper right.

最后,您必须配置一个 Django 服务器。点击Add Configuration ...右上角的。

enter image description here

在此处输入图片说明

Click on the plus sign in the upper left part, and select Django Server.

单击左上角的加号,然后选择 Django Server。

enter image description here

在此处输入图片说明

Provide a name, check Run browser if you want to open a tab when you click on the play button, and click on Apply and then OK.

提供名称,如果要在单击播放按钮时打开选项卡,请选中运行浏览器,然后单击应用,然后单击确定。

enter image description here

在此处输入图片说明

Now you just can click on the play button in the right upper section.

现在您只需单击右上方的播放按钮即可。

enter image description here

在此处输入图片说明

Congrats! You are all set if you visualize the following page.

恭喜!如果您想象以下页面,您就大功告成了。

enter image description here

在此处输入图片说明