PyCharm Creating Django Project

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

Launch PyCharm and create a new project

  1. Open PyCharm, click "New Project"
  2. Select "Django" on the left
  3. Set the project location (Location)
  4. Configure the Python interpreter (it is recommended to create a new virtual environment)

Configure project parameters

  • Project name: Use a combination of lowercase letters and underscores (e.g., DjangoProject)
  • Template language: Select Django by default
  • Front-end framework: You can select as needed (beginners can leave it unselected)
  • Enable Django admin: Can be checked

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 breakdown

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

Project root directory

  • manage.py: Django command-line tool
  • Directory with the same name as the project(e.g., DjangoProject/): Contains the main project configuration

Project configuration directory

  • init.py: Identifies this as a Python package
  • settings.py: Project configuration file
  • urls.py: URL routing configuration
  • wsgi.py: WSGI server configuration

Other important files

  • requirements.txt: Project dependency file (PyCharm may generate it automatically)
  • venv/: Virtual environment directory (if you chose to create a virtual environment)

Run the Django development server

Run via PyCharm

  1. Click the run configuration dropdown menu in the upper-right corner of PyCharm
  2. Select "Configuration -> Edit"

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

Run via command line

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

    After successful startup, the terminal information is displayed as follows:

    :

  3. Visit http://127.0.0.1:8000View the default page:

Troubleshooting

  • Port conflict: Usepython manage.py runserver 8080Specify another port
  • Database not migrated: Must be executed on first runpython manage.py migrate

Create a Django app

Create a new app

Enter the following in PyCharm's terminal:

python3 manage.py startapp myapp

Add the app tosettings.pyofINSTALLED_APPS:

INSTALLED_APPS = [
    ...
    'myapp',
]

In the projecturls.pyinclude the app's URL 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

  • migrations/: Database migration files
  • admin.py: Admin backend configuration
  • apps.py: Application configuration
  • models.py: Data model definitions
  • tests.py: Test code
  • views.py: View functions

Configure URL routing under the myapp directory

Open myapp/models.py to define the 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 views 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 URLs in myapp/urls.py (needs to 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 directory, then create post_list.html under that folder.

Example

<!DOCTYPE html>
<html>
<head>
    <title>Post List</title>
</head>
<body>
    <h1>Posts</h1>
    <ul>
        {% for post in posts %}
            <li>{{ post.title }} - {{ post.created_at }}</li>
        {% empty %}
            <li>No posts available.</li>
        {% endfor %}
    </ul>
</body>
</html>

Ensure the TEMPLATES setting in settings.py contains the correct template directory:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR / 'templates'],
        ...
    }
]
Other extensions