• 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.

    1. django_news
    2. ├── django_news // 项目全局文件目录
    3. │ ├── __init__.py
    4. │ ├── settings.py // 全局配置
    5. │ ├── urls.py // 全局路由
    6. │ └── wsgi.py // WSGI服务接口
    7. └── 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:

    1. news // news 应用目录
    2. ├── __init__.py // 初始化模块
    3. ├── admin.py // 后台管理配置
    4. ├── apps.py // 应用配置
    5. ├── migrations // 数据库迁移文件目录
    6. │ └── __init__.py // 数据库迁移初始化模块
    7. ├── models.py // 数据模型
    8. ├── tests.py // 单元测试
    9. └── 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:

    1. from django.urls import path
    2. from . import views
    3. urlpatterns = [
    4. path('', views.index, name='index'),
    5. ]

    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:

    1. mkdir -p news/templates/news
    2. 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:

    1. {% if news_list %}
    2. <ul>
    3. {% for elem in news_list %}
    4. <li>
    5. <h3>{{ elem.title }}</h3>
    6. <p>{{ elem.content }}</p>
    7. </li>
    8. {% endfor %}
    9. </ul>
    10. {% else %}
    11. <p>暂无新闻</p>
    12. {% 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:

    1. from django.shortcuts import render
    2. from .models import Post
    3. def index(request):
    4. context = { 'news_list': Post.objects.all() }
    5. return render(request, 'news/index.html', context=context)

    Reference article

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

  • 相关阅读:
    她从家乡自贡起步,努力奋斗进京任职
    CTFHub | UA注入
    利用commit理解镜像构成
    CSP赛前复习总结
    通过瑞利判据对显微镜物镜进行分辨率研究
    【Spring面试】十、SpringBoot相关
    对话句子互动创始人李佳芮 | AIGC结合私域运营影响不可估量
    算法设计与分析 SCAU11090 最大m段乘积和最小m段和(优先做)
    Spring Boot文档阅读笔记-CORS Support
    【kali-密码攻击】(5.1.1)密码在线破解:Hydra(图形界面)
  • 原文地址:https://blog.csdn.net/m0_72572822/article/details/133781412