跳至主要内容

django 详细介绍



网站示例

wagtail
是一个用Python编写的免费开源 内容管理系统(CMS),在使用Django框架的网站中很流行

pdf文档下载

Django使您可以更轻松地以更少的代码更快地构建更好的Web应用程序

Django软件包是Django项目中可重复使用的应用程序,网站,工具等的目录https://djangopackages.org/

https://www.djangosites.org/

•  GeoDjango: 定制的GIS Web框架 
•  Django Debug Toolbar:调试工具 
•  Django Easy Maps: 地图显示工具 
•  Django Haystack: 模块化搜索工具








To create a Django application that performs CRUD operations, follow the following steps.

1. Create a Project $ django-admin startproject crudexample
2. Create an App $ python3 manage.py startapp employee
3. Project Structure
4.Run the command to migrate the migrations.$ python3 manage.py migrate
5.Run Server To run server use the following command.$ python3 manage.py runserver

Access to the Browser Access the application by entering localhost:8000/show, it will show all the available employee records. Initially, there is no record.

Adding Record Click on the Add New Record button and fill the details.

This section also allows, update and delete records from the actions column. After saving couple of records, now we have following records.

Update Record Lets update the record of x by clicking on edit button. It will display record of x in edit mode.

Lets, suppose I update x to x kumar then click on the update button. It updates the record immediately. Click on update button and it redirects to the following page. See name is updated. Same like, we can delete records too, by clicking the delete link.

Delete Record Suppose, I want to delete x, it can be done easily by clicking the delete button.


框架的名称受到比利时著名的吉普赛爵士吉他手Django Reinhardt的艺名的启发,因此为该框架创建了许多便捷附件的开发人员将他们称为Jazzband

对于ABC版本的Django,AB代表功能版本,C代表补丁版本
例如:Django version 2.1.3
2.1 is a number of feature release(功能版本
3 is a number of patch release(补丁版本

LTS: Long-Term Support (长期支持)软件开发中的著名标准。这意味着开发人员将在更长的时间内支持该版本的框架(对于Django,通常为3年或更长时间),您可以安全地将版本更新到较新的修补程序版本,而不必担心会破坏与源代码的兼容性

Django采用了MVT的软件设计模式,即模型(Model),视图(View)和模板(Template)

Model-View-Controller(MVC的主要思想是将责任划分为三个部分。模型部分包含业务逻辑,视图代表应用程序,控制器管理两个逻辑之间的数据流

Model -->        my_app/models.py
View -->           my_app/templates/my_app/index.html
Controller -->  my_app/views.py

在商店应用程序中,它可以是客户,产品和购买商品;在博客中,业务对象可以是作者,帖子和评论。

Django默认提供User 和 Group模型,来自django.contrib.auth.models

在cmd中创建虚拟环境
python -m venv web
激活虚拟环境
web\scripts\activate.bat
退出虚拟环境
deactivate



先安装django后创建项目
pip install django==3.0.2
要创建项目和第一个应用程序,请在命令行中运行以下命令
django-admin startproject magazine
cd magazine
django-admin startapp blog
mkdir blog\templates\blog

magazine/
├── blog
    ├── ...
    ├── models.py    # Model
    ├── views.py     # Controller
    └── templates    # View
        └── blog
            └── index.html
├── magazine
    ├── ...
    └── urls.py      # Controller
└── templates        # View
    └── base.html


为应用程序创建“templates”目录时
应将其命名为“<application name>/templates/<application name>”

你有 blog/templates/index.html and shop/templates/index.html 文件,其中blog和shop是项目中的应用程序。这种布局的主要缺陷是什么?
Django template loader返回找到的第一个"index.html" 。因此,您无法控制自己得到哪一个

magazine\blog\templates\blog\index.html
<!DOCTYPE html>
<title>Movies</title>
 
<h1>Films by {{ director }}</h1>
 
<ul>
{% for movie in movies %}
  <li>{{ movie.year }} - {{ movie.title }}</li>
{% endfor %}
</ul>

magazine\blog\views.py
from django.conf import settings
from django.shortcuts import render
from django.views import View
 
movies = [
    {
        'title''Catchfire',
        'year'1990,
    },
    {
        'title''Mighty Ducks the Movie: The First Face-Off',
        'year'1997,
    },
    {
        'title''Le Zombi de Cap-Rouge',
        'year'1997,
    },
]
 
 
class MovieView(View):
    def get(self, request, *args, **kwargs):
        return render(
            request, 'blog/index.html', context={
                'director': settings.DIRECTOR,
                'movies': movies,
            }
        )



# magazine\magazine\urls.py
from django.urls import path
from blog.views import MovieView
 
urlpatterns = [
    path('', MovieView.as_view()),
]



magazine\magazine\settings.py中修改INSTALLED_APPS变量添加变量DIRECTOR
INSTALLED_APPS = [
    ...
    'blog',
]


DIRECTOR = 'James'



在<project_name>/settings.py文件中,您仅定义Variables变量,而不编写任何函数或类
BASE_DIR -->               The root of your project
INSTALLED_APPS -->    All the applications you wish to use
DATABASES  -->           Configuration of databases
ALLOWED_HOSTS -->     The hostnames for your server

开发服务器如果出现问题,您想显示回溯和其他有用信息。您可以DEBUG = True在settings.py模块中添加调试模式,生产服务器时,强烈建议设置DEBUG = False。

IS_RELEASE_SERVER = input().lower() == 'true'
DEBUG = not IS_RELEASE_SERVER

# 启动本地服务器
python manage.py runserver
等同于python manage.py runserver 8000


浏览器打开http://127.0.0.1:8000/

Controller由两种类型的文件组成:"views.py" and "urls.py".
在"urls.py"中,定义服务的路由。 Routing 是将请求链接与适当的视图处理程序进行匹配的过程

在"views.py"中定义了视图处理程序,它们在模型和视图之间扮演中介者的角色。一个视图处理程序是一个函数或一个类来响应请求。由于客户端和服务器之间的通信是HTTP协议的一种实现,因此处理程序将使用状态码进行回答


http状态码

让我们看看在有网站请求时它们如何交互。

#用户在视图中看到链接或按钮,然后按它并创建请求。
1. A user sees a link or a button in a View, presses it and creates a request.

#控制器收到请求。
2. The Controller receives the request. 

#它将请求传递给适当的处理程序。
3. It passes the request to the appropriate handler.

#处理程序调用Model方法从数据存储中检索对象。
4. The handler calls Model methods to retrieve objects from data storage.

#选择View模板以呈现响应。
5. It chooses the View template to render a response.

#用户看到响应。
6. A user sees a response.

 





每当我们创建模型,删除模型或更新我们项目的任何models.py中的任何内容时。我们需要运行两个命令makemigrationsmigratemakemigrations基本上会为预安装的应用程序(可以在settings.py中的已安装应用程序中查看)和您在已安装的应用程序中添加的新创建的应用程序模型生成SQL命令,而migration会在数据库文件中执行这些SQL命令。









评论

此博客中的热门博文

学习地址

清华大学计算机系课程攻略 https://github.com/PKUanonym/REKCARC-TSC-UHT 浙江大学课程攻略共享计划 https://github.com/QSCTech/zju-icicles https://home.unicode.org/ 世界上的每个人都应该能够在手机和电脑上使用自己的语言。 http://codecanyon.net   初次看到这个网站,小伙伴们表示都惊呆了。原来代码也可以放在网上卖的?!! 很多coder上传了各种代码,每个代码都明码标价。看了下销售排行,有的19刀的卖了3万多份,额di神啊。可以看到代码的演示效果,真的很漂亮。代码以php、wordpress主题、Javascript、css为主,偏前台。 https://www.lintcode.com/ 算法学习网站,上去每天刷两道算法题,走遍天下都不怕。 https://www.codecademy.com/ 包含在线编程练习和课程视频 https://www.reddit.com/ 包含有趣的编程挑战题,即使不会写,也可以查看他人的解决方法。 https://ideone.com/ 在线编译器,可运行,可查看代码示例。 http://it-ebooks.info/ 大型电子图书馆,可即时免费下载书籍。 刷题 https://github.com/jackfrued/Python-100-Days https://github.com/kenwoodjw/python_interview_question 面试问题 https://github.com/kenwoodjw/python_interview_question https://www.journaldev.com/15490/python-interview-questions#python-interpreter HTTP 身份验证 https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Authentication RESTful 架构详解 https://www.runoob.com/w3cnote/restful-architecture.html https://www.rosettacode.org/wiki/Rosetta_C...

MechanicalSoup

用于自动与网站交互的 Python 库。 MechanicalSoup 自动存储和发送 cookie,跟踪重定向,并且可以跟踪链接和提交表单。 它不执行 JavaScript。 https://github.com/MechanicalSoup/MechanicalSoup https://mechanicalsoup.readthedocs.io/en/stable/index.html https://realpython.com/python-web-scraping-practical-introduction/ pip show Mechanicalsoup 找到模块的安装位置 https://stackoverflow.com/questions/54352162/download-file-with-mechanicalsoup # Install dependencies # pip install requests # pip install BeautifulSoup4 # pip install MechanicalSoup # Import libraries import mechanicalsoup import urllib.request import requests from bs4 import BeautifulSoup import re # Create a browser object that can collect cookies browser = mechanicalsoup.StatefulBrowser() browser.open("https://www.ons.gov.uk/economy/grossdomesticproductgdp/timeseries/l2kq/qna") browser.download_link(link_text=".xls",file="D:/ONS_Data.xls" )

安装和卸载软件(msi\exe)

如何判断一个软件是64位的还是32位的? 情况1、 未安装--右键安装程序查看属性,兼容性,勾选兼容模式查看最低适配是vista的是64位,反之32位, 不太准确 情况2、 已安装--运行软件,64位操作系统打开任务管理器看进程后缀名,带*32就是32位,反之64位 https://www.joci.net/xxbk/126251/ FileMon 和 Regmon 不再可供下载。从Windows 2000 SP4,Windows XP SP2,Windows Server 2003 SP1和Windows Vista开始的Windows版本上,它们已被 Process Monitor 取代 https://adamtheautomator.com/procmon/ Process Monitor 是Windows的高级监视工具,它显示实时文件系统,注册表和进程/线程活动。 它结合了两个旧的Sysinternals实用程序 Filemon 和  Regmon的功能 ,并添加了广泛的增强功能列表,包括丰富的和非破坏性的过滤,全面的事件属性(例如会话ID和用户名),可靠的过程信息,带有集成符号的完整线程堆栈支持每个操作,同时记录到文件等。 它独特的强大功能将使Process Monitor成为您的系统故障排除和恶意软件搜索工具包中的核心实用程序。 https://wikileaks.org/ciav7p1/cms/page_42991626.html HKLM\Software\Microsoft\Cryptography\MachineGuid Machine GUID/Cryptography GUID---该密钥通常用作机器的唯一标识符。它也已用于将两台计算机链接在一起-在某些情况下,计算机GUID是与设备(MP3播放器等)一起传递的。 Machine GUID不是唯一 https://docs.microsoft.com/zh-cn/windows/win32/properties/props-system-identity-uniqueid?redirectedfrom=MSDN UniqueID才是唯一 注册表 是存储系统和应用程序的设置信息 打开注册表的方式很简单:cmd中输入regedit 卸载路径只有一个 已安装32位的程序,如果是系统是32位...