簡體   English   中英

如何在 django 中鏈接我的 css、js 和圖像文件鏈接

[英]How to link my css, js and image file link in django

我是 Django 1.9.5 的新手,使用 windows 作為我的平台。 我在將 css、圖像和 js 鏈接到 django 模板時遇到問題,

這是我的項目結構

這是我的 setting.py 頁面

這是我的設置頁面

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    PROJECT_DIR = os.path.dirname(__file__)

    STATIC_ROOT = os.path.join(BASE_DIR, 'static')
    STATIC_URL = '/static/'
    STATICFILES_DIRS = (
        os.path.join(BASE_DIR, 'statics'),
    )


    MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
    MEDIA_URL = '/media/'

這是我的主要 url.py 頁面

這是我的主要 url.py 頁面

from django.conf import settings
    from django.conf.urls import include, url
    from django.conf.urls.static import static
    from django.contrib import admin
    from django.contrib.staticfiles.urls import staticfiles_urlpatterns

    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'^mysite/', include('myapp.urls')),
        # (r'^media/(?P.*)$', 'django.views.static.serve',
        #  {'document_root': settings.MEDIA_ROOT}),
        url(r'^media/(?P.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
    ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

    urlpatterns += staticfiles_urlpatterns()

這是我位於模板文件夾中的基本 html 頁面

這是我的基本 html 模板

這是我的基本 html 模板

Github 鏈接我在 github 中的項目我嘗試了所有可能的組合,但在 2 天內失敗了。 任何幫助都將被挪用,我​​將不勝感激 謝謝

    {% load staticfiles %}

    <link rel="stylesheet" type="text/css" href="{% static 'pathtostaticfile' %}" />

您可以使用 statcfiles 標簽來加載您的靜態文件。 使用 pathtostaticfile 是您的靜態文件

更多細節https://docs.djangoproject.com/en/1.9/intro/tutorial06/

首先添加以下代碼

urls.py使用這個庫

from django.contrib.staticfiles.urls import staticfiles_urlpatterns

urlpatterns += staticfiles_urlpatterns()

settings.py中添加以下代碼

STATIC_URL = '/static/'
STATICFILES_DIRS= [os.path.join(BASE_DIR,'assets'),] # this variable have been created for  adding static resourcess

在模板(html)文件中,您將首先加載

{% load static %}

<link rel="stylesheet" href=" {% static '/boostrap4.4/bootstrap.min.css' %}">
     <link rel="stylesheet" href=" {% static '/fontawesome-free-5.12.1-web/css/all.css' %}">
     <link rel="stylesheet" href=" {% static '/css/style.css' %}">

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM