如何从0开始创建一个python+Pdm+Django项目

news/2024/9/25 15:27:59/

1、安装pdm

 pip3.10 install pdm

或者

 pip install pdm

2、初始化python项目的配置和环境

pdm init

3、在项目中添加 Django 框架

pdm add django

4、当前目录创建一个叫做Tesla的Django项目

 pdm run django-admin startproject Tesla ./

如图

5、编辑pyproject.toml文件,添加下面内容:

makeM = 'python manage.py makemigrations'M = 'python manage.py migrate'R = 'python manage.py runserver' # --noreloadT = 'python manage.py test'createadmin = '''python manage.py shell -c "from django.contrib.auth.models import User; User.objects.create_superuser('admin', 'admin@example.com', 'adminpass')"'''

6、初始化django

pdm run init

7、启动django

 pdm run R 

8、测试django

 pdm run T

9、项目前台是:http://127.0.0.1:8000/ 项目后台是:http://127.0.0.1:8000/admin/ 账号:admin 密码:adminpass

其中账号和密码就在刚刚编辑的pyproject.toml文件里面createadmin这里

10、在当前项目路径下面,创建一个叫做xingfu的app,终端执行命令

python manage.py startapp xingfu

11、编辑xingfu文件夹下面的views文件,视图函数是用于接受用户HTTP请求,并生成对应HTTP响应

views.pyfrom django.http import HttpResponse
from django.shortcuts import render# Create your views here.
def hello(request):html = "hello world"return HttpResponse(html)

12、创建路由,router (路由):将URL和视图,建立联系,编辑Tesla下面的urls.py

urls.pyfrom django.urls import pathfrom .views import hello
urlpatterns = [path('hello123', hello),
]

13、重启程序,终端执行命令:

 pdm run R

14、如果想要再次写一个函数,编辑views.py

from django.http import HttpResponse,HttpRequest
from django.shortcuts import render# Create your views here.
def hello(request):html = "hello world"return HttpResponse(html)def echo(request:HttpRequest):html = f"""{request.method} {request.path} {request.GET}{request.headers}{request.body}"""return HttpResponse(html)

15、在Tesla下面的urls.py加上这个路由

16、访问:http://127.0.0.1:8000/echo/,如图

学到这里你就可以开始对你的django框架进行丰富,扩展,愉快的玩耍啦


http://www.ppmy.cn/news/1450287.html

相关文章

LPO vs CPO:谁是数据中心光互连的领跑者?

在不断扩大的数据中心领域,速度和效率至关重要,光互连的主导地位之争已经到了关键时刻。激光相控振荡器(LPO)和相干相控振荡器(CPO)这两项强大的技术已成为彻底改变数据中心光互连竞赛的主要竞争者。本文深…

C语言 | Leetcode C语言题解之第64题最小路径和

题目&#xff1a; 题解&#xff1a; int minPathSum(int** grid, int gridSize, int* gridColSize) {int rows gridSize, columns gridColSize[0];if (rows 0 || columns 0) {return 0;}int dp[rows][columns];dp[0][0] grid[0][0];for (int i 1; i < rows; i) {dp[i…

Spark-机器学习(8)分类学习之随机森林

在之前的文章中&#xff0c;我们学习了分类学习之支持向量机决策树支持向量机&#xff0c;并带来简单案例&#xff0c;学习用法。想了解的朋友可以查看这篇文章。同时&#xff0c;希望我的文章能帮助到你&#xff0c;如果觉得我的文章写的不错&#xff0c;请留下你宝贵的点赞&a…

verilog分析task的接口设计,证明这种写法:assign {a,b,c,d} = links;

verilog分析task的接口设计&#xff0c;证明这种写法&#xff1a;assign {a,b,c,d} links; 1&#xff0c;task在状态机中的使用好处&#xff1a;2&#xff0c;RTL设计3&#xff0c;测试testbench4&#xff0c;波形分析&#xff0c;正确&#xff01; 参考文献&#xff1a; 1&am…

区块链 | IPFS:CID

&#x1f98a;原文&#xff1a;Anatomy of a CID &#x1f98a;写在前面&#xff1a;本文属于搬运博客&#xff0c;自己留存学习。 1 CID 在分布式网络中与其他节点交换数据时&#xff0c;我们依赖于内容寻址&#xff08;而不是中心化网络的位置寻址&#xff09;来安全地定位…

STM32——点亮第一个LED灯

代码示例&#xff1a; #include "stm32f10x.h" // Device headerint main() {RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//开启时钟GPIO_InitTypeDef GPIO_InitStructure;GPIO_InitStructure.GPIO_Mode GPIO_Mode_Out_PP;GPIO_InitSt…

【设计模式】17、iterator 迭代器模式

文章目录 十七、iterator 迭代器模式17.1 user_slice17.1.1 collection_test.go17.1.2 collection.go17.1.3 iterator.go17.1.4 user.go 十七、iterator 迭代器模式 https://refactoringguru.cn/design-patterns/iterator 为了集合数据的安全性, 或方便迭代, 可以用迭代器接口…

2023下半年软件设计师上午题——冒泡排序

快速排除法&#xff0c;根据冒泡排序特性&#xff0c;每一趟排序都会确实最大/最小值&#xff0c;故升序两趟后&#xff0c;最后两个元素应该是已经排序好的第二大&#xff0c;和最大的元素&#xff0c;所以排除B,D&#xff0c;再因为每次排序都会两两交换&#xff0c;所以排除…