简体   繁体   English

Static 文件未加载到 django 项目

[英]Static files not loading to django project

I have a project, where my partner worked on the Frontend without using any frameworks, just used js, html and css.我有一个项目,我的合作伙伴在前端工作,没有使用任何框架,只使用了 js、html 和 css。 I wanted to attach that front to the back by using Django.我想通过使用 Django 将前面连接到后面。

Here are the settings from settting.py以下是来自 settting.py 的设置

STATIC_ROOT = os.path.join('C:/Users/1224095/work/Backend/backend/static')

STATIC_URL = '/static/'

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

I added the {% load static %} in my html header.我在我的 html header 中添加了 {% load static %}。 To not confuse you, I made a static directory named static, and inside I got my assets and plugins from the frontend, plus in every place, I added href= {% static 'path to the file' %} .为了不让你感到困惑,我创建了一个名为 static 的 static 目录,在里面我从前端获取了我的资产和插件,而且在每个地方,我都添加了href= {% static 'path to the file' %}

As a result, I am getting an error of 404, does anyone has an idea why?结果,我收到 404 错误,有人知道为什么吗? here's an example of my html:这是我的 html 的示例:

<!doctype html>
<html lang="en">

{% load static %}

<head>

    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="{% static 'assets/plugins/simplebar/css/simplebar.css' %}" rel="stylesheet" />
    <link href="{% static 'assets/plugins/perfect-scrollbar/css/perfect-scrollbar.css' %}" rel="stylesheet" />
    <link href="{% static 'assets/plugins/metismenu/css/metisMenu.min.css' %}" rel="stylesheet" />
    <link href="{% static 'assets/plugins/vectormap/jquery-jvectormap-2.0.2.css' %}" rel="stylesheet" />
    <link href="{% static 'assets/plugins/highcharts/css/highcharts-white.css' %}" rel="stylesheet" />

This is not the best way...这不是最好的方法...

(1): You can only use this in your settting.py==> STATIC_URL = '/static/' (1): 你只能在你的 settting.py==> STATIC_URL = '/static/'中使用这个

(2): You have to Copy and Plast your STATIC files (Js, CSS, ...) in each App with templates(html) that you have created. (2): 您必须使用您创建的模板 (html) 在每个应用程序中复制并粘贴您的 STATIC 文件(Js、CSS、...)。 It will look like that:它看起来像这样:

 App |_migrations |_templates |_static |__App (directory with the App name) |_assets |_js |_css |_img |_plugins |_....

Your HTML will look like this ===>你的 HTML 看起来像这样 ===>

 <,doctype html> <html lang="en"> {% load static %} <head> <.-- Required meta tags --> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link href="{% static 'App/assets/plugins/simplebar/css/simplebar.css' %}" rel="stylesheet" />

Just don't forget to write App before each 'assets'只是不要忘记在每个“资产”之前写 App

Hope this will help you....希望对你有帮助....

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

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