Django中的静态文件管理过程解析

作者:再见紫罗兰 时间:2021-01-21 03:09:14 

Static files管理

static files指一些用到的像css,javascript,images之类的文件。

在开发阶段:

1.在settings设置INSTALLED_APPS中添加'django.contrib.staticfiles'。

2.将STATIC_URL设置为'/static/'。

3.将某个app要用的静态文件放置到my_app/static/my_app中,例如my_app/static/my_app/my_image.jpg.

当然也可以直接放在my_app/static中,但这样的话,如果在不同的app中有相同名字的静态文件,就会产生冲突。

4.模板中使用


{% load static %}
<img src="{% static 'my_app/myexample.jpg' %}" alt="My image"/>

5.如果有些不是某个app特用的静态文件,可以建立static文件夹将静态文件放置其中,settings设置:


STATICFILES_DIRS = (
 os.path.join(BASE_DIR, "static"),
 '/var/www/static/',
)

这样,在开发环境中,settings.DEBUG设置为True时,运行runserver就可以访问静态文件了。

如果INSTALLED_APPS中没有包含'django.contrib.staticfiles',需要手动运行django.contrib.staticfiles.views.serve()。


from django.conf import settings
from django.contrib.staticfiles import views

if settings.DEBUG:
 urlpatterns += [
   url(r'^static/(?P<path>.*)$', views.serve),
 ]

或者


from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
 # ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

以上都在DEBUG设置为True时起作用。

在生产环境中,就需要使用反向代理服务器直接访问静态文件,需要将静态文件转移到代理服务器可以访问的文件夹,设置


STATIC_ROOT = "/var/www/example.com/static/"

然后运行


python manage.py collectstatic

将各个app内的静态文件及STATICFILES_DIRS内的静态文件收集起来放置到STATIC_ROOT中由服务器apache或nhinx管理即可。

Media管理

MEDIA:指用户上传的文件,比如在Model里面的FileFIeld,ImageField上传的文件。

假如有个Model


from django.db import models
class Car(models.Model):
 name = models.CharField(max_length=255)
 price = models.DecimalField(max_digits=5, decimal_places=2)
 photo = models.ImageField(upload_to='cars')

设置MEDIA_ROOT=os.path.join(BASE_DIR , 'media'),用来存储用户上传的文件

MEDIA_URL=/media/,为MEDIA_ROOT中的文件建立url地址。

当建立一个Car实例时,Car的ImageField字段图片就存到media/cars文件夹里面


>>> car = Car.objects.get(name="57 Chevy")
>>> car.photo
<ImageFieldFile: chevy.jpg>
>>> car.photo.name
u'cars/chevy.jpg'
>>> car.photo.path
u'/media/cars/chevy.jpg'
>>> car.photo.url
u'/media/cars/chevy.jpg'

在模板中使用图片


<img src="{{ car.photo.url }}" alt="My image"/>

在urls.py中使用 django.contrib.staticfiles.views.serve() view


from django.conf import settings #from myapp import settings
from django.conf.urls.static import static

urlpatterns = patterns('',
 # ... the rest of your URLconf goes here ...
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

这样就可以使用media文件了。

来源:https://www.cnblogs.com/linxiyue/p/3677405.html

标签:django,静态,文件,管理
0
投稿

猜你喜欢

  • python数字图像处理之高级形态学处理

    2021-08-18 15:55:42
  • Python使用psutil对系统数据进行采集监控

    2023-03-13 07:21:19
  • asp如何调用DLL来加快服务器的执行速度?

    2009-11-15 20:07:00
  • 8段用于数据清洗Python代码(小结)

    2023-10-01 06:04:25
  • Mootools 1.2教程(13)——正则表达式

    2008-12-07 20:25:00
  • python制作的天气预报小工具(gui界面)

    2022-04-03 17:20:42
  • python time.strptime格式化实例详解

    2022-03-25 19:13:02
  • Keras设置以及获取权重的实现

    2021-11-22 10:04:09
  • PHP实现的登录,注册及密码修改功能分析

    2023-11-14 21:45:29
  • 利用python在大量数据文件下删除某一行的例子

    2023-08-24 09:15:22
  • Python实现快速保存微信公众号文章中的图片

    2021-02-18 23:03:25
  • JS语法检查插件 jsLint for Vim

    2010-11-15 21:31:00
  • Python偏函数实现原理及应用

    2022-12-13 17:12:03
  • python版本单链表实现代码

    2022-12-06 16:49:26
  • 跟老齐学Python之字典,你还记得吗?

    2022-09-07 20:22:53
  • Python基础之logging模块知识总结

    2021-12-20 04:20:07
  • wxPython之解决闪烁的问题

    2022-05-12 13:21:30
  • ASP使用wsImage组件给图片加水印代码

    2010-06-09 19:23:00
  • 详细了解 MySQL锁机制

    2010-08-08 09:04:00
  • 基于Python列表解析(列表推导式)

    2021-11-14 16:31:36
  • asp之家 网络编程 m.aspxhome.com