https://github.com/zhao-cpu/python-study
https://github.com/zhao-cpu/python-study
Last synced: over 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/zhao-cpu/python-study
- Owner: zhao-cpu
- Created: 2023-05-18T13:11:19.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2023-05-18T13:16:12.000Z (about 3 years ago)
- Last Synced: 2025-01-05T09:43:39.959Z (over 1 year ago)
- Language: Python
- Size: 698 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# django
py --version
Python 3.11.1
django-admin --version
4.2.1
## 下载
```python
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple django
```
## 创建项目
```python
django-admin startproject project
```
## 创建一个 app
```python
python manage.py startapp app
```
### 注册 app
project/settings
```python
# app名称.apps.app名称Config
INSTALLED_APPS = [
...,
'app.apps.AppConfig',
]
```
## 编写 url
project/urls
```python
from app import views
urlpatterns = [
path('index/', views.index),
]
```
## 编写视图函数
app/views
```python
from django.shortcuts import render, HttpResponse
def index(request):
return HttpResponse('index 函数')
```
## 启动项目
如果首次启动
```python
python manage.py migrate
```
```python
python manage.py runserver
```
## 使用模板和静态文件
app/views.py
```python
def user_list(request):
return render(request, 'user_list.html')
```
需要重新启动才会显示图片
app/static/image/xm.png
```python
{% load static %}

{% block content %}{% endblock %}