简体   繁体   中英

Django setting debug=False breaks href links

I am developing a website using Django framework. 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. Let say, I am developing 'mysite', then the following code:

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

is present behind an icon on 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.

And this happens only when we set Django=False and in production. No link on the homepage is working due to this.

EDIT-1: I've already reviewed:

Why does DEBUG=False setting make my django Static Files Access fail?

The static files are already working fine on my app because they are in 'assets' folder. 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. So my question is more regarding finding files at root folder using href links.

No error for files in 'assets' folder, but 404 for root folder files:

在此处输入图像描述

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. For example: I added following to my views.py file:

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

and used as follows in index.html:

href = '\about'

Doing this for all used html files resolved the issue for both debug=True and 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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