[英]My css and images arent showing in django
我非常擅長使用Django,而且我的home.html中出現了我的CSS和圖像問題。
我在查找之后添加了一個STATICFILES_DIR,但它仍然沒有出現。 我也嘗試將path('', include('template/pesq.css'))
到urlpatterns,但這只會導致一些錯誤,所以我把它拿出來了。
home.html的
<html>
<head>
<title>homepage</title>
{% load static %}
<link href="{% static 'pesq.css' %}" rel="stylesheet">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
...
urls.py
from django.urls import path
from . import views
from django.conf.urls import url, include
app_name = "main"
urlpatterns = [
path("", views.homepage, name="homepage"),
path("", views.register, name='register'),
path('tinymce/', include('tinymce.urls')),
#path('', include('template/pesq.css'))
]
views.py
from django.shortcuts import render, redirect
from django.http import HttpResponse
from django.template import Context, loader
from django.contrib.auth.decorators import login_required
from django.contrib.auth import login, authenticate
from django.contrib.auth.forms import UserCreationForm
from .models import info
# Create your views here.
def homepage(request):
return render(request=request, template_name="template/home.html", context={"info": info.objects.all})#context_instance=RequestContext(request),
def register(request):
if request.method == "POST":
form = UserCreationForm(request.POST)
if form.is_valid():
user = form.save()
username = form.cleaned_data.get('username')
login(request, user)
return redirect("main:homepage")
else:
for msg in form.error_messages:
print(form.error_messages[msg])
return render(request = request,
template_name = "main/register.html",
context={"form":form})
form = UserCreationForm
return render(request = request,
template_name = "main/register.html",
context={"form":form})
setting.py
...
DEBUG = True
...
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'main.apps.MainConfig',
'tinymce',
]
...
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['C:/Users/usr/Desktop/djangoProjects/mysite/main/'],
'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/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
pesq.css
.column {
float: left;
width: 50%;
}
/* Clear floats after the columns */
.row:after {
content: "";
display: table;
clear: both;
}
a:visited{
color:lightgreen;
}
.ovlist h3{
position:relative;
display:inline-block;
margin:30px;
font-style:italic;
}
.ovlist {
background-color: lightgreen;
height: 50px;
}
完整目錄是,C:/Users/usr/Desktop/djangoProjects/mysite/main/template/pesq.css
通過定義STATICFILES_DIRS
您已指示Django在運行python manage.py collectstatic
時將該目錄中的所有文件復制到STATIC_ROOT
設置定義的目錄。
所以,你的CSS文件應位於STATICFILES_DIRS定義的目錄中Django開發服務器后,會發現他們內部 python manage.py runserver
調用。 對於生產,您應該運行collectstatic
管理命令並定義STATIC_ROOT,如:
STATIC_ROOT = os.path.abspath(os.path.join(BASE_DIR, '../../static'))
您的靜態文件應該在YourProject/YourApp/static/pseq.css
我在你的代碼中發現的另一個問題是你的views.homepage
和view.register
共享相同的路徑。它應該是這樣的:
urlpatterns = [
path("", views.homepage, name="homepage"),
path("register/", views.register, name='register'),
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.