码迷,mamicode.com
首页 > 其他好文 > 详细

Django 的简单使用

时间:2021-04-08 13:39:39      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:rom   业务逻辑   sed   admin   meta   validator   messages   pes   default   

app:

  migrations:数据库操作记录,只修改表结构记录

  admin:Djangot为我们提供的后台管理

  apps:表示配置当前app

  model:ORM,写指定的类  通过命令可以创建数据库结构

  tests:单元测试

  views:写相关所有业务逻辑代码

创建项目流程:

  1.模板的路径settings中templates

    

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,
            ],
        },
    },
]

 

  2.配置静态资源目录

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, static),
)

 

注释:第一种通过绝对路径打开html展示。

第二种通过render会去找到settings中templates中配置的dirs路径下的文件。直接就可以找到html文件

from django.shortcuts import render
from django.shortcuts import HttpResponse
from django.shortcuts import render
# Create your views here.
def home(request):
    return HttpResponse(<h1>CMDB二傻子</h1>)


def login(request):
    #方式一
    # f = open(‘templates/login.html‘,‘r‘,encoding=‘utf-8‘)
    # data=f.read()
    # f.close()
    # return HttpResponse(data)
    #方式二:
    return render(request,login.html)

"""day8 URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/3.1/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 path
from cmdb import views




urlpatterns = [
    path(admin/, admin.site.urls),
    path(login/, views.login),
]

 

"""
Django settings for day8 project.

Generated by ‘django-admin startproject‘ using Django 3.1.7.

For more information on this file, see
https://docs.djangoproject.com/en/3.1/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.1/ref/settings/
"""

from pathlib import Path
import os

# Build paths inside the project like this: BASE_DIR / ‘subdir‘.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = zs*%5_u-o*8t&)h02%-=po0pgh5ohm$_6^drdjb#or-%f)tjnw

# SECURITY WARNING: don‘t run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    django.contrib.admin,
    django.contrib.auth,
    django.contrib.contenttypes,
    django.contrib.sessions,
    django.contrib.messages,
    django.contrib.staticfiles,
]

MIDDLEWARE = [
    django.middleware.security.SecurityMiddleware,
    django.contrib.sessions.middleware.SessionMiddleware,
    django.middleware.common.CommonMiddleware,
    django.middleware.csrf.CsrfViewMiddleware,
    django.contrib.auth.middleware.AuthenticationMiddleware,
    django.contrib.messages.middleware.MessageMiddleware,
    django.middleware.clickjacking.XFrameOptionsMiddleware,
]

ROOT_URLCONF = day8.urls

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,
            ],
        },
    },
]

WSGI_APPLICATION = day8.wsgi.application


# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases

DATABASES = {
    default: {
        ENGINE: django.db.backends.sqlite3,
        NAME: BASE_DIR / db.sqlite3,
    }
}


# Password validation
# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        NAME: django.contrib.auth.password_validation.UserAttributeSimilarityValidator,
    },
    {
        NAME: django.contrib.auth.password_validation.MinimumLengthValidator,
    },
    {
        NAME: django.contrib.auth.password_validation.CommonPasswordValidator,
    },
    {
        NAME: django.contrib.auth.password_validation.NumericPasswordValidator,
    },
]


# Internationalization
# https://docs.djangoproject.com/en/3.1/topics/i18n/

LANGUAGE_CODE = en-us

TIME_ZONE = UTC

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.1/howto/static-files/

STATIC_URL = /static/

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        label{
            width: 80px;
            text-align: right;
            display: inline-block;
        }
    </style>
</head>
<body>
    <form action="/login" method="post">
        <p>
            <label for="username">用户名:</label>
            <input id="username" type=‘text‘/>

        </p>
        <p>
            <label for="password">密码:</label>
            <input  id="password" type=‘text‘/>
            <input type="submit" value="提交">
        </p>
    </form>
</body>
</html>

 

 

 

 

 

form

Django 的简单使用

标签:rom   业务逻辑   sed   admin   meta   validator   messages   pes   default   

原文地址:https://www.cnblogs.com/anhao-world/p/14514389.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!