Django 学习

使用Django表格

 


使用Django view 视图

polls/views.py
将视图绑定到url链接
polls/urls.py

使用Django template

创建index.html文件
polls/template/polls/index.html

# return HttpResponse(template.render(context, request))

return render(request, ‘polls/index.html’, context)
* context is a dictionary
polls/templates/polls/detail.html

Raising 404

get_object_or_404()

Template system

Tags: if, for, url


使用API操作数据库

python manage.py shell
数据显示为Object
在models.py中使用__str__方法:
def __str__(self):
return self.textField
这个很重要,这不仅帮助在shell中显示,同时也帮助在数据库管理后台的显示优化。

增加自定义方法

polls/models.py

使用Django数据库后台

创建管理员用户
python manage.py createsuperuser
将子应用模型注册到数据库后台中
polls/admin.py


搭建数据库并且连接

https://docs.djangoproject.com/en/3.1/intro/tutorial02/

已安装的应用

数据库迁移

python manage.py migrate

创建数据库模型

polls/models.py

  • 创建数据库架构模型
  • 创建可访问模型数据的API

将子应用Polls安装到项目应用列表中

mysite/settings.py::INSTALLED_APPS
python manage.py makemigrations polls
python manage.py sqlmigrate polls 0001 -> view mysql codes to run
python manage.py migrate


创建项目子应用

python manage.py startapp polls
生成应用结构

polls/
    __init__.py
    admin.py
    apps.py
    migrations/
        __init__.py
    models.py
    tests.py
    views.py

创建view

polls/views.py

创建子应用url配置文件 对接 url请求与view的关系

polls/urls.py

将子应用的rul配置加入根目录的url配置文件中

mysite/urls.py


检查是否安装django

python -m django –version
django中具有自己的scripts,包括startproject, startapp等。

创建新的projet

django-admin startproject mysite
生成的项目结构:

mysite/
    manage.py
    mysite/
        __init__.py
        settings.py
        urls.py
        asgi.py
        wsgi.py

文件说明:

  • The outer mysite/ root directory is a container for your project. Its name doesn’t matter to Django; you can rename it to anything you like.
  • manage.py: A command-line utility that lets you interact with this Django project in various ways. You can read all the details about manage.py in django-admin and manage.py.
  • The inner mysite/ directory is the actual Python package for your project. Its name is the Python package name you’ll need to use to import anything inside it (e.g. mysite.urls).
  • mysite/__init__.py: An empty file that tells Python that this directory should be considered a Python package. If you’re a Python beginner, read more about packages in the official Python docs.
  • mysite/settings.py: Settings/configuration for this Django project. Django settings will tell you all about how settings work.
  • mysite/urls.py: The URL declarations for this Django project; a “table of contents” of your Django-powered site. You can read more about URLs in URL dispatcher.
  • mysite/asgi.py: An entry-point for ASGI-compatible web servers to serve your project. See How to deploy with ASGI for more details.
  • mysite/wsgi.py: An entry-point for WSGI-compatible web servers to serve your project. See How to deploy with WSGI for more details.

运行Django服务

python manage.py runserver


资料:
Django documentation: https://docs.djangoproject.com/en/3.1/topics/

 


欢迎大家关顾我的Facebook主页「Y Life Book」以及我的公众微信号「不点语书」,我们下期见。

Leave a Comment

Your email address will not be published. Required fields are marked *