django基础-命令&项目构建
命令行工具
django admin
常用子命令
startproject            创建一个项目
startapp                创建一个app
runserver               运行开发服务器
shell                   进入django shell
dbshell
check
flush
compilemessages
makemessages
makemigrations
migrate
showmigrations
sqlflush
sqlmigrate
dumpdata
loaddata
diffsettings
django help 命令         查看命令的功能
例子
django-admin.py startproject hello_django    #创建工程
#进入hello_django目录后
django-admin.py startapp hello               #创建app
manager.py是在hello_django工程下生成的工具,除了django-admin.py的命令外,也有一些特有的命令,是对manager.py的简单包装
createsuperuser
changepassword
clearsessions
...
工程的结构
hello_django/
├── db.sqlite3
├── hello                #app
│   ├── admin.py         #后台管理配置
│   ├── apps.py          #app配置信息
│   ├── __init__.py
│   ├── migrations       #数据库同步脚本
│   │   └── __init__.py
│   ├── models.py        #模型配置
│   ├── tests.py         #单元测试
│   └── views.py         #业务代码
├── hello_django
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── settings.py        #配置文件
│   ├── settings.pyc
│   ├── urls.py            #url映射用
│   ├── urls.pyc
│   ├── wsgi.py            #部署时配置协议
│   └── wsgi.pyc
└── manage.py
使用manager.py runserver运行起服务器
访问localhost:8000能获取到提示信息
访问localhost:8000/admin这个管理后台,但是登录之后报错,因为还没有设置管理后台
#数据库修改
manage.py makemigrations
#同步
manage.py migrate
#需要先创建个超级管理员
manage.py createsuperuser
创建之后,可以访问管理后台
修改某一账号的密码
manage.py changepassword sean
修改之后再进行runserver运行
在特定端口执行
manage.py runserver 端口号
展示用户列表
- viers.py定义函数
- 定义一个模板并引入静态文件
- urls.py中定义url地址
- 启动服务
- 把用户数据查询出来渲染到页面
views.py中定义函数
from django.shortcuts import render
def hello(request):
    return render(request,'table.html')
在hello中创建static和tmplates
css和js放在static中,模板放在templates中,这是默认的配置;
工程中urls.py定义地址,首先引入app的views.py,然后在urls.py中配置
from django.conf.urls import url
from django.contrib import admin
from hello import views
urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^hello/$', views.hello, name='hello'),
]
在工程的settings.py中INSTALLED_APPS中加入APP
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'hello',
]
修改后自动加载代码的
运行后发现前端效果没有生效
模板功能
table模板放在了项目里面,需要设置模板加载静态文件的路径
替换静态文件导入位置,如下
{% load staticfiles %}
<!DOCTYPE html>
<html>
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
   <meta name="viewport" content="width=device-width, initial-scale=1.0" />
   <title>Bootstrap 实例 - 条纹表格</title>
   <link href="{% static 'css/bootstrap.min.css' %}" rel="stylesheet">
   <script src="{% static 'js/jquery.min.js' %}"></script>
   <script src="{% static 'js/bootstrap.min.js' %}"></script>
</head>
<body>
替换talbe内容为用户列表
<table class="table table-striped">
   <caption>用户列表</caption>
   <thead>
      <tr>
         <th>用户名</th>
         <th>密码</th>
         <th>first name</th>
         <th>last name</th>
         <th>email</th>
      </tr>
   </thead>
   <tbody>
    {% for user in user_list %}
      <tr>
         <td>{{ user.username }}</td>
         <td>{{ user.password }}</td>
         <td>{{ user.first_name }}</td>
         <td>{{ user.lastname }}</td>
         <td>{{ user.email }}</td>
      </tr>
   {% endfor %}
   </tbody>
</table>
hello中的views.py修改
from django.shortcuts import render
from django.contrib.auth.models import User
def hello(request):
    user_list = User.objects.all()
    return render(request,'table.html',{'user_list':user_list})
现在只有一条记录,可以去管理后台创建,adduser创建
静态文件和模板位置修改
现在静态文件和模板是放在app下面的static和templates
app下面连个目录拷贝到根目录下,在settings.py中TEMPLATES把模板的位置添加到DIRS中
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ['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',
            ],
        },
    },
]
静态文件配置settings.py中加入静态文件地址
STATICFILES_DIRS=(
    os.path.join(BASE_DIR,'static'),
)
url
url现在定义在工程的url.py,如何设置每一个app一个urls.py
工程下urls.py引入app的ruls.py
from django.conf.urls import url,include
from django.contrib import admin
urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^', include('hello.urls')),
]
app中建立自己的urls.py
from django.conf.urls import url
from django.contrib import admin
from hello import views
urlpatterns = [
    url(r'^hello/$', views.hello),
]
在urls.py中,为什么一个用include一个用admin.site.urls的方式引入呢
模板的渲染过程
<!DOCTYPE html>
<html>
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
   <meta name="viewport" content="width=device-width, initial-scale=1.0" />
   <title>Bootstrap 实例 - 条纹表格</title>
   <link href="/static/css/bootstrap.min.css" rel="stylesheet">
   <script src="/static/js/jquery.min.js"></script>
   <script src="/static/js/bootstrap.min.js"></script>
</head>
<body>
<table class="table table-striped">
   <caption>用户列表</caption>
   <thead>
      <tr>
         <th>用户名</th>
         <th>密码</th>
         <th>first name</th>
         <th>last name</th>
         <th>email</th>
      </tr>
   </thead>
   <tbody>
      <tr>
         <td>sean</td>
         <td>pbkdf2_sha256$30000$50L092ZVd78E$Xmh7WNP4PyorWYRP5HZcmQBjoMnDhywN5mroNTmPF4c=</td>
         <td></td>
         <td></td>
         <td>sean@qunar.com</td>
      </tr>
   
      <tr>
         <td>jane</td>
         <td>pbkdf2_sha256$30000$ccY7Snhx9GXx$2A0NBzszo/fLISg8QnMTGL/eM9beAoW91JnBFQ6fav4=</td>
         <td>jane-first</td>
         <td></td>
         <td>jane@qunar.com</td>
      </tr>
   
   </tbody>
</table>
</body>
 
                     
                    
                 
                    
                
 
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号