- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在构建一个 Javascript 交互性有限的 Django 应用程序,并且正在研究如何将 Vue 模板与 Django 模板合并以实现相同的内容。
想象一个无限滚动的页面,其中 SEO 非常重要。 Django 非常擅长解决这个问题,因为它根据定义是一个服务器端框架。但是,如果这两种技术都需要呈现相同的内容,那么如何丰富 Django 使用 Vue 呈现的页面呢?在这种情况下,Django 将为 SEO 爬虫进行渲染,在 Vue 接管之前,Vue “水合”这些组件并使它们具有交互性。发生这种情况后,后续使用 AJAX 异步获取的内容也将使用 Vue 进行模板化。
我做了一些研究,但没有找到足够的信息来解决这个问题:
我没有感觉到这些来源正在谈论 SEO,或者更确切地说,如果它们是,他们仅在内容不属于 SEO 的情况下使用 Vue 模板(例如打开模式)。
下面是这两种技术以不同方式呈现相同内容的初步想法。由于 Vue 有 delimiter
选项,我觉得也许有一种方法可以将两者结合起来(以避免模板语法的冲突)。
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<title>Django and Vue test</title>
</head>
<body>
{% if names %}
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script type="text/javascript">
var names = {{ names_json|safe }};
</script>
<div id="app">
<!-- This list is for SEO purposes, let's say 'above the fold' content contained in a responsive grid before an infinite scroll is triggered -->
<h2>Django list</h2>
<ul>
{% for name in names %}
<li>
<div>
<img src="avatar.jpg">
<div>
<div>{{ name }}@test.se</div>
</div>
</div>
</li>
{% endfor %}
</ul>
<!-- This list is supposed to somehow 'hydrate' the django template content, in order to enrich the template with interactive VueJS. When the client Vue instance is instantiated/mounted, the idea is that only these elements populate the page, and not the SSR ones -->
<h2>Vue list</h2>
<ul>
<li v-for="name in names">
<div v-on:click="greet(name)">
<img src="avatar.jpg">
<div>
<div>[[ name ]]@test.se</div>
</div>
</div>
</li>
</ul>
</div>
<script>
var app = new Vue({
delimiters: ['[[', ']]'],
el: '#app',
data: {
names: names,
},
methods: {
greet: function (name) {
console.log('Vue says hello from ' + name + '!')
}
}
});
</script>
{% endif %}
</body>
</html>
我是否以错误的方式思考这个问题,或者你们中有人能给我任何指导吗?
非常感谢。
最佳答案
我就是这样做的:
View .py
def show_customers(request):
customers = Customer.objects.all()
context = {
'customers': customers,
}
return render(request, "app/customers.html", context)
模板:customers.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Customer list</title>
{% if DEBUG %}
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
{% else %}
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
{% endif %}
</head>
<body>
<div>
{% for customer in customers|slice:":3" %}
<p>
{{ customer.id }} - {{ customer.user.username }}
</p>
{% endfor %}
{% verbatim %}
<script id="more-customers-template" type="text/template">
<p>
{{ customer.id }} - {{ customer.name }}
</p>
</script>
{% endverbatim %}
</div>
<div class="issue-more-posts" id="more-customers">
<customer
v-for="customer in customers"
v-bind:customer="customer"
v-bind:key="customer.id"
></customer>
<button @click="loadMorecustomer">
SHOW MORE customers
</button>
</div>
<script type="text/javascript">
Vue.component('customer', {
props: {
customer: Object,
nextPage: String
},
template: "#more-customers-template"
});
var customerMore = new Vue({
el: '#more-customers',
data: {
customers: []
},
methods: {
loadMorecustomer: function () {
this.customers = [{name: 'Some dynamic data', id: 1}]
}
}
});
</script>
</body>
</html>
此代码用于演示目的,您需要将 JS 代码和 Vue 模板代码分开。
编辑:我发现了一篇关于此的非常好的文章,甚至更清晰。 vuejs-and-django
关于javascript - 出于 SEO 原因,对相同内容使用 Django 模板 + Vue 模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58082170/
我在将过滤器从 vue 1 迁移到 vue 2 时遇到问题,我在这里完全创建了我需要的东西(突出显示与输入文本匹配的文本): Vue.component('demo-grid', { templa
我有一个在 vue 组件外部运行的函数。我想要将它返回的数据传递给vue组件中的数据。 function example(){ var item = 'item'
我正在尝试安装一个 Vue 插件,以便我可以使用选项管理一些 API 调用。我有一个 stocks.js 文件,我想从中进行 API 调用。 当我执行以下操作时,出现'Vue is defined b
如何从指令访问 Vue 实例? 我有这个 HTML 和这个脚本 var app = new Vue({ el: '#vueApp', data: { myData:
如何在 .vue 文件中使用 Vue.set() 和 Vue.use()?我正在使用 vue-cli 搭建我的项目,我需要使用 Vue.use(VeeValidate) 进行验证。我也想使用类似下面的
从 vue-property-decorator 和 vue 导入 Vue 之间有什么区别和用例?据我所知,在使用 @Component 装饰器定义自定义组件时,我始终需要从 vue-property
有没有办法使用 yarn serve(可能使用 webpack/vuetify-loader)在本地 Vuetify 应用程序的本地 npm 依赖项上发生热重载? 商业案例 我们有一些通用的 Vuet
我有一个在某些未知情况下不可靠的插槽的奇怪错误。 成分 有3个层次组件。 孙子 (headlessTable),它提供一个名为 arrayValue 的插槽. 子项 (collapsableCard)
我是 Vue 本地新手,我也遇到了一个问题,can I use the Vue component inside a Vue native component such as Vue-chart an
Vue.delete 的替代方案是什么?在 Vue 3 的新 Reactivity API 中? 最佳答案 Vue.delete和 Vue.set在 Vue 3 中不需要。通过使用代理的新 react
我是 Vue 的新手,正在尝试学习如何使用它。 我想我在尝试安装一个新的 Vue 应用程序时被绊倒了。 这是我可以开始工作的内容: const vm = new Vue({}) 从那里我可以安装
我使用boots-vue。我从文档https://bootstrap-vue.js.org/docs/components/table/#table-body-transition-support中举
我真的只是想为我的图书馆建立一个 jest+vue 的框架,并迅速得到这个错误。 我知道这个结构不是通常的笑话结构,我在这里尝试用一个辅助控件来描述一个测试。 这是我的test的内容文件夹:array
我正在尝试使用基于 examples 的 vue-router , 如 let routes = [ { path: '/', component: MainComponent }, ];
我有一个想要通过简单的 v-model 功能发布到 NPM 的组件。 因此,如果它能够在 vuejs 2/3 上互换运行那就更理想了。 我可以通过将组件设置为发出 input 和 update:mod
我正在尝试在 bootstrap-vue 表中创建一个插槽,以使用自定义组件呈现任何 bool 值。 所以我有一个简单的表格 现在,如果我想以特定方式渲染单个列,我必须使用插槽 它有
Vue Router 在主 Vue 实例之前加载,但要加载该 Router,我应该准备一些信息,然后将它们作为属性传递给此 Route。在此示例中,它是从主 Vue 实例传递到主屏幕的 current
我有一个想要通过简单的 v-model 功能发布到 NPM 的组件。 因此,如果它能够在 vuejs 2/3 上互换运行那就更理想了。 我可以通过将组件设置为发出 input 和 update:mod
我找到了一个关于如何使用 Vue 路由器的很好的例子。这是 app.js 文件: // Telling Vue to use the router Vue.use(VueRouter) // Init
我没有完整的 vue 应用程序,所以我使用 custom elements替换一些应该用 vue 处理的元素。 我只想使用 vue multiselect plugin在 html 文件中。 所以我尝
我是一名优秀的程序员,十分优秀!