一、软件
- (1)python-3.12.3-amd64.exe
- (2)pycharm-community-2024.2.2.exe
- (3)django==4.0.10
- (4)pycharm选择适合自己电脑版本即可
-
PyCharm激活馬:
KQ8KMJ77TY-eyJsaWNlbnNlSWQiOiJLUThLTUo3N1RZIiwibGljZW5zZWVOYW1lIjoiVW5pdmVyc2l0YXMgTmVnZXJpIE1hbGFuZyIsImxpY2Vuc2VlVHlwZSI6IkNMQVNTUk9PTSIsImFzc2lnbmVlTmFtZSI6IkpldOWFqOWutuahtiDorqTlh4blupflkI0iLCJhc3NpZ25lZUVtYWlsIjoibmtucWFyY214a0AxNjMuY29tIiwibGljZW5zZVJlc3RyaWN0aW9uIjoiRm9yIGVkdWNhdGlvbmFsIHVzZSBvbmx5IiwiY2hlY2tDb25jdXJyZW50VXNlIjpmYWxzZSwicHJvZHVjdHMiOlt7ImNvZGUiOiJHTyIsInBhaWRVcFRvIjoiMjAyNC0xMi0wNyIsImV4dGVuZGVkIjpmYWxzZX0seyJjb2RlIjoiUlMwIiwicGFpZFVwVG8iOiIyMDI0LTEyLTA3IiwiZXh0ZW5kZWQiOmZhbHNlfSx7ImNvZGUiOiJETSIsInBhaWRVcFRvIjoiMjAyNC0xMi0wNyIsImV4dGVuZGVkIjpmYWxzZX0seyJjb2RlIjoiQ0wiLCJwYWlkVXBUbyI6IjIwMjQtMTItMDciLCJleHRlbmRlZCI6ZmFsc2V9LHsiY29kZSI6IlJTVSIsInBhaWRVcFRvIjoiMjAyNC0xMi0wNyIsImV4dGVuZGVkIjpmYWxzZX0seyJjb2RlIjoiUlNDIiwicGFpZFVwVG8iOiIyMDI0LTEyLTA3IiwiZXh0ZW5kZWQiOnRydWV9LHsiY29kZSI6IlBDIiwicGFpZFVwVG8iOiIyMDI0LTEyLTA3IiwiZXh0ZW5kZWQiOmZhbHNlfSx7ImNvZGUiOiJEUyIsInBhaWRVcFRvIjoiMjAyNC0xMi0wNyIsImV4dGVuZGVkIjpmYWxzZX0seyJjb2RlIjoiUkQiLC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3.10.10-amd64.exe
2.如果Web开发,可以使用django(这只是其中一种框架),注意安装的django版本需要与对应的Python版本对应,我这里python-3.10.10对应的就是django==4.0.10
- pip install django==4.0.10
3.初始化项目
- ①先进入到自己的目录,比如cd D:\python
- ②django-admin startproject bysms
这里要注意,如果你电脑上同时安装了2个版本的python,那请都卸载了重新安装你自己想要的Python,且重新用命令“pip install django==版本号”安装django。
不然他们可能会一直出现如下错误
C:\Users\Jungle>django-admin startproject bysms Fatal error in launcher: Unable to create process using "C:\Users\Jungle\AppData\Local\Programs\Python\Python313\python.exe" "C:\Users\Jungle\AppData\Local\Programs\Python\Python310\Scripts\django-admin.exe" startproject bysms': ???????????
python manage.py runserver 0.0.0.0:80
接着访问
http://127.0.0.0:80/
如果访问不了,比如可能是端口被占用了,那我们就修改端口,修改如下
可以通过修改manage.py进行修改,只需要加三行:
from django.core.management.commands.runserver import Command as Runserver Runserver.default_addr = '127.0.0.1' # 修改默认地址 Runserver.default_port = '8080' # 修改默认端口
如果想用其他ip,需要再在如下settings.py文件添加ip
ALLOWED_HOSTS = ['192.168.2.11']
在用命令行执行如下命令
python manage.py runserver 0.0.0.0:8087
再次访问地址
http://192.168.2.11:8087/ python manage.py startapp sales
三、写代码
python manage.py startapp sales
from django.http import HttpResponse from select import select # Create your views here. def listorders(request): return HttpResponse("hello Djando!")
from django.contrib import admin from django.urls import path,include from sales.views import listorders urlpatterns = [path('admin/', admin.site.urls),path('sales/orders',listorders)]
访问http://192.168.2.11:8087/sales/orders/ 即可返回hello Djando!
但是,我们一般开发可能是分模块的,每个模块都需要在根目录下设置该模块的多个接口方法,就有点分工不对似的,根目录只需要知道模块名字,而具体方法由子模块来编写,所以我们改写如下
from django.contrib import admin from django.urls import path,include from sales.views import listorders urlpatterns = [path('admin/', admin.site.urls),path('sales/',include('sales.urls'))]
from django.urls import path from sales.views import listorders,listorders2,listorders3,listorders5,listorders6 urlpatterns = [path('orders/',listorders), path('orders2/',listorders2),path('orders3/',listorders3),path('orders6/',listorders6)]
四、数据库操作
pip install pymysql
在sales目录下的views.py下面添加访问数据库的方法,主要是方法listorders5和6
from itertools import count from django.shortcuts import render from django.http import HttpResponse from pymysql import from select import select # Create your views here. def listorders(request): return HttpResponse("hello Djando!") def listorders2(request): return HttpResponse("hello Djando2!") def listorders3(request): return HttpResponse("hello Djando3!") # MySQL插入 def listorders5(request):conn = connect(host='127.0.0.1',user= 'root',password='root',port=3306,db='testdb', charset='utf8')cs1=conn.cursor() count=cs1.execute('INSERT INTO student(name) VALUES(1)')# count2 = cs1.execute('select * from student') conn.commit()cs1.close()conn.close() return HttpResponse(count) # MySQL查询 def listorders6(request):conn = connect(host='127.0.0.1',user= 'root',password='root',port=3306,db='testdb',charset='utf8'`) cs1=conn.cursor() # count=cs1.execute('INSERT INTO student(name) VALUES(1)') count2 = cs1.execute('select * from student') conn.commit()cs1.close()conn.close() return HttpResponse(count2)
如果导入的from pymysql import * 显示红色波浪线,我们可以把鼠标放到红色波浪线上面根据pycharm的提示菜单安装插件即可
http://192.168.2.11:8087/sales/orders5/``http://192.168.2.11:8087/sales/orders6/
总结
- 最后希望你编程学习上不急不躁,按照计划有条不紊推进,把任何一件事做到极致,都是不容易的,加油,努力!相信自己!
文末福利
- 最后这里免费分享给大家一份Python全套学习资料,希望能帮到那些不满现状,想提升自己却又没有方向的朋友,也可以和我一起来学习交流呀。
包含编程资料、学习路线图、源代码、软件安装包等!【[这里]】领取!
- ① Python所有方向的学习路线图,清楚各个方向要学什么东西
- ② 100多节Python课程视频,涵盖必备基础、爬虫和数据分析
- ③ 100多个Python实战案例,学习不再是只会理论
- ④ 华为出品独家Python漫画教程,手机也能学习
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://sigusoft.com/goland/1521.html