django render没办法传到前端

django render没办法传到前端

大佬们,我这个代码的render context为什么传不到我的模板页

这是我的视图

def news_all(request):

context = {'hello': 'Hello World!'}

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

这是我的base.html

{% load static %}

<!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 rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">

<!-- HTML5 shim 和 Respond.js 是为了让 IE8 支持 HTML5 元素和媒体查询(media queries)功能 -->

<!-- 警告:通过 file:// 协议(就是直接将 html 页面拖拽到浏览器中)访问页面时 Respond.js 不起作用 -->

<!--[if lt IE 9]>

<script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>

<script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>

<![endif]-->

</head>

<body>

<body>

<nav class="navbar navbar-default">

<div class="container-fluid">

<!-- Brand and toggle get grouped for better mobile display -->

<div class="navbar-header">

<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#my-nav" aria-expanded="false">

<span class="sr-only">切换导航条</span>

<span class="icon-bar"></span>

<span class="icon-bar"></span>

<span class="icon-bar"></span>

</button>

<a class="navbar-brand" href="/index/">新闻网</a>

</div>

<!-- Collect the nav links, forms, and other content for toggling -->

<div class="collapse navbar-collapse" id="my-nav">

<ul class="nav navbar-nav">

<li class="active"><a href="/index/">主页</a></li>

<li class="active"><a href="/military/">军事</a></li>

<li class="active"><a href="/game/">游戏</a></li>

<li class="active"><a href="/entertainment/">娱乐</a></li>

<li class="active"><a href="/International/">国际</a></li>

</ul>

<ul class="nav navbar-nav navbar-right">

{% if request.session.is_login and request.session.user_role == '0' %}

<li><a href="/index/">当前在线:{{ request.session.user_name }}</a></li>

<li><a href="/register/">注册</a></li>

<li><a href="/newsadmin/">控制台</a></li>

<li><a href="/logout/">登出</a></li>

{% elif request.session.is_login and request.session.user_role == '1' %}

<li><a href="/index/">当前在线:{{ request.session.user_name }}</a></li>

<li><a href="/newsadmin/">控制台</a></li>

<li><a href="/logout/">登出</a></li>

{% elif request.session.is_login and request.session.user_role == '2' %}

<li><a href="/index/">当前在线:{{ request.session.user_name }}</a></li>

<li><a href="/newsadmin/">控制台</a></li>

<li><a href="/logout/">登出</a></li>

{% elif request.session.is_login and request.session.user_role == '3' %}

<li><a href="/index/">当前在线:{{ request.session.user_name }}</a></li>

<li><a href="/newsadmin/">控制台</a></li>

<li><a href="/logout/">登出</a></li>

{% elif request.session.is_login and request.session.user_role == '4' %}

<li><a href="/index/">当前在线:{{ request.session.user_name }}</a></li>

<li><a href="/newsadmin/">控制台</a></li>

<li><a href="/logout/">登出</a></li>

{% else %}

<li><a href="/login/">登录</a></li>

{% endif %}

</ul>

</div><!-- /.navbar-collapse -->

</div><!-- /.container-fluid -->

</nav>

<!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery,所以必须放在前边) -->

<script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js" integrity="sha384-nvAa0+6Qg9clwYCGGPpDQLVpLNn0fRaROjHqs13t4Ggj3Ez50XnGQqc/r8MhnRDZ" crossorigin="anonymous"></script>

<!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->

<script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js" integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd" crossorigin="anonymous"></script>

{% block content %}{% endblock %}

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-gtEjrD/SeCtmISkJkNUaaKMoLD0//ElJ19smozuHV6z3Iehds+3Ulb9Bn9Plx0x4" crossorigin="anonymous"></script>

</body>

</html>

</footer>

</html>

下面这个就是newsadmin.html,那个Hello无法在页面的表格中显示

{% extends 'base.html' %}

{% load static %}

{% block content %}

{% csrf_token %}

{% if msg %}

<div class="alert alert-warning">{{ msg }}</div>

{% endif %}

{% if request.session.is_login and request.session.user_role == '0' %}

<h1 class="text-center">超级管理员后台</h1>

<div class="col-md-12">

<span><a href="/addnew" class="btn btn-primary">Add New Record</a></span>

<div class="table-responsive">

<table id="bootstrapdatatable" class="table table-striped table-bordered">

<thead>

<th><input type="checkbox" id="checkall" /></th>

<th>ID</th>

<th>Employee Name</th>

<th>Employee Email</th>

<th>Employee Contact</th>

<th>Edit</th>

<th>Delete</th>

</thead>

<tbody>

<tr>

<td><input type="checkbox" class="checkthis" /></td>

<td>{{ hello }}</td>

<td>{{ i.Title }}</td>

<td>{{ employee.email }}</td>

<td>{{ employee.contact }}</td>

<td><p data-placement="top" data-toggle="tooltip" title="Edit"><a href="/edit/{{ employee.id }}" class="btn btn-primary btn-xs" data-title="Edit" data-toggle="modal" data-target="#edit" ><span class="glyphicon glyphicon-pencil"></span></a></p></td>

<td><p data-placement="top" data-toggle="tooltip" title="Delete"><a href="/delete/{{ employee.id }}" class="btn btn-danger btn-xs" data-title="Delete" data-toggle="modal" data-target="#delete" ><span class="glyphicon glyphicon-trash"></span></a></p></td>

</tr>

</tbody>

</table>

</div>

</div>

{% elif request.session.is_login and request.session.user_role == '1' %}

<h1 class="text-center">游戏新闻栏目管理员后台</h1>

{% elif request.session.is_login and request.session.user_role == '2' %}

<h1 class="text-center">国际新闻栏目管理员后台</h1>

{% elif request.session.is_login and request.session.user_role == '3' %}

<h1 class="text-center">娱乐新闻栏目管理员后台</h1>

{% elif request.session.is_login and request.session.user_role == '4' %}

<h1 class="text-center">军事新闻栏目管理员后台</h1>

{% else %}

<h1 class="text-center">你没有访问权限</h1>

{% endif %}

{% endblock %}

问题描述

问题出现的环境背景及自己尝试过哪些方法

相关代码

粘贴代码文本(请勿用截图)

你期待的结果是什么?实际看到的错误信息又是什么?


回答:

render(request, 'newsadmin.html', context)
你这是把数据传到newsadmin.html 这与base.html有啥关系

以上是 django render没办法传到前端 的全部内容, 来源链接: utcz.com/p/937990.html

回到顶部