python - 我的 css 和图像没有在 django 中显示

标签 python html css django

我刚开始使用 Django,我的 css 和图像出现在我的 home.html 中时遇到问题。

我在查找后添加了一个 STATICFILES_DIR,但它仍然没有出现。我还尝试将 path('', include('template/pesq.css')) 添加到 urlpatterns 但这只是导致了一些错误,所以我把它去掉了。

主页.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>
...

网址.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'))
]

View .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})

设置.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

最佳答案

你的静态文件应该在YourProject/YourApp/static/pseq.css

我在您的代码中发现的另一个问题是您的 views.homepageview.register 共享相同的路径。它应该是这样的:

urlpatterns = [
    path("", views.homepage, name="homepage"),
    path("register/", views.register, name='register'),

关于python - 我的 css 和图像没有在 django 中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57536191/

相关文章:

python - 如何使用 Python 将 JSONB 插入到 Postgresql 中?

python - 尝试从 google.appengine.api 导入 background_thread(如示例)失败并出现 ImportError

python - 访问 Pandas 中的组

html - 如果图像不存在,Opera 会显示损坏的图像

html - 如何在 float 旁边准确地将文本水平居中

javascript - 我该怎么做才能让我的 cookie 同意横幅为每个用户 (ip) 弹出一次

jquery - 滚动到顶部按钮不淡入/淡出

python - 想要在 django 模板中显示单词的第一个字符

html - 电子邮件不以 iphone 5 和 6 为中心

javascript - 单击时显示 Div 内容