如何让 liveserver 渲染 django 模板? [英] How to get liveserver to render django templates?

查看:48
本文介绍了如何让 liveserver 渲染 django 模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用教程站点,当我尝试打开 Django 模板时,我发现我的 VS Code LiveServer 插件无法正常工作.我应用的 CSS 丢失了(尽管一切都在我的本地开发服务器中正确呈现),并且模板语言代码实际上是打印到屏幕上而不是执行(见下图).我的 liveserver 插件似乎正在处理 Django 之外的 html 文件.

(1) 现在我右击并选择用 Liveserver 打开".这对 Django 来说是错误的吗?liveserver 文档建议尝试访问实际服务器地址:http://localhost/[workspace],而不是 VS Code 扩展的 Live Server 地址:

解决方案

我和您遇到了同样的问题,并设法通过您在帖子第 (2) 点中包含的链接解决了问题.你现在可能已经分类了,但我想我会在这里发帖以防万一.

首先我去我的控制台运行:

pip install livereload

安装成功.然后我在我的 settings.py 文件中向我安装的应用程序添加了livereload".例如...

INSTALLED_APPS = [...'livereload',...]

然后我转到终端上包含 manage.py 文件的目录,然后运行

./manage.py livereload

这为我提供了一个指向实时更新服务器的链接.以前我一直在使用python manage.py runserver"运行服务器,所以这是替换该命令.现在,当我保存在 IDE 中所做的任何更改时,它们会在浏览器上实时更新.抱歉,为了清楚起见,我没有包含任何屏幕截图,由于某种原因我无法添加.

I've been messing around with a tutorial site, and I found that my VS Code LiveServer plugin doesn't work properly when I try to open Django templates. The CSS I applied is missing (although everything renders correctly in my local development sever), and the template language code is actually printed to the screen rather than executed (see image below). My liveserver plugin appears to be working with html files outside of Django.

(1) Right now I'm right clicking and selecting "Open with Liveserver." Is this wrong for Django? The liveserver docs recommend trying to "visit the Actual Server Address: http://localhost/[workspace], not the VS Code extension's Live Server Address: http://127.0.0.1:5500/". I tried including the file path in place of [workspace], but no luck. What do I do here?

(2) I saw in another thread where someone recommended their own solution, here. I'm not sure where I'm supposed to run the './manage.py livereload' command, but it's not working in command prompt. What is the difference between './manage.py' and 'py manage.py'? And will this solution be any better than the VS Code plugin?

解决方案

I have had the same problem as you, and managed to get it working with the link you included in the point (2) of your post. You may have gotten sorted by now, but I thought I'd post here incase.

First I went to my console and ran:

pip install livereload

which installed successfully. Then I added 'livereload' to my installed apps in my settings.py file. E.g...

INSTALLED_APPS = [ ... 'livereload', ... ]

Then I went to the directory containing my manage.py file on the terminal, and ran

./manage.py livereload

and this provided me with a link to a live update server. Previously I had been running a server with "python manage.py runserver", so this is to replace that command. Now when I save any changes I make in my IDE, they update live on the browser. Sorry that I haven't included any screenshot images for clarity, I wasn't able to for some reason.

这篇关于如何让 liveserver 渲染 django 模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆