Django|路由与模板|URLconf &Templates

python:3.6.5
django:2.0.5

  • 文件目录
➜  proMana tree -d
.
├── appMana
│   ├── migrations
│   │   └── __pycache__
│   └── __pycache__
├── proMana
│   └── __pycache__
├── static
│   ├── css
│   ├── fonts
│   └── js
└── templates
    └── appMana


URL Configuration

  • 设置project路由
    proMana/urls
"""proMana URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/2.0/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import include,path

urlpatterns = [
    path('',include('appMana.urls')),
    path('admin/', admin.site.urls),
]

  • 编写view文件appMana/views.py
from django.shortcuts import render

from django.http import HttpResponse



# Create your views here.
def index(request):
    return HttpResponse("信息管理系统")

def stuinfo(request):
    #return HttpResponse("学生信息")
    return render(request,'appMana/stuinfo.html')
def scinfo(request):
    return HttpResponse("选课信息")

def courseinfo(request):
    return HttpResponse("课程信息")
  • 设置app内页面路由
    appMana/urls
from django.urls import path

from . import views

urlpatterns = [
    path('', views.index, name='index'),
    path('index/',views.index),
    path('stuinfo/',views.stuinfo),
    path('scinfo/',views.scinfo),
    path('courseinfo/',views.courseinfo)
]


在模板中使用Bootstrap

  • 在项目根目录下建立文件夹templates,并创建所需html
➜  proMana tree templates
templates
├── appMana
│   └── stuinfo.html
└── base.html

1 directory, 2 files
  • 修改proMana/settings.py
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',
            ],
        },
    },
]
  • 在根目录下建立静态资源文件夹static
➜  proMana tree static -d
static
├── css
├── fonts
└── js
  • 在Django中引用Bootstrap模版

1、首先下载bootsrtap代码(http://v3.bootcss.com/getting-started/#download),并将下载后的文件放在project下新创建的static目录下。下载jquery(https://jquery.com/download/)放在static/js/目录下。

2、下载合适的bootstrap模版样式(http://v3.bootcss.com/getting-started/),下载的文件包含一个html和一个目录。
实例模板:



  
    
    
    
    
    Bootstrap 101 Template

    
    

    
    
    
  
  
    

你好,世界!

你可能感兴趣的:(Django|路由与模板|URLconf &Templates)