一、创建project
先进入自己指定的目录
django-admin startproject demo_django
demo_django
- demo_django (配置文件)
- manage.py (管理Project)
- app(cmdb)
- models.py 数据库操作
- admin.py 配置Django自带的后台管理
- apps.py 当前app的配置
- tests.py 单元测试
- views.py 做业务处理...
>>>运行
cd demo_django
python3 manage.py runserver 127.0.0.1:8000
二、创建app
cd demo_django
python3 manage.py startapp cmdb
python3 manage.py startapp monitor
1、创建工程并运行
Django-admin manager.py startserver project_name
python manage.py runserver 127.0.0.1:8000
2、创建应用
python manage.py startapp app_name
3、修改工程配置文件,配置html文件的路径
修改:'DIRS': [os.path.join(BASE_DIR, 'templates')],其中BASE_DIR为工程目录的路径
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(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',
],
},
},
]
4、添加静态文件路径及修改匹配规则
修改settings.py文件,添加STATICFILES_DIRS,如下:
STATICFILES_DIRS = (
os.path.join(BASE_DIR,"/static/"),
)
修改settings.py文件,添加APPEND_SLASH,如下:
APPEND_SLASH=False
5、配置应用页面映射关系
修改工程下的utls.py文件
from cmdb import views #选择应用并导入业务处理模块
#url(r'^index/', views.index)指定
urlpatterns = [
# url(r'^admin/', admin.site.urls),
# url(r'^index/', admin.site.urls),
url(r'^index/', views.index),
]
6、修改应用业务处理模块,添加映射关系中的函数
from django.shortcuts import render
from django.shortcuts import HttpResponse
# Create your views here.
def index(resquest):
print("get>>>:",resquest.GET)
# return HttpResponse("OK")
return render(resquest,"index.html")
7、注册app
setttings.py文件的INSTALLED_APPS字段中添加"app01",指定选择某一个app进行创建数据库表
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app01',
]
注意:
(1)、django默认使用mysqldb模块创建表,py3不支持,修改为pymysql
修改project下的init.py,加入如下字段
import pymysql
pymysql.install_as_MySQLdb()
(2)、使用mysql数据库时需要先手动创建库,settings.py中修改数据库引擎,如下
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'app',
'USER':'root',
'PASSWORD':'python',
'HOST':'192.168.12.81',
'PORT':'3306',
}
}
8、创建表
(1)在app01的models.py中添加类,即表示要创建含有四个字段的名为“userinfo”的表
class UserInfo(models.Model):
uid = models.AutoField(primary_key=True)
username = models.CharField(max_length=32)
password = models.CharField(max_length=32)
age = models.IntegerField()
(2) 命令1:python manage.py makemigrations
Migrations for 'app01':
app01\migrations\0001_initial.py:
- Create model UserInfo
命令2:python manage.py migrate
Operations to perform:
Apply all migrations: admin, app01, auth, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying app01.0001_initial... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying sessions.0001_initial... OK
9、获取表中的数据
from app01 import models
server_list = models.UserInfo.objects.all() ##获取userinfo表的所有数据
for i in server_list:
print(i.uid,i.username,i.password,i.age)
修改前台页面-->修改映射-->修改视图-->添加中间页面
提交报错:URL doesn'tend in a slash and you have APPEND_SLASH set.
建议修改settings.py追加:APPEND_SLASH=False
或者指定映射的开始和结束:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^user$', views.user),
url(r'^add_user$', views.add_user),
url(r'^del_user$', views.del_user),
url(r'^edit_user$', views.edit_user),
url(r'^new_edit_user/(?P<nnid>\d+)$', views.new_edit_user),
]
Change your form to point to 127.0.0.1:8000/user/ (note the trailing slash), or set APPEND_SLASH=False in your Django settings.