使用PyCharm和Django搭建网站
就写个入门
😈一个搞事情的围笑
首先打开PyCharm,New Project,选择Django,然后改个名啥的,点Create

这里呢我取名叫MySite
然后PyCharm会开始下载Django的依赖,等就完事了。
然后就会看到酱婶的一个目录结构

settings是Django的配置文件,urls是Django的路由文件,wsgi是用来和uwsgi通讯的,template目录用于存放html模板。
然后开始Hello World
在应用目录下新建一个index.py
像这样

代码是这样的
from django.http.response import HttpResponse
def index(request):
return HttpResponse(b'Hello World')
然后修改urls.py文件
加入这些
from django.contrib import admin
from django.urls import path
from MySite import index
urlpatterns = [
path('admin/', admin.site.urls),
path('', index.index),
]
然后就可以运行了
点这个

控制台会出现

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

好了一个基本的Hello World完成了。
接下来是简单的模板使用
在template目录下建立index.html,随便写点什么
比如这样

修改index.py文件
from django.shortcuts import render_to_response
def index(request):
return render_to_response('index.html', {'title': 'This is Title', 'message': 'This is message'})
然后运行就是

好了你已经可以使用简单的模板了
学了简单的复杂的也要对吧
在Django的模板里面用双花括号来显示一个变量,这个变量来自于render函数传入的context参数,所以呢刚刚的title就对应context参数里面key为title的值
django的if模板长这样
{% if condition1 %}
你要展示的东西1
{% elif condition2 %}
你要展示的东西2
{% else %}
你要展示的东西3
{% endif %}
举个例子
# index.py
from django.shortcuts import render_to_response
from django.http.request import HttpRequest
def index(request=HttpRequest()):
txt = request.GET['txt']
context = {}
if txt == '1':
context['condition1'] = True
elif txt == '2':
context['condition2'] = True
return render_to_response('index.html', context)
访问http://127.0.0.1:8000/?txt=1

访问http://127.0.0.1:8000/?txt=2

就是这样一个意思
然后就一个for标签
{% for item in items %}
循环的东西里面可以用{{ item }}来获取每次迭代的项
{% endfor %}
继续举🌰
# index.py
from django.shortcuts import render_to_response
from django.http.request import HttpRequest
def index(request=HttpRequest()):
context = {}
context['items'] = ['小明', '小强', '小刚', '小红', '小青']
return render_to_response('index.html', context)
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Test</title>
</head>
<body>
<ul>
{% for item in items %}
<li>{{ item }}</li>
{% endfor %}
</ul>
</body>
</html>
运行一下

这就是一个很基础的For使用
往Django里面添加静态资源
在根目录底下新建一个static
目录,放入一些静态文件,比如图片、CSS文件什么的

在settings.py的末尾添加
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
加完就像这样

然后打开对应的地址比如http://127.0.0.1:8000/static/img.jpg,如果看到可以访问,说明静态文件配置成功了。

具体的文件放在上面目录,就得看自己怎么去规范了。
使用数据库
嘛,做这种动态网站总是要使用数据库嘛。Django使用Model类作为数据库操作的基类,使用ActiveRecord模式进行增删改查。大家都知道Python自带SQLite数据库,所以在这里就直接使用自带数据库,进行演示。
在应用目录下建models.py文件,文件名得是这个。
输入下面代码
from django.db import models
class User(models.Model):
username = models.TextField()
nickname = models.TextField()
Django常见的Field有下面几种
Field | 描述 |
---|---|
AutoField | 根据实际ID自动增长的IntegerField,通常不指定。 |
CharField(max_length=长度) | 文本域,需指定一个最大长度 |
TextField | 大文本 |
IntegerField | 整数 |
BooleanField | 布尔值True或者False |
其他的可以自己再去找,嘻嘻😄因为入门感觉用这些也差不多了。
在settings.py里面,INSTALLED_APPS加入自己的应用名,比如MySite

打开Tools菜单,选择Run manage.py Task。

输入
migrate
makemigrations MySite
migrate MySite
如果没错的话应该都是一路OK下来的

细心的你会发现项目目录里面多了一个db.sqlite3文件

如果装了插件然后打开这个数据库,会有这样一些表

其中
你的APP名_Model名
这种结构的表就是自动根据models里面的类生成的。
往数据库里面随便加点啥

修改index.py
from django.shortcuts import render_to_response
from django.http.request import HttpRequest
from .models import User
def index(request=HttpRequest()):
context = {}
context['items'] = User.objects.all()
return render_to_response('index.html', context)
修改index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Test</title>
</head>
<body>
<table border="1">
{% for item in items %}
<tr>
<td>{{ item.id }}</td>
<td>{{ item.username }}</td>
<td>{{ item.nickname }}</td>
</tr>
{% endfor %}
</table>
</body>
</html>
运行一下

酱基础的数据库操作就好辣。