Django--Learning and understanding

  1. Create a virtual environment in anaconda

  2. Installing Django

    复制代码
    pip install django

    Django is a Python web framework that provides many features such as ORM, authentication, forms, templates, etc. It can help you develop web applications faster and easier.

  3. Installing the DRF

    复制代码
    pip install djangorestframework

    DRF is a powerful and flexible Django-based RESTful framework that provides many tools and libraries to help you quickly develop web applications based on RESTful apis.

  4. Install Django-Filter

    复制代码
    pip install django-filter

    Description::Integration with DRF --- django-filter 23.2 documentation

    Django-filter is a Django-based library that provides a simple, flexible way to Filter sets of Django model queries. Django-Filter's API allows developers to use simple query expressions, build and apply complex filters, and select and exclude data in query sets.

    Djangos Filter provides a more elegant API specification through its integration with DRF Spectacular, which supports data filtering and querying as expressed by the OpenAPI specification.

  5. Installing Django Spectacular

    复制代码
    pip install drf_spectacular

    Description: DRF Spectacular is the OpenAPI specification tool for DRF. It automatically builds and generates OpenAPI specification documentation, and provides convenient API testing tools that make it easier to create, test, and maintain RESTful apis. It also supports integration with Django Filter, which allows you to filter query data by URL parameters.

Once django is installed, create a project using Django-admin, the tool that comes with Django:

复制代码
django-admin startproject django_news

Go to cd django_news.

复制代码
django_news
├── django_news              // 项目全局文件目录
│   ├── __init__.py
│   ├── settings.py          // 全局配置
│   ├── urls.py              // 全局路由
│   └── wsgi.py              // WSGI服务接口
└── manage.py                // 项目管理脚本

To run a Development Server using manage.py:

复制代码
python manage.py runserver

Implement custom apps

Let's create our first custom App, named news:

复制代码
python manage.py startapp news

The resulting news application folder structure looks like this:

复制代码
news                     // news 应用目录
├── __init__.py          // 初始化模块
├── admin.py             // 后台管理配置
├── apps.py              // 应用配置
├── migrations           // 数据库迁移文件目录
│   └── __init__.py      // 数据库迁移初始化模块
├── models.py            // 数据模型
├── tests.py             // 单元测试
└── views.py             // 视图

The view function you just wrote needs to be accessible to the system. So first implement the routing table of the sub-application news and create the news/urls.py file as follows:

复制代码
from django.urls import path
from . import views
urlpatterns = [
    path('', views.index, name='index'),
]

Implement the first Django template

Let's implement our first Django template. Create a templates directory in the news directory, then create a news directory in the templates directory, and then create the index.html file in the inner news directory:

复制代码
mkdir -p news/templates/news
touch news/templates/news/index.html

Because Djangos template lookup mechanism collects all the templates in the application together, if two template names conflict, one of the templates will not be accessible correctly. If it is placed in the news subfolder, it can be accessed through news/index.html, and conflicts are avoided through the namespace mechanism.

The code for the template is as follows:

复制代码
{% if news_list %}
  <ul>
  {% for elem in news_list %}
    <li>
      <h3>{{ elem.title }}</h3>
      <p>{{ elem.content }}</p>
    </li>
  {% endfor %}
  </ul>
{% else %}
  <p>暂无新闻</p>
{% endif %}

Once defined, run the following command to create the migration file:

复制代码
python manage.py makemigrations

The news/migrations/0001_initial.py migration script was successfully automatically created. Then proceed to the database migration:

复制代码
python manage.py migrate

After the database migration is complete, create a super user for logging in to the background management.

复制代码
python manage.py createsuperuser

Enter the user name and password as prompted. Interview localhost:8000/admin,Enter the login page of the background system:

Configure the background management interface

We did not implement the background management interface for the news application. Enter the following code in news/admin.py:

Enter the background management system again, you can see the news application and Post model created just now.

Adds a data query to the view

Finally, we add the code to the view to query from the database:

复制代码
from django.shortcuts import render

from .models import Post


def index(request):
    context = { 'news_list': Post.objects.all() }
    return render(request, 'news/index.html', context=context)

Reference article

https://zhuanlan.zhihu.com/p/98788776

相关推荐
CodeCraft Studio2 小时前
PDF处理控件Aspose.PDF教程:使用 Python 将 PDF 转换为 Base64
开发语言·python·pdf·base64·aspose·aspose.pdf
程序员爱钓鱼3 小时前
Go语言实战案例 — 工具开发篇:实现一个图片批量压缩工具
后端·google·go
困鲲鲲4 小时前
Python中内置装饰器
python
摩羯座-185690305944 小时前
Python数据可视化基础:使用Matplotlib绘制图表
大数据·python·信息可视化·matplotlib
爱隐身的官人5 小时前
cfshow-web入门-php特性
python·php·ctf
gb42152875 小时前
java中将租户ID包装为JSQLParser的StringValue表达式对象,JSQLParser指的是?
java·开发语言·python
THMAIL5 小时前
量化股票从贫穷到财务自由之路 - 零基础搭建Python量化环境:Anaconda、Jupyter实战指南
linux·人工智能·python·深度学习·机器学习·金融
~-~%%5 小时前
从PyTorch到ONNX:模型部署性能提升
人工智能·pytorch·python
蒋星熠5 小时前
Flutter跨平台工程实践与原理透视:从渲染引擎到高质产物
开发语言·python·算法·flutter·设计模式·性能优化·硬件工程
ChinaRainbowSea5 小时前
7. LangChain4j + 记忆缓存详细说明
java·数据库·redis·后端·缓存·langchain·ai编程