Django:如何使用静态文件(简单情况,jquery)
我正在尝试在 Django 网站上使用 jQuery。我需要包含 jQuery.js 库。我读过很多关于 Django 静态文件的内容,但我认为没有人问过这个特定的问题。我只有三个静态文件可供使用:jquery.js、anothersmallfile.js 和 styles.css。关于静态文件服务的 Django 文档说:
“对于小型项目,这不是什么大问题,因为你可以将静态文件保存在你的 Web 服务器可以找到的地方。link
我想“把它们放在我的网络服务器可以找到它们的地方”,因为 Django 文档在其他地方明确指出(警告)他们的静态文件服务方法仅适用于开发环境。我只有一些静态文件,而且我只想要最简单的安全解决方案。
无论我将文件放在哪里,Django 都无法通过 Chrome 找到它们。 Web 开发人员控制台我看到 404 错误:
GET http://127.0.0.1:8000/templates/polls/jquery.js 404 (NOT FOUND)
我是运行服务器的新手。 A.) 需要告诉我的 urls.py 文件在哪里可以找到静态文件吗?或者问题可能是 B.) 我对这个问题有误解—— Django是我的网络服务器(用于生产),所以现在我必须使用 Django 静态文件解决方案
似乎应该很难获取我的模板 ?只需识别与它们位于同一目录中的 .js 文件即可。我错过了什么吗?
编辑,在我获得更多反对票之前:我正在谈论上面链接的页面中的这段话:
////////////////////////
Django 开发人员最关心的是Web 应用程序的动态部分 - 针对每个请求重新呈现的视图和模板。但 Web 应用程序还有其他部分:渲染完整网页所需的静态文件(图像、CSS、Javascript 等)。
对于小型项目,这并不是什么大问题,因为您可以将静态文件保存在 Web 服务器可以找到的地方。 但是,在较大的项目中(尤其是由多个应用程序组成的项目),处理每个应用程序提供的多组静态文件开始变得棘手。
这就是 django.contrib.staticfiles 的用途:它将来自每个应用程序(以及您指定的任何其他位置)的静态文件收集到一个可以在生产中轻松使用的位置。
////////////////////////////////////// 添加强调
那么,如果这就是 django.contrib.staticfiles 的用途,那么更简单的解决方案是什么?我认为这是对之前问题的重复。
I am trying to use jQuery on a Django site. I need to include the jQuery.js library. I have read a lot about Django static files, but I don't think anyone has asked this particular question. I have only three static files to serve: jquery.js, anothersmallfile.js, and styles.css. The Django docs on static file serving say:
"For small projects, this isn’t a big deal, because you can just keep the static files somewhere your web server can find it. link
I would like to "just keep them somewhere my webserver can find them" because elsewhere the Django docs clearly state (warn) that their static-files serving method is only for a development environment. I only have a few static files and I just want the simplest secure solution.
Unfortunately I can't get it working. No matter where I put the files, Django can't find them. Debugging through Chrome web developer console I see I'm getting a 404 error:
GET http://127.0.0.1:8000/templates/polls/jquery.js 404 (NOT FOUND)
I am new to running a server. Do I A.) need to tell my urls.py file where to find static files? or perhaps the problem is B.) that I have misunderstood this issue - Django is my webserver (for production) so right now I must use the Django static files solution?
Doesn't seem like it ought to be very difficult to get my templates to simply recognize a .js file that's in the same directory as they are. Am I missing something?
Edit, before I get more downvotes: I am talking about this passage from the page linked above:
///////////////////////
Django developers mostly concern themselves with the dynamic parts of web applications – the views and templates that render anew for each request. But web applications have other parts: the static files (images, CSS, Javascript, etc.) that are needed to render a complete web page.
For small projects, this isn’t a big deal, because you can just keep the static files somewhere your web server can find it. However, in bigger projects – especially those comprised of multiple apps – dealing with the multiple sets of static files provided by each application starts to get tricky.
That’s what django.contrib.staticfiles is for: it collects static files from each of your applications (and any other places you specify) into a single location that can easily be served in production.
///////////////////
Emphasis added
So if that's what django.contrib.staticfiles is for, what's the simpler solution? I dispute that this is a repeat of prior questions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要更仔细地阅读该文档。该警告针对生产。在开发中,您确实使用静态服务方法,即将其放入 urls.py 中。而且,该文档还将表明模板目录不是放置它们的正确位置:单独的静态或媒体目录才是。
评论后编辑我真的不明白你的评论。您可以通过静态服务视图在开发中执行此操作,也可以使用生产服务器。但你说你没有生产服务器。当您获得一台服务器时,无论是 Apache 还是 Nginx 还是其他什么,您都可以将静态文件放在一个目录中,并告诉该服务器从那里提供文件。这是简单的解决方案。静态文件应用程序,与您引用的文档完全相同,适用于不同应用程序中拥有大量文件的情况(它简化了从开发到生产的转变,而不是像您想象的那样使其复杂化)。
You need to read that documentation more closely. That warning is for production. In development, you do use that static-serving method, ie putting it in your urls.py. And, that documentation will also show that the templates directory is not the right place to put them: a separate static or media directory is.
Edit after comment I really don't understand your comment. Either you do it in development via the static serving view, or you use your production server. But you say you don't have a production server. When you get one, whether it's Apache or Nginx or whatever, you put your static files in a directory and tell that server to serve files from there. That is the simple solution. The staticfiles app, exactly as in the docs you quoted, are for when you've got lots of files in different apps (and it simplifies the move from development to production, not complicates it as you seem to think).
假设您的应用程序是 www.
就这样。
Suppose your app is www.
That's all.