Python 我的css和图像没有在django中显示
我对使用Django非常陌生,我的css和出现在my home.html中的图像有问题 我在查找后添加了一个STATICFILES\u DIR,但是它仍然没有出现。我还尝试向urlpatterns添加Python 我的css和图像没有在django中显示,python,html,css,django,Python,Html,Css,Django,我对使用Django非常陌生,我的css和出现在my home.html中的图像有问题 我在查找后添加了一个STATICFILES\u DIR,但是它仍然没有出现。我还尝试向urlpatterns添加path(“”,include('template/pesq.css')),但这只会导致一些错误,所以我将其删除 home.html <html> <head> <title>homepage</title> {% load static %}
path(“”,include('template/pesq.css'))
,但这只会导致一些错误,所以我将其删除
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>
...
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\u DIRS
您已经指示Django在运行python manage.py collectstatic
时将所有文件从该目录复制到STATIC\u ROOT
设置定义的目录
因此,您的css文件应该位于STATICFILES\u DIRS中定义的目录中,Django development server将在调用python manage.py runserver
后在该目录中找到它们。对于生产,您应该运行collectstatic
management命令并定义STATIC\u ROOT,如下所示:
STATIC_ROOT = os.path.abspath(os.path.join(BASE_DIR, '../../static'))
静态文件应该位于
YourProject/YourApp/static/pseq.css
我在你的代码中发现的另一个问题是,你的视图.homepage
和视图.register
共享相同的路径。应该是这样的:
urlpatterns = [
path("", views.homepage, name="homepage"),
path("register/", views.register, name='register'),
urlpatterns = [
path("", views.homepage, name="homepage"),
path("register/", views.register, name='register'),