简体   繁体   English

我的CSS和图像没有在django中显示

[英]My css and images arent showing in django

I'm very new to using Django and am having issues with my css and images appearing in my home.html. 我非常擅长使用Django,而且我的home.html中出现了我的CSS和图像问题。

I added a STATICFILES_DIR after looking it up, however it's still no appearing. 我在查找之后添加了一个STATICFILES_DIR,但它仍然没有出现。 I also tried adding path('', include('template/pesq.css')) to urlpatterns but that just caused some errors so I took it out. 我也尝试将path('', include('template/pesq.css'))到urlpatterns,但这只会导致一些错误,所以我把它拿出来了。

home.html 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 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 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 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 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;
}

The full directory is, C:/Users/usr/Desktop/djangoProjects/mysite/main/template/pesq.css 完整目录是,C:/Users/usr/Desktop/djangoProjects/mysite/main/template/pesq.css

By defining STATICFILES_DIRS you've instructed Django to copy all the files from that directory to the directory defined by STATIC_ROOT setting when you run python manage.py collectstatic . 通过定义STATICFILES_DIRS您已指示Django在运行python manage.py collectstatic时将该目录中的所有文件复制到STATIC_ROOT设置定义的目录。

So, your css files should be located inside a directory defined in STATICFILES_DIRS where Django development server will find them after python manage.py runserver call. 所以,你的CSS文件应位于STATICFILES_DIRS定义的目录中Django开发服务器后,会发现他们内部 python manage.py runserver调用。 For production you should run collectstatic management command and define STATIC_ROOT too like: 对于生产,您应该运行collectstatic管理命令并定义STATIC_ROOT,如:

STATIC_ROOT = os.path.abspath(os.path.join(BASE_DIR, '../../static'))

Your static files should be inside YourProject/YourApp/static/pseq.css 您的静态文件应该在YourProject/YourApp/static/pseq.css

and also another issue what i found in your code is that your views.homepage and view.register sharing the same path .It should be like this: 我在你的代码中发现的另一个问题是你的views.homepageview.register共享相同的路径。它应该是这样的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM