前端 - 网页根目录改成子目录后,文件如何调用?
PHP中文网
PHP中文网 2017-04-18 09:06:32
0
4
900

用django写的程序,然后大概的问题是这样的.跟这个类似:

然后按照答案 改成 ./ 的话,首页是可以访问的,但是内页,文章页就不行了.
网站的结构 :

默认生成的都是 / 的,我知道把所有的链接前面加一个 目录的名称 比如 /001/ 也是可以访问的,但是感觉这样不够智能 而且也不够方便,求快捷的方法?

PHP中文网
PHP中文网

认证0级讲师

reply all(4)
刘奇

The following excerpts are from a previous book I wrote: It's Django

Use static files

Static files will also be needed in dynamic websites, such as images, css, js files, etc. used by the website. How do we manage and use these static files? First of all, the upper layer mysite底下增加兩個資料夾 staticassetsstatic 資料夾是開發時用來放置靜態檔的目錄,該目錄底下可以新增數個子目錄來放置不同種類的靜態檔,比如說設置 img 來放置圖片或是 css 來放置 css 檔。而 assets is the directory where the static files are placed when the website is actually online. The reason why these two directories are separated is because we need to hand over the management of static files to the web server when it goes online.

Next, let’s set it up in settings.py:

...
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)
STATIC_ROOTS = os.path.join(BASE_DIR, 'assets')
...

A total of three parameters are set here. The detailed description is as follows:

Parameters Instructions
STATIC_URL The URL pattern of the static file, here we set it to the path in /static/ ,那麼在網頁路徑中以 /static/ 開始的便會被視為靜態檔,如: http://127.0.0.1/static/hello.pnghttp://127.0.0.1/static/hi.js ,但如果是 http://127.0.0.1/restaurants/static/ 則會匹配urls.py instead of the static file
STATICFILES_DIRS The folder where static files are placed during development. Allows setting up of multiple folders to indicate the location of static files. As set up above, we can create a folder with BASE_DIR所指示的資料夾底下新增一個 static and put image files, css, and js in it
STATIC_ROOT Place the folder for static files when going online. After using python manage.py collectstatic ,Django 會將 STAIC_DIRS 下發現的靜態檔複製至 STATIC_ROOT 下。由於當設定檔的 DEBUG 設為 False, Django will not process the return of static files by default. These files are collected into a folder through commands to facilitate web server management and reading

After the above parameters are set, they can be used in the template STATICFILES_DIRS 資料夾下的靜態檔案。假設網站要新增一張圖片 logo.png (放在 mysite/static/img/logo.png ) On the page, we will write this on the template:

...
    <img src='/static/img/logo.png'>
...

And you’re done. But if one day the web page path of the static file is forced to be changed to /static_file/ , you have to find the path and modify it one template at a time. Isn't this tiring? A comparison with "Django" is as follows:

{% load staticfiles %}
...
    <img src="{% static 'img/logo.png' %}">
...

Please remember to load staticfiles ,再使用標籤 {% static ...%} 。經過以上調整之後,Django在處理靜態檔的時候便會將 {% static 'img/logo.png' %} 轉為 '/static/img/logo.png' ,而且當有需要修改靜態檔的路徑時,可以放心大膽地修改 settings.py 檔中的 STATIC_URL in the template first, so you don’t need to worry about modifying a large number of templates.


Questions I answered: Python-QA

洪涛

If you refer to the URL corresponding to the View, you can read this document
https://docs.djangoproject.com/en/1.9/intro/tutorial03/#removing-hardcoded-urls-in-templates

If you are referring to static files, such as js, css, you can achieve this by changing the STATIC_URL in the Django configuration
https://docs.djangoproject.com/en/1.9/howto/static-files/

Peter_Zhu

.NET has outbound urlrewrite
It sounds confusing
To put it simply, it is to add a filter before the html output
Replace all link addresses starting with /

黄舟

Apache seems to be able to use the .htaccess file to rewrite the accessed address, a regular rule

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template