简体   繁体   English

在Django中加载静态文件

[英]Loading static files in django

I'm just at my wits end here. 我只是机智在这里。 I want to load my custom js files from the static folder. 我想从静态文件夹加载我的自定义js文件。 When I view the source on Chrome, it's not showing up and it's not being loaded when I refresh the page. 当我在Chrome上查看源代码时,刷新页面时未显示源代码,也未加载源代码。

What it looks like on Chrome (and I can't click on my file, while the jQuery does light up): 在Chrome上的外观(当jQuery点亮时,我无法单击文件):

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"/>
<script src="/static/js/ajax.js"/>

UPDATE : Here is what I have now, but it's still not working 更新 :这是我现在所拥有的,但仍然无法正常工作

base.html base.html文件

{% load staticfiles %}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"/>
<script src="{% static 'js/ajax.js' %}"/>

settings.py settings.py

STATIC_ROOT = 'static/'
STATIC_URL = 'assets/'
STATICFILES_DIRS=(BASE_DIR, 'assets')

I ran python manage.py collectstatic and it copied a bunch of files over. 我运行python manage.py collectstatic ,它复制了一堆文件。

Here is a layout of the updated structure. 这是更新结构的布局。 You can see that the ajax.js was copied over but it's still not showing up when I run the server. 您可以看到ajax.js被复制了,但是当我运行服务器时它仍然没有显示。 Any ideas? 有任何想法吗?

在此处输入图片说明

What am I missing?? 我想念什么? Please help. 请帮忙。 Python 2.7, Django 1.7 Python 2.7,Django 1.7

Obligatory reading: 必读:

https://docs.djangoproject.com/en/1.7/howto/static-files/ https://docs.djangoproject.com/en/1.7/ref/contrib/staticfiles/ https://docs.djangoproject.com/en/1.7/ref/settings/#static-files https://docs.djangoproject.com/zh-CN/1.7/howto/static-files/ https://docs.djangoproject.com/zh-CN/1.7/ref/contrib/staticfiles/ https://docs.djangoproject.com/ EN / 1.7 / REF /设置/#静态文件


About these settings: 关于这些设置:

STATIC_URL = 'assets/'

This means that your app should serve static files with assets/{name} prefix. 这意味着您的应用应提供带有assets/{name}前缀的静态文件。

I think this is the source of your problem - the initial / is missing and you're generating relative links for static resources, which sounds like a Bad Idea. 我认为这是问题的根源 -开头的/丢失了,并且您正在为静态资源生成相对链接,这听起来像个坏主意。

Now a page at http://yourserver/foo/bar/ would ask for static files via a relative link: 现在,位于http://yourserver/foo/bar/将通过相对链接询问静态文件:
<img src="assets/{name}"> , so your browser would look in /foo/bar/assets/{name} and find nothing. <img src="assets/{name}"> ,因此您的浏览器将在/foo/bar/assets/{name}查找而未找到任何内容。
You'd normally want an absolute link for static files, so instead you should use STATIC_URL = '/assets/' and obtain absolute links <img src="/assets/{name}"> . 通常,您需要静态文件的绝对链接,因此应使用STATIC_URL = '/assets/'并获取绝对链接<img src="/assets/{name}">


STATICFILES_DIRS=(BASE_DIR, 'assets')

Okay, so how does Django find the static files? 好的,那么Django如何找到静态文件? A Django project can have many installed applications, and each application can have its own directory with static files. Django项目可以具有许多已安装的应用程序,并且每个应用程序可以具有自己的包含静态文件的目录。

So there's this setting (set by default): 所以有这个设置(默认设置):

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

This means that, on development setup ( runserver ), whenever someone asks for http://yourserver/assets/ponies/applejack.jpg (where /assets/ is your, now hopefully fixed, STATIC_URL ), Django would try to find a file ponies/applejack.jpg in the following way: 这意味着,在开发设置( runserver )上,每当有人要求http://yourserver/assets/ponies/applejack.jpg (其中/assets/是您的STATIC_URL ,现在希望是已修复的)时,Django都会尝试查找文件ponies/applejack.jpg以下列方式:

  • first by using FileSystemFinder that looks in some fixed directories you specify ( STATICFILES_DIRS ), 首先使用FileSystemFinder ,它会在您指定的某些固定目录( STATICFILES_DIRS )中STATICFILES_DIRS
  • second by using AppDirectoriesFinder that looks into the static/ subdirectory of each of your INSTALLED_APPS . 其次,通过使用AppDirectoriesFinder来查找每个INSTALLED_APPSstatic/子目录。

So if you want to keep your static files per-app, you don't need to rely on FileSystemFinder and you can leave STATICFILES_DIRS empty. 因此,如果STATICFILES_DIRS应用程序保留静态文件,则无需依赖FileSystemFinder ,可以将STATICFILES_DIRS留空。

But if you want to keep all the static files in one place, point STATICFILES_DIRS to any directory like you did. 但是,如果要将所有静态文件都放在一个位置,请像您一样将STATICFILES_DIRS指向任何目录。 You should still keep AppDirectoriesFinder for third party apps you use, like the Django Admin, that come with their own static files. 您仍应为自己使用的第三方应用(如Django Admin)保留AppDirectoriesFinder ,这些应用带有其自己的静态文件。


STATIC_ROOT = 'static/'

This is unnecessary for development setup with runserver because Django dev server serves your static files itself. 这对于使用runserver进行开发设置是不必要的,因为Django dev服务器本身会提供您的静态文件。

For production, however, you probably want your web server (Apache, Nginx, ...) or a content delivery network to handle the static file delivery for you. 但是,对于生产而言,您可能希望您的Web服务器(Apache,Nginx等)或内容传送网络为您处理静态文件传送。 You can then use the collectstatic command to collect all the static files from everywhere (eg all static files your STATICFILES_FINDERS can find), and put them all in one directory on your machine, from where you can easily pass them on to Nginx or copy over to a CDN. 然后,您可以使用collectstatic命令从任何地方收集所有静态文件(例如,您的STATICFILES_FINDERS可以找到的所有静态文件),并将它们全部放置在计算机上的一个目录中,从那里您可以轻松地将它们传递到Nginx或复制到到CDN。

Do you have this in your settings? 您的设置中有这个吗?

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

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

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