我爱编程

Django|路由与模板|URLconf &Templates

2018-05-27  本文已影响0人  绍重先

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

"""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),
]

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("课程信息")
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

➜  proMana tree templates
templates
├── appMana
│   └── stuinfo.html
└── base.html

1 directory, 2 files
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',
            ],
        },
    },
]
➜  proMana tree static -d
static
├── css
├── fonts
└── js

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和一个目录。
实例模板:

<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
    <title>Bootstrap 101 Template</title>

    <!-- Bootstrap -->
    <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">

    <!-- HTML5 shim 和 Respond.js 是为了让 IE8 支持 HTML5 元素和媒体查询(media queries)功能 -->
    <!-- 警告:通过 file:// 协议(就是直接将 html 页面拖拽到浏览器中)访问页面时 Respond.js 不起作用 -->
    <!--[if lt IE 9]>
      <script src="https://cdn.bootcss.com/html5shiv/3.7.3/html5shiv.min.js"></script>
      <script src="https://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  <body>
    <h1>你好,世界!</h1>

    <!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery,所以必须放在前边) -->
    <script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
    <!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
    <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  </body>
</html>
上一篇 下一篇

猜你喜欢

热点阅读