静态文件无法在 Django 中应用
Static files couldnt applied in Django
我是 Django 的新手,我在 youtube 上看到课程
我尝试将 bootstrap\css 文件包含到 HTML 模板中,并在应用程序中创建了一个静态文件,如照片
我还检查了文件(第二个\ urls.py)并在其中添加了代码
'''
STATIC_URL = '/static/'
INSTALLED_APPS = [
'accounts',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
'''
在 login.html 中有这个 link
'''
{% load static%}
<link rel="=stylesheet" type="text/css" href="{% static 'accounts/style.css' %}">
'''
我应用了其他方式,比如添加这个 second\setting.py
STATICFILES_DIRS = [
os.path.join(BASE_DIR,"accounts/static"),
'accounts/static/'
]
这是我的 urls.py 文件
from django.contrib import admin
from django.urls import path, include
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
path('admin/', admin.site.urls),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
在终端有警告,但我认为它不影响
WARNINGS:
?: (2_0.W001) Your URL pattern '^account/' has a route that contains '(?P<', begins with a '^', or ends with a '$'. This was likely an
oversight when migrating to django.urls.path().
更新:查看您的应用程序代码后,我发现了问题所在。
您必须输入此代码
+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
在主 urls.py
文件中,在您的情况下是 second/urls.py
.
并确保您有此代码
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'name_of_the_root_directory/static'),
]
在您的 settings.py
文件中
你 运行 collectstatic
命令了吗? python manage.py collectstatic
.
也许尝试在您的 urls.py
文件中更改此设置
urlpatterns = [
url(r'^$', views.home),
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
进入这个
urlpatterns = [
url(r'^$', views.home),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
这是我在settings.py
中的配置
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'name_of_the_root_directory/static'),
]
这是我的 urls.py
文件
from django.contrib import admin
from django.urls import path, include
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
path('admin/', admin.site.urls),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
如果你按照我的例子,你必须从终端 运行 collectstatic
命令 window
我是 Django 的新手,我在 youtube 上看到课程 我尝试将 bootstrap\css 文件包含到 HTML 模板中,并在应用程序中创建了一个静态文件,如照片 我还检查了文件(第二个\ urls.py)并在其中添加了代码
'''
STATIC_URL = '/static/'
INSTALLED_APPS = [
'accounts',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
'''
在 login.html 中有这个 link
'''
{% load static%}
<link rel="=stylesheet" type="text/css" href="{% static 'accounts/style.css' %}">
'''
我应用了其他方式,比如添加这个 second\setting.py
STATICFILES_DIRS = [
os.path.join(BASE_DIR,"accounts/static"),
'accounts/static/'
]
这是我的 urls.py 文件
from django.contrib import admin
from django.urls import path, include
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
path('admin/', admin.site.urls),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
在终端有警告,但我认为它不影响
WARNINGS:
?: (2_0.W001) Your URL pattern '^account/' has a route that contains '(?P<', begins with a '^', or ends with a '$'. This was likely an
oversight when migrating to django.urls.path().
更新:查看您的应用程序代码后,我发现了问题所在。
您必须输入此代码
+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
在主 urls.py
文件中,在您的情况下是 second/urls.py
.
并确保您有此代码
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'name_of_the_root_directory/static'),
]
在您的 settings.py
文件中
你 运行 collectstatic
命令了吗? python manage.py collectstatic
.
也许尝试在您的 urls.py
文件中更改此设置
urlpatterns = [
url(r'^$', views.home),
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
进入这个
urlpatterns = [
url(r'^$', views.home),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
这是我在settings.py
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'name_of_the_root_directory/static'),
]
这是我的 urls.py
文件
from django.contrib import admin
from django.urls import path, include
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
path('admin/', admin.site.urls),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
如果你按照我的例子,你必须从终端 运行 collectstatic
命令 window