Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PyCharm Create Django Project

PyCharm Create Django Project

PyCharm is a powerful Python IDE that offers excellent support for the Django framework, especially in its Professional edition (PyCharm Pro).

Launch PyCharm and create a new project

  1. Open PyCharm and click 'New Project'
  2. Select 'Django' on the left
  3. Set the project location (Location)
  4. Configure the Python interpreter (creating a new virtual environment is recommended)

Configure project parameters

Wait for project initialization

PyCharm will automatically:

  1. Create a virtual environment
  2. Install the latest stable version of Django
  3. Generate the basic project structure

Project structure analysis

After creation, you will see the following main files and directories:

Project root directory

Project configuration directory

Other important files


Run the Django development server

Run via PyCharm

  1. Click the run configuration dropdown in the top-right corner of PyCharm
  2. Select 'Configuration -> Edit'

  3. Make sure the Django server is configured
  4. Click the green Run button

Run via the command line

  1. Open PyCharm's terminal
  2. Enter the command:python3 manage.py runserver

    After successful startup, the terminal shows the following information

    :

  3. Visit http://127.0.0.1:8000 to view the default page:

Common Problem Solutions


Create a Django app

Create a new app

Enter the following in PyCharm's terminal:

python3 manage.py startapp myapp

Add the app to settings.py 's INSTALLED_APPS:

INSTALLED_APPS = [

    ...

    'myapp',

]

In the project urls.py , include the app's routes:

Example

from

django.

contrib

import

admin

from

django.

urls

import

path

urlpatterns

=

[

    path

(

'admin/'

,

admin.

site

.

urls

)

,

    path

(

'myapp/'

,

include

(

'myapp.urls'

)

)

,

]

App directory structure

Configure URL routing under the myapp directory

Open myapp/models.py and define a model. For example:

Example

from

django.

db

import

models

class

Post

(

models.

Model

)

:

    title

=

models.

CharField

(

max_length

=

200

)

    content

=

models.

TextField

(

)

    created_at

=

models.

DateTimeField

(

auto_now_add

=

True

)

   

def

str

(

self

)

:

       

return

self

.

title

After saving, run python manage.py makemigrations and python manage.py migrate to update the database.

** Create views and URLs:**

Define the view in myapp/views.py:

Example

from

django.

shortcuts

import

render

from

.

models

import

Post

def

post_list

(

request

)

:

    posts

=

Post.

objects

.

all

(

)

   

return

render

(

request

,

'myapp/post_list.html'

,

{

'posts'

: posts

}

)

Configure the URL in myapp/urls.py (must be created manually):

Example

from

django.

urls

import

path

from

.

import

views

urlpatterns

=

[

    path

(

''

,

views.

post_list

,

name

=

'post_list'

)

,

]

Create the DjangoProject/templates/myapp/ folder in the project root, then create post_list.html under that folder:

Example

   

<p>Post List</p>

   

Posts

   

Make sure the TEMPLATES setting in settings.py includes the correct template directory:



TEMPLATES = [

    {

        'BACKEND': 'django.template.backends.django.DjangoTemplates',

        'DIRS': [BASE_DIR / 'templates'],

        ...

    }

]