Python CSS静态文件在Django中不起作用,我是Django的初学者,main.CSS属性未应用于my store.html
这是在settings.py中添加的Python CSS静态文件在Django中不起作用,我是Django的初学者,main.CSS属性未应用于my store.html,python,html,css,django,Python,Html,Css,Django,这是在settings.py中添加的 STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'static'), ] from django.urls import path from . import views from django.contrib.staticfiles.urls import staticfiles_urlpatterns urlpatterns = [ path('', views.store, name=&q
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
from django.urls import path
from . import views
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = [
path('', views.store, name="store"),
]
urlpatterns += staticfiles_urlpatterns()
from django.shortcuts import render
from django.template.context import RequestContext
def store(request):
context ={}
return render(request, 'store/store.html',context)
url.py
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
from django.urls import path
from . import views
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = [
path('', views.store, name="store"),
]
urlpatterns += staticfiles_urlpatterns()
from django.shortcuts import render
from django.template.context import RequestContext
def store(request):
context ={}
return render(request, 'store/store.html',context)
视图.py
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
from django.urls import path
from . import views
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = [
path('', views.store, name="store"),
]
urlpatterns += staticfiles_urlpatterns()
from django.shortcuts import render
from django.template.context import RequestContext
def store(request):
context ={}
return render(request, 'store/store.html',context)
store.html
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'css/main.css' %}">
<h3>store</h3>
<img src="{% static 'images/cart.png' %}">
文件夹树
商店->商店->商店.html
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'css/main.css' %}">
<h3>store</h3>
<img src="{% static 'images/cart.png' %}">
static->css->main.css
body{
background-color: blue;
}
static->images->cart.png代码看起来很干净,但有一个地方你显然忽略了。也许我们可以通过将下面的细节与您的项目进行比较来找到解决方案 设置.py
INSTALLED_APPS = [..,
'your_app',]
...
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
...
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static")
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static'), ]
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'media')
url.py
urlpatterns = [
path('admin/', admin.site.urls),
path('', IndexView.as_view(),name='index'),
]
if settings.DEBUG:
urlpatterns = urlpatterns + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns = urlpatterns + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
models.py
class Task(models.Model):
user = models.ForeignKey("auth.User", on_delete=models.CASCADE)
title = models.CharField(max_length=150)
content = RichTextField()
created_date = models.DateTimeField(auto_now_add=True)
slug = models.SlugField(editable=False)
def __str__(self):
return self.title
def save(self, *args, **kwargs):
self.slug = slugify(self.title)
super(Task, self).save(*args, **kwargs)
views.py
class IndexView(ListView):
template_name = 'task/index.html'
model = Task
context_object_name = 'task'
# paginate_by = 7
def get_context_data(self, *, object_list=None, **kwargs):
context = super(IndexView, self).get_context_data(**kwargs)
return context
如果要在html文件中使用静态文件,必须指定如下内容:
.html
{%load static%}
....
Templates文件夹树=>Templates->task->index.html
静态文件夹树=>Static->css->file.css
=>static->css->file.css是否检查并确保文件链接正确?它不是“/static/css/main.css”吗?