-
安装模块
pip install django
-
添加环境变量
Python.exe,script/django-admin.exe
-
创建工程
django-admin startproject example example - example # 对整个程序进行配置 - init.py - settings.py # 配置文件 - urls.py # URL对应关系 - wsgi.py # 遵循WSGI规范. uwsgi + nginx - manage.py # 管理Django程序: - python manage.py - python manage.py startapp xx - python manage.py makemigrations - python manage.py migrate
-
运行项目
编译环境配置,添加设置文件settings.py到DJANGO_SETTINGS_MODULE 运行 example\manage.py python manage.py runserver
-
app的应用
- 主站app - 后台管理app app目录: - migrations # 记录数据库修改表结构 - admin.py # Django为我们提供的后台管理 - apps.py # 配置当前app - models.py # ORM,写指定的类,通过命令可以创建数据库结构 - tests.py # 单元测试 - views.py # 业务代码
-
项目配置
- 配置模板的路径 TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join(BASE_DIR, 'templates')], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, }, ] - 配置静态目录 static STATICFILES_DIRS = ( os.path.join(BASE_DIR,"static"), ) - settings中 middleware # 注释 csrf
-
定义显示规则
- 路由规则 url.py "login"---->函数位置 - 定义视图函数 app下面的views.py def func(request): # request.method GET/POST # http://127.0.0.1:8000/home?nid=123&name=Jim # request.GET.get("",None) # 获取请求发来的数据 # request.POST.get("",None) # return HttpResponse("字符串") # return render(request,"HTML模板的路径") # return redirect("/只能填URL")
-
模板渲染
特殊的模板语言 - {{变量}} def func(request): return render(request,"login.html",{"current_user":current_user}) <html> ... <body> <div>{{current_user}}</div> </body> </html> - for循环 def func(request): return render(request,"login.html",{"current_user":current_user,"user_list":["Jim","Tom"]}) <html> ... <body> <div>{{current_user}}</div> <table> {% for tmp in user_list %} <tr>tmp</tr> {% endfor %} </table> </body> </html> - if条件语句 {% if a > 10 %} <div>123</div> {% else %} <div>456</div> {% endif %}
网页制作之Django-基本知识
最新推荐文章于 2024-06-04 08:03:29 发布