简体   繁体   English

Django 设置 debug=False 中断 href 链接

[英]Django setting debug=False breaks href links

I am developing a website using Django framework.我正在使用 Django 框架开发一个网站。 It works perfectly well using Django=True, however when I set Django=False, system could not find the other html files which are being accessed in <a href=... links.使用 Django=True 可以很好地工作,但是当我设置 Django=False 时,系统找不到在 <a href=... 链接中访问的其他 html 文件。 Let say, I am developing 'mysite', then the following code:假设我正在开发“mysite”,然后是以下代码:

<li><a href="index-2.html"><i class="fab fa-dribbble"></i></a></li>

is present behind an icon on mysite.出现在 mysite 上的图标后面。 Then clicking the icon takes the user to 'mysite.com/index-2.html', however it throws "The requested resource was not found on this server" error.然后单击该图标将用户带到“mysite.com/index-2.html”,但它会引发“在此服务器上找不到请求的资源”错误。

And this happens only when we set Django=False and in production.只有当我们设置 Django=False 并且在生产环境中才会发生这种情况。 No link on the homepage is working due to this.由于这个原因,主页上的任何链接都不起作用。

EDIT-1: I've already reviewed: EDIT-1:我已经审查过:

Why does DEBUG=False setting make my django Static Files Access fail? 为什么 DEBUG=False 设置使我的 django Static 文件访问失败?

The static files are already working fine on my app because they are in 'assets' folder. static 文件已经在我的应用程序上运行良好,因为它们位于“资产”文件夹中。 The html files are in the root folder and somehow system is not able to find any file from root folder when debug is set to False. html 文件位于根文件夹中,当调试设置为 False 时,系统无法从根文件夹中找到任何文件。 So my question is more regarding finding files at root folder using href links.所以我的问题更多是关于使用 href 链接在根文件夹中查找文件。

No error for files in 'assets' folder, but 404 for root folder files: “资产”文件夹中的文件没有错误,但根文件夹文件为 404:

在此处输入图像描述

To resolve this, I added specific functions for each of the.html files in views.py file and added link of the function to main html file.为了解决这个问题,我在 views.py 文件中为每个 .html 文件添加了特定功能,并添加了 function 到主 html 文件的链接。 For example: I added following to my views.py file:例如:我在 views.py 文件中添加了以下内容:

def about(request):
     return render(request,'about.html')

and used as follows in index.html:并在 index.html 中使用如下:

href = '\about'

Doing this for all used html files resolved the issue for both debug=True and debug=False.对所有使用过的 html 文件执行此操作解决了 debug=True 和 debug=False 的问题。 Not sure if this is the best way but working for now.不确定这是否是最好的方法,但现在可以工作。

try using onclick function instead of href and In static files you have add some configration to serve the static files in django. try using onclick function instead of href and In static files you have add some configration to serve the static files in django.

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
#STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
# add manual
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static')
]
MEDIA_ROOT = os.path.join(BASE_DIR,'static/images/')
MESSAGE_TAGS = {
    messages.ERROR: 'danger'
}

if this is work please contact me, I found this from freelance app如果这是工作请联系我,我是从自由应用程序中找到的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM