10-11 view主要返回、admin

view主要返回

之前在view中用到的返回的render只是view视图返回的一种

render通过html模板语言渲染出界面返回给用户

render

通过模板语言进行渲染然后返还给用户界面

HttpResponse

def test(request):

	return HttpResponse('This is Httpresponse')

urls中配置的路由是test/

HTTPResponse后面写什么内容,就会返还什么内容

一般比较常用在jquery这种js请求

举个json的例子
import json
def test(request):
	dic = {'a':1,'b':2}
	dic_json = json.dumps(dic)
	return HttpResponse(dic_json)

redirect

urls中加入这行

 url('redir/', views.redir),

定义redir

def redir(request):
	print ("This is redirect ")
	return redirect('/test/')

访问 127.0.0.1:8000/redir/

我们发现他执行完python代码后,跳转到了定义的test路径下,实现了重定向功能

admin

admin是Django提供的管理平台

1、创建admin账号

在中终端中使用manager

python manage.py createsuperuser

输入用户名,密码即可创建完成

访问127.0.0.1:8000/admin/

app目录下有个admin.py文件,我们可以用这个文件进行增加数据

from django.contrib import admin
from Django111 import models
admin.site.register(models.UserInfo)
admin.site.register(models.School)

多app

1、在myapp中创建models

from django.db import models

class MYAPP(models.Model):
	name = models.CharField(max_length=32,blank=True,verbose_name='名称')
	def __str__(self):
		return self.name

2、将myapp注册到setting中

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

3、配置admin

from django.contrib import admin
from Django111 import models
admin.site.register(models.UserInfo)
admin.site.register(models.School)

from myapp import  models
admin.site.register(models.MYAPP)

admin文件可以放在任意app中

重新访问

在admin中可以比较方便进行数据的关联删除

转载于:https://my.oschina.net/u/4030294/blog/2997480

vue-element-admin 中,可以通过配置路由 meta 中的 keepAlive 属性来进行缓存。但是对于三级路由缓存,需要将他们作为二级路由的子路由进行配置。 具体配置方式如下: 1. 在 router/index.js 中,将三级路由定义为二级路由的子路由,例如: ```javascript { path: '/system', component: Layout, redirect: '/system/menu', name: 'System', meta: { title: '系统管理', icon: 'example' }, children: [ { path: 'menu', name: 'Menu', component: () => import('@/views/system/menu'), meta: { title: '菜单管理', icon: 'table', keepAlive: true } }, { path: 'role', name: 'Role', component: () => import('@/views/system/role'), meta: { title: '角色管理', icon: 'tree', keepAlive: true } }, { path: 'user', name: 'User', component: () => import('@/views/system/user'), meta: { title: '用户管理', icon: 'user', keepAlive: true } }, { path: ':id', // 三级路由 name: 'EditUser', component: () => import('@/views/system/edit-user'), meta: { title: '编辑用户', icon: 'user', hidden: true } // 隐藏该路由 }, ] }, ``` 2. 在 src/layout/components/MultiTab下的index.vue中,监听路由变化,根据当前路由中的 fullPath 进行判断,如果是三级路由,则将 fullPath 的前缀作为 parentPath 保存下来,用于找到他的二级父路由,从而正确进行缓存。 ```javascript computed: { // 通过 fullPath 匹配出二级父路由名称 parentPath() { const { fullPath } = this.$route; const matched = this.$route.matched; if (matched && matched.length > 2) { const parentPath = `/${matched[1].path}`; return fullPath.replace(parentPath, '').split('/')[1]; } return ''; }, // 是否需要缓存 needKeepAlive() { const { meta } = this.$route; if (meta && (typeof meta.keepAlive !== 'undefined')) { return meta.keepAlive; } return false; } }, watch: { '$route': function(newVal, oldVal) { // 监听路由变化,判断是否需要缓存 if (this.needKeepAlive) { const fullPath = newVal.fullPath; if (fullPath !== oldVal.fullPath) { const parentPath = this.parentPath; if (parentPath) { this.cacheList.push(`${parentPath}/${fullPath}`); } else { this.cacheList.push(fullPath); } } this.$forceUpdate(); } } } ``` 3. 在 src/layout/components/MultiTab下的tab-list.vue中,根据传入的 cacheList 进行渲染路由缓存。 ```javascript <keep-alive> <router-view v-if="$route.meta.keepAlive" :key="$route.path" /> </keep-alive> <component v-else :is="currentRoute.component" :key="currentRoute.path" /> ... computed: { // 用于 keep-alive 的路由列表 keepAliaveList() { if (this.cacheList.length) { const keepAliveList = this.cacheList.map(item => { const matched = item.split('/'); if (matched.length > 2) { // 当前路由是三级路由,需要找到他的二级父路由 const parentPath = `/${matched[1]}`; const parentRoute = this.getRouteObjByName(parentPath); if (parentRoute) { return { ...this.getRouteObjByName(item), parent: parentRoute }; } } else { // 当前路由是二级路由,直接返回 return this.getRouteObjByName(item); } }).filter(item => item); return keepAliveList; } return []; } }, methods: { // 根据路由名称获取路由对象 getRouteObjByName(name) { const routes = this.$router.options.routes; const obj = routes.find(route => route.path === name); return obj; }, // 关闭所有缓存的标签页 closeAllTags() { this.cacheList = []; this.$store.dispatch('multiTab/resetTabs'); }, ... } ``` 通过这些配置,即可实现 vue-element-admin 的三级路由缓存功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值