python django mvc

MVC框架:

1、URL映射到方法

root@zz mysite# cat urls.py

"""mysite URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:

https://docs.djangoproject.com/en/1.11/topics/http/urls/

Examples:

Function views

  1. Add an import: from my_app import views

  2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')

Class-based views

  1. Add an import: from other_app.views import Home

  2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')

Including another URLconf

  1. Import the include() function: from django.conf.urls import url, include

  2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))

"""

from django.conf.urls import url

from django.contrib import admin

from polls import views as polls_view

urlpatterns = [

url(r'^admin/', admin.site.urls),

url(r'^cmdb/add/$',polls_view.cmdb_add ),

url(r'runoob/', polls_view.runoob)

]

2、具体方法

root@zz polls# cat views.py

-*- coding: utf-8 -*-

from future import unicode_literals

from django.shortcuts import render

from django.http import HttpResponse

Create your views here.

from django.shortcuts import render,render_to_response,redirect

from django.http import HttpResponse, HttpResponseNotFound

import datetime

from django.views.decorators.http import require_http_methods,require_GET

from django.http import HttpResponseRedirect

from django.shortcuts import render

import os

import os

import json

from django.template import loader

Create your views here.

import os

os.environ'DJANGO_SETTINGS_MODULE' = 'mysite.settings'

from django.shortcuts import render

def runoob(request):

context = {}

context'hello' = 'Hello World!'

return render(request, 'runoob.html', context)

3、

root@zz mysite# cat polls/templates/runoob.html

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>测试</title>

</head>

<body>

<h1>{{ hello }}</h1>

</body>

</html>

相关推荐
渣波4 小时前
拒绝 SQL 焦虑!手把手带你用 NestJS + Prisma + DTO 写出“防弹”级后端代码
javascript·数据库·后端
兵慌码乱14 小时前
基于Python+PyQt5+SQLite的药房管理系统实现:事务一致性与界面解耦全流程解析
python·sqlite·信号与槽·pyqt5·数据库设计·桌面应用开发·事务处理
倔强的石头_1 天前
KingbaseES 新版MySQL 兼容版体验:旧版迁移 + 功能实测
数据库
倔强的石头_4 天前
《Kingbase护城河》——数据库存储空间全景探测与精细化瘦身实战
数据库
冬奇Lab5 天前
每日一个开源项目(第134篇):Zvec - 阿里开源的嵌入式向量数据库,向量搜索界的 SQLite
数据库·人工智能·llm
ClouGence5 天前
Oracle CDC 架构优化:从主库直连到 DataGuard 备库同步
数据库·后端·oracle
无响应de神5 天前
三、用户与权限管理
数据库·mysql
麦聪聊数据6 天前
数据服务化时代:企业数据能力输出的核心路径
数据库
shushangyun_6 天前
2026年快消品B2B系统推荐:支持终端门店订货、促销政策自动化的工具?
java·运维·网络·数据库·人工智能·spring·自动化
DARLING Zero two♡6 天前
【MySQL数据库】数据类型与表约束
数据库·mysql