YouTip LogoYouTip

Pycharm Django

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 New Project 1. Open PyCharm, click "New Project" 2. Select "Django" on the left 3. Set project location 4. Configure Python interpreter (recommended to create new virtual environment) !(#) ### Configure Project Parameters * **Project name**: Use lowercase letters and underscores (e.g., DjangoProject) * **Template language**: Default selection is Django * **Frontend framework**: Optional based on needs (beginners may skip) * **Enable Django admin**: Can be checked !(#) ### Wait for Project Initialization PyCharm will automatically: 1. Create virtual environment 2. Install latest stable Django version 3. Generate basic project structure * * * ## Project Structure Analysis After creation, you will see the following main files and directories: !(#) ### Project Root Directory * **manage.py**: Django command-line utility * **Project-name directory** (e.g., DjangoProject/): Contains 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 dependencies file (may be auto-generated by PyCharm) * venv/: Virtual environment directory (if virtual environment was selected) * * * ## Run Django Development Server ### Run via PyCharm 1. Click the run configuration dropdown in the upper right corner of PyCharm 2. Select "Configure -> Edit" !(#) 3. Ensure Django server is configured 4. Click the green run button !(#) ### Run via Command Line 1. Open PyCharm's terminal 2. Enter command: `python3 manage.py runserver` !(#) After successful startup, terminal information displays as follows: !(#) 3. Visit http://127.0.0.1:8000 to view the default page: !(#) ### Common Issues Resolution * **Port conflict**: Use `python manage.py runserver 8080` to specify another port * **Database not migrated**: First run requires executing `python manage.py migrate` * * * ## Create Django Application ### Create New Application Enter in PyCharm's terminal: python3 manage.py startapp myapp !(#) Add the application to INSTALLED_APPS in `settings.py`: INSTALLED_APPS = [ ... 'myapp',] !(#) Include application routes in project `urls.py`: ## Example from django.contrib import admin from django.urls import path urlpatterns =[ path('admin/', admin.site.urls), path('myapp/', include('myapp.urls')), ] !(#) ### Application 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 in myapp Directory Open myapp/models.py and define models. 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 DjangoProject/templates/myapp/ folder in project root directory, then create post_list.html in that folder: ## Example Post List

Posts

    {% for post in posts %}
  • {{ post.title }} - {{ post.created_at }}
  • {% empty %}
  • No posts available.
  • {% endfor %}
Ensure TEMPLATES setting in settings.py includes the correct template directory: TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [BASE_DIR / 'templates'], ... }]
← Swagger TutorialJava Mybatis β†’