django介绍及基本使用


一、python主流web框架

ps:作为小白的你,初学阶段不要混着学,很容易走火入魔,先学Django就好了。

1.django
	大而全 自身自带的功能组件非常的多 类似于航空母舰 		
2.flask
	小而精 自身自带的功能组件非常的少 类似于游骑兵
 	几乎所有的功能都需要依赖于第三方模块 
3.tornado
	异步非阻塞 速度极快效率极高甚至可以充当游戏服务端
ps:sanic、fastapi...

二、django简介

1.版本问题

  • django1.X:同步 1.11
  • django2.X:同步 2.2
  • django3.X:支持异步 3.2
  • django4.X:支持异步 4.2

ps:版本之间的差异其实不大 主要是添加了额外的功能

image

image

通常来说我们使用的都是上图中的LTS版本的Django,一些老项目会使用1.11版本,我们学习的是2.2.22版本。

2.运行django注意事项

1.django项目中所有的文件名目录名不要出现中文
2.计算机名称尽量也不要出现中文
3.一个pycharm尽量就是一个完整的项目(不要嵌套 不要叠加)
4.不同版本的python解释器与不同版本的django可能会出现小问题(这里依旧是百度解决问题,身边有大佬也可以求助大佬)

三、django基本使用

1.下载模块

pip方式下载

	pip3 install django 				默认最新版
 	pip3 install django==版本号		  指定版本
    pip3 install django==2.2.22

image

pycharm中下载

依旧是点击左上角的File,然后选择settings

image

在出现的弹窗中点击左边的project选项然后选择interpreter

image

在右边出现如图窗口后,在右侧窗口中双击,然后再在出现的窗口中搜索Django

image

待加载完成后我们选择Django模块,然后在右侧下方勾选Specify version,然后再在这里选择需要安装的版本

image

再点击install即可

image

特殊情况说明

当我们在pycharm中下载的时候会出现提示,告诉我们在安装的时候出现了关联安装(即顺带安装了一些关联的模块)。

但是在cmd中用pip下载模块,会自动解决依赖问题(不会把关联需要用到的模块一起下了)。

2.验证

cmd窗口直接输入django-admin有一长串结果展示表明成功(需提前配置解释器环境变量)

django-admin

3.常见命令

1、创建django项目

django-admin startproject 项目名(如:mysite)

2、启动django项目

ps:需要先进入项目的所在目录才能使用代码启动Django服务

    cd 项目名(如:mysite)
    python3 manage.py runserver IP:PORT 

IP:PORT可以不写 默认在本地8000端口起服务
如果想使用本地地址,使用的ip是127.0.0.1
如果想要使用本机ip,需要在settings.py文件中修改配置

ALLOWED_HOSTS = []

默认这个列表是空的(也就是只允许本机访问)

ALLOWED_HOSTS = []

修改这个列表:

ALLOWED_HOSTS = ['*'] # 允许所有的主机

当然也可以指定可访问主机的ip:

ALLOWED_HOSTS = ['198.211.99.20', 'localhost', '127.0.0.1']

3、创建app应用

python3 manage.py startapp 应用名(jason01)

4.pycharm自动创建django项目

  • templates文件夹是用于存放html文件的。

  • pycharm会自动创建templates文件夹,但是配置文件中可能会报错,需要我们手动修改。在cmd中需要自己创建,并打开文件添加配置信息

这里是我们在pycharm中建的Django项目,项目内部的settings.py文件会有错误,需要我们手动修改

image

把中括号以及内部的信息替换成下列代码即可(cmd中创建的Django项目,如果也用templates当作文件夹的名称,也是添加一样的代码)。

[os.path.join(BASE_DIR,'templates'),]


posted @   致丶幻  阅读(35)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
  1. 1 So Far Away (Acoustic) Adam Christopher
  2. 2 雪 Distance Capper&罗言RollFlash
  3. 3 CollapsingWorld
  4. 4 Call You Tonight Johnta Austin
Call You Tonight - Johnta Austin
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.

Mmmm...

Mmmmm...

Mmmmmm....

The stars must be alighted tonight

I believe this has to have a meaning

Lightning had to strike tonight

Cause the two of us are finally meeting

In this place at this time

And I feel safe when I look in your eyes

I feel like I know you from another life

And it makes me wish I wasn‘t so pressed by time

I can‘t catch my breathe

Cause you take it away

The best writer in town

Could not find words to say

How there are so many things

I wanna get to know

I wish that I could stay

But I gotta go

So I‘mma call you tonight

I will baby

Just as soon as I get time alone

I‘mma call you tonight

I will baby

Just as soon as I get home

That‘s not the way it goes in life

You get busy when you just don‘t wanna

There‘s never enough time day or night

You have to make it so baby I‘m gonna

Make a way to connect

Cause your face is one I can‘t forget

I feel like I know you from another life

And it makes me wish I wasn‘t so pressed for time

I can‘t catch my breathe

I can‘t catch my breathe

Cause you take it away

The best writer in town

Could not find words to say

How there are so many things

I wanna get to know

I wish that I could stay

But I gotta go

So I‘mma call you tonight

I will baby

Just as soon as I get time alone

I‘mma call you tonight

I will baby

Just as soon as I get home

Destiny

I believe in it

Meant to be

Don‘t you see it‘s possible that this kind of magic

Anything can happen

And if you wanna know

Then you‘ll stay by the phone

Ohhhhhh

I can‘t catch my breathe

Cause you take it away

The best writer in town

Could not find words to say

How there are so many things

I wanna get to know

I wish that I could stay

But I gotta go

So I‘mma call you tonight

I will baby

Just as soon as I get time alone

I‘mma call you tonight

I will baby

Just as soon as I get home

I‘mma call you tonight

I will baby

Just as soon as I get home

点击右上角即可分享
微信分享提示