【Django 笔记】第一个demo

1. pip  安装 

 2. django 指令 

D:\software\python3\anconda3\Lib\site-packages\django\bin>django-adminType 'django-admin help <subcommand>' for help on a specific subcommand.Available subcommands:[django]checkcompilemessagescreatecachetabledbshelldiffsettingsdumpdataflushinspectdbloaddatamakemessagesmakemigrationsmigraterunserversendtestemailshellshowmigrationssqlflushsqlmigratesqlsequenceresetsquashmigrationsstartappstartprojecttesttestserver
Note that only Django core commands are listed as settings are not properly configured (error: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.).

 3.创建项目  

D:\software\python3\anconda3\Lib\site-packages\django\bin>django-admin  startproject guest
CommandError: [WinError 5] 拒绝访问。: 'D:\\software\\python3\\anconda3\\Lib\\site-packages\\django\\bin\\guest

 python - Django creating new project - access is denied - Stack Overflow

问题解决 ; 

选择其他路径创建,不要再 'D:\\software\\python3\\anconda3\\Lib\\site-packages\\django\\bin下执行命令

 

  4 目录结构 : 

 查看 manange 提供的命令

E:\data\python\djaongo_prj>cd  guestE:\data\python\djaongo_prj\guest>  python manage.pyType 'manage.py help <subcommand>' for help on a specific subcommand.Available subcommands:[auth]changepasswordcreatesuperuser[contenttypes]remove_stale_contenttypes[django]checkcompilemessagescreatecachetabledbshelldiffsettingsdumpdataflushinspectdbloaddatamakemessagesmakemigrationsmigratesendtestemailshellshowmigrationssqlflushsqlmigratesqlsequenceresetsquashmigrationsstartappstartprojecttesttestserver[sessions]clearsessions[staticfiles]collectstaticfindstaticrunserverE:\data\python\djaongo_prj\guest>

官方命令文档 : django-admin and manage.py | Django documentation | Django 

5.创建应用   sign

E:\data\python\djaongo_prj\guest>python  manage.py startapp  sign

 

  运行 :

 python    manager.py  runserver

 http://127.0.0.1:8000/

如果运行失败可能出现端口占用   : python    manager.py  runserver  127.0.0.1:8001  运行

127.0.0.1 表示本机的IP地址  8001: 是端口

guest/settings.py  添加项目 sign

INSTALLED_APPS = ['django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles','sign'
]

6显示 Hello Django! 

guest/urls.py

 url(r'^index/$', views.index),  # 添加 index 、路径配置

"""guest URL ConfigurationThe `urlpatterns` list routes URLs to views. For more information please see:https://docs.djangoproject.com/en/2.2/topics/http/urls/
Examples:
Function views1. Add an import:  from my_app import views2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views1. Add an import:  from other_app.views import Home2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf1. Import the include() function: from django.urls import include, path2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.conf.urls import url
from django.contrib import admin
from django.urls import path
from sign import viewsurlpatterns = [url(r'^admin/', admin.site.urls),url(r'^index/$', views.index),  # 添加 index 、路径配置
]

sign/views.py

from django.shortcuts import render# Create your views here.
from django.http  import  HttpResponse# 定义inex 函数,通过HttpResponse 类向客户端返回字符创
def index(request):return HttpResponse("Hello Django!")

7.使用模版 

创建 templates   (文件名称不要随便改django 默认从这个路径下找html)

sign/templates/index.html

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title><h1> Hello Django!<h1>
</head>
<body></body>
</html>

8.修改 sign/views.py

  return  render(request,"index.html")  使用 render函数
 

from django.shortcuts import render# Create your views here.
from django.http  import  HttpResponse# 定义inex 函数,通过HttpResponse 类向客户端返回字符创
def index(request):#return HttpResponse("Hello Django!")  # 使用Django 的render函数return  render(request,"index.html")

 Django 工作流 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/91548.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

精彩回顾 | 迪捷软件亮相2023世界智能网联汽车大会

2023年9月24日&#xff0c;2023世界智能网联汽车大会&#xff08;以下简称大会&#xff09;在北京市圆满落幕。迪捷软件北京参展之行圆满收官。 本次大会由工业和信息化部、公安部、交通运输部、中国科学技术协会、北京市人民政府联合主办&#xff0c;是我国首个经国务院批准的…

WPF中的控件

内容控件&#xff1a;label、border Window控件 Label控件 Border控件 内容控件 Button控件 点击取消按钮关闭程序&#xff1b;点击登录按钮打开BorderWindow窗口。 TextBox控件 PasswordBox控件 TextBlock控件 加载窗口时显示TextBlock中的内容 RadioButton控件 CheckBox控件…

react antd InputNumber只允许输入数字的方法

一、前言 前端项目是react&#xff0c;使用了antd&#xff0c;需要一个数字输入框&#xff0c;只允许输入数字。 二、代码样例 import { Form, InputNumber } from antd;const FormItem Form.Item;const formItemLayout {labelCol: {xs: { span: 24 },sm: { span: 8 },},…

深入探讨java -jar命令:详解及代码演示

引言&#xff1a; 在Java开发中&#xff0c;我们经常需要将多个类打包成一个可执行的jar文件&#xff0c;以便于分发和运行。而使用java -jar命令是一种方便快捷地在命令行中运行Java可执行jar文件的方法。本文将详细介绍java -jar命令的使用方式、原理及提供相关的代码演示和…

Docker清理

title: “Mysql安装” createTime: 2022-01-04T20:07:3108:00 updateTime: 2022-01-04T20:07:3108:00 draft: false author: “name” tags: [“mysql”] categories: [“docker”] description: “测试的” docker-mysql安装部署文档 文章目录 title: "Mysql安装" …

413 Request Entity Too Large问题

问题背景 在某系统中上传文件时&#xff0c;如果文件大小超过了一定范围就会爆 413 Request Entity Too Large 问题。 原因 在使用 nginx 反向代理后台服务时&#xff0c;如果请求体中过大&#xff0c;超过了默认的 1M 则会爆该错误。 解决方案 在 nginx 中&#xff0c;指…

信号类型(雷达)——脉冲雷达(四)

系列文章目录 《信号类型&#xff08;雷达通信&#xff09;》 《信号类型&#xff08;雷达&#xff09;——雷达波形认识&#xff08;一&#xff09;》 《信号类型&#xff08;雷达&#xff09;——连续波雷达&#xff08;二&#xff09;》 《信号类型&#xff08;雷达&…

【Java 进阶篇】深入理解 JDBC:Java 数据库连接详解

数据库是现代应用程序的核心组成部分之一。无论是 Web 应用、移动应用还是桌面应用&#xff0c;几乎都需要与数据库交互以存储和检索数据。Java 提供了一种强大的方式来实现与数据库的交互&#xff0c;即 JDBC&#xff08;Java 数据库连接&#xff09;。本文将深入探讨 JDBC 的…

力扣 -- 10. 正则表达式匹配

解题步骤&#xff1a; 参考代码&#xff1a; class Solution { public:bool isMatch(string s, string p) {int ms.size();int np.size();//处理后续映射关系s s;//处理后续映射关系p p;vector<vector<bool>> dp(m1,vector<bool>(n1));//初始化dp[0][0]true…

数学相关训练题解

训练链接 CF645F 题目链接 点击打开链接 题目解法 一眼莫反 推式子的步骤就不写了&#xff0c;反正也比较套路 这里只给出最后的式子为&#xff1a; A n s ∑ i 1 V ϕ ( i ) ∗ ( ∑ i ∣ j c n t j k ) Ans\sum\limits_{i1}^{V}\phi(i)*\binom{\sum\limits_{i\mid j}c…

C- 静态链接

静态链接意味着在编译时将所有库函数直接嵌入到最终的可执行文件中&#xff0c;而不是在运行时通过共享库来动态链接这些函数。静态链接的结果是一个更大的可执行文件&#xff0c;因为它包含了所有必要的代码&#xff0c;但它可以在没有外部依赖的情况下独立运行。 下面是一个…

【开发篇】十、Spring缓存:手机验证码的生成与校验

文章目录 1、缓存2、用HashMap模拟自定义缓存3、SpringBoot提供缓存的使用4、手机验证码案例完善 1、缓存 缓存是一种介于数据永久存储介质与数据应用之间的数据临时存储介质使用缓存可以有效的减少低速数据读取过程的次数&#xff08;例如磁盘IO&#xff09;&#xff0c;提高…

排序算法之【快速排序】

&#x1f4d9;作者简介&#xff1a; 清水加冰&#xff0c;目前大二在读&#xff0c;正在学习C/C、Python、操作系统、数据库等。 &#x1f4d8;相关专栏&#xff1a;C语言初阶、C语言进阶、C语言刷题训练营、数据结构刷题训练营、有感兴趣的可以看一看。 欢迎点赞 &#x1f44d…

Maven路上的疑难杂症

问题一&#xff1a;idea同步jar包提示“Could not find artifact org.springframework.security.oauth.boot:spring-security-oauth2-autoconfigure:pom:2.1.0.M4 in aliyunmaven”错误&#xff1b; 问题二&#xff1a;本地有maven依赖&#xff0c;idea却扫描不到&#xff1b;…

YOLOv8改进算法之添加CA注意力机制

1. CA注意力机制 CA&#xff08;Coordinate Attention&#xff09;注意力机制是一种用于加强深度学习模型对输入数据的空间结构理解的注意力机制。CA 注意力机制的核心思想是引入坐标信息&#xff0c;以便模型可以更好地理解不同位置之间的关系。如下图&#xff1a; 1. 输入特…

Error: Activity class {xxx.java} does not exist

git切换到不同的branch之后&#xff0c;报下面的错误&#xff1a; Error: Activity class {xxx.java} does not exist 解决方案&#xff1a; 首先clean 然后会删除build目录 然后点击&#xff1a;Invalidate Caches Android Studio重启&#xff0c;然后重新build即可。

数据链路层 MTU 对 IP 协议的影响

在介绍主要内容之前&#xff0c;我们先来了解一下数据链路层中的"以太网" 。 “以太网”不是一种具体的网络&#xff0c;而是一种技术标准&#xff1b;既包含了数据链路层的内容&#xff0c;也包含了一些物理层的内容。 下面我们再来了解一下以太网数据帧&#xff…

【Java 进阶篇】MySQL 事务详解

在数据库管理中&#xff0c;事务是一组SQL语句的执行单元&#xff0c;它们被视为一个整体。事务的主要目标是保持数据库的一致性和完整性&#xff0c;即要么所有SQL语句都成功执行&#xff0c;要么所有SQL语句都不执行。在MySQL中&#xff0c;事务起到了非常重要的作用&#xf…

Linux文件查找,别名,用户组综合练习

1.文件查看: 查看/etc/passwd文件的第5行 [rootserver ~]# head -5 /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologi…

【实践成果】Splunk 9.0 Configuration Change Tracking

Splunk 9.0 引入了新的功能&#xff0c;一个很重要的一个&#xff0c;就是跟踪conguration 文件的变化&#xff1a; 这个很重要的特性&#xff0c;在splunk 9.0 以后才引入&#xff0c;就看server.conf 配置中&#xff0c;9.0 以后的版本才有&#xff1a; server.conf - Splu…