1.创建项目:web和应用 WebApp
django-admin startproject web
1.1切换到项目目录:
cd web/
1.2在项目中创建一个mytest应用
python3.6 manage.py startapp webapp
2.执行数据库连接配置,网站配置:
import pymysql
pymysql.install_as_MySQLdb()
2.2编辑web/web/settings.py文件,配置数据库连接
#配置自己的服务器ip地址:
ALLOWED_HOSTS = ['10.10.5.156']
2.3添加自己应用:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'WebApp', #增加至这个位置
]
2.4配置模板路径信息:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR / 'templates'], #增加这里
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
2.5#数据库的配置连接:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'HOST': '10.10.5.156', # 数据库主机
'PORT': 3306, # 数据库端口
'USER': 'mydba', # 数据库用户名
'PASSWORD': '123456', # 数据库用户密码
'NAME': 'pydb' # 数据库名字
}
}
3.定义Model类:
3.1编辑web/webapp/models.py
vim /root/web/webapp/models.py
from django.db import models
# Create your models here.
class Users(models.Model):
name = models.CharField(max_length=32)
age = models.IntegerField(default=20)
phone = models.CharField(max_length=16)
def __str__(self):
return self.name+':'+self.phone+':'+str(self.age)
class Meta:
db_table = 'py_users' #指定表名
3.2测试Model类的使用:
在项目根目录下执行命令:python3.6 manage.py shell
[root@localhost web]# pwd
/root/web
[root@localhost web]# python3.6 manage.py shell
Python 3.6.1 (default, Mar 2 2022, 11:00:37)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-23)] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from webapp.models import Users
>>> Users.objects.all()
<QuerySet [<Users: two:13137876574:15>]>
>>> s = Users.objects.get(id=1)
>>> s.id
1
>>>
4.实现web端访问:
4.1编写项目主路由urls配置,配置对webapp应用路由的访问连接配置
vim /root/web/web/urls.py
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('webapp/', include('webapp.urls')),
]
4.2配置当前应用webapp的路由配置
#在webapp应用目录下创建一个路由文件urls.py文件,注意此文件编码为utf-8(建议复制一个)
#编辑应用中的路由配置文件:web/webapp/urls.py,内容如下:
[root@localhost webapp]# vim urls.py
from django.urls import path
from . import views
urlpatterns = [
path('',views.index,name='index' ),
]
4.3编辑视图文件:web/webapp/views.py,内容如下:
from django.http import HttpResponse
# Create your views here.
from webapp.models import Users
def index(request):
try:
s = Users.objects.get(id=1)
return HttpResponse(s)
except :
return HttpResponse('库中没有对应信息!')
4.4测试:
#项目根目录下运行:python3.6 manage.py runserver 0:8000命令,开启服务:
[root@localhost web]# python3.6 manage.py runserver 0:8000
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
March 02, 2022 - 04:24:52
Django version 3.2.12, using settings 'web.settings'
Starting development server at http://0:8000/
Quit the server with CONTROL-C.