如何在Django应用中使用bower包管理器?

时间:2021-12-28 11:03:19

I'm new to Django framework and i have read that that the 'static' files like css and js must be inside the 'static' directory, but my question is:

我是Django框架的新手,我读到过像css和js这样的“静态”文件必须在“静态”目录中,但是我的问题是:

Given that bower package manager install its dependencies on a new directory called bower_components in the current directory, the bower.json must be created on the 'static' django directory? and if it is true, is not bower.json exported with the collectstatic command? (something might not wanted)

假设bower包管理器将其依赖项安装在当前目录中名为bower_components的新目录下,那么该目录就是bower。必须在“静态”django目录上创建json ?如果这是真的,那就不是鲍尔。用collectstatic命令导出的json ?(可能不希望)

Which is the recommended way to work with bower and Django framework?

使用bower和Django框架的推荐方式是什么?

Update:

更新:

Thanks Yuji 'Tomita' Tomita, your answer can give more perspective. I want to use bower just to manage front end dependencies like jQuery, bootstrap and so on, as you see, by logic must be inside de static/ django directory, but do it that way, can cause to the bower.json be treated as a static resource, something might not wanted.

感谢“Tomita”Yuji,你的回答可以提供更多的视角。我想使用bower来管理前端依赖,如jQuery、bootstrap等,正如您所看到的,逻辑上必须在de static/ django目录中,但是这样做,会导致bower。json被视为一种静态资源,有些东西可能不需要。

5 个解决方案

#1


6  

I followed this blog post to setup my django bower project:

我跟随这篇博文来建立我的django bower项目:

Project structure:

项目结构:

|-root
  |-app
     |-assets
     |-static
     |-templates
     |settings.py
     |urls.py
     |views.py
     |wsgi.py
  |manage.py
  |bower.json
  |.bowerrc

My .bowerrc:

我的.bowerrc:

{
    "directory": "app/static/bower_components"
}

And I user bower components like this:

我的用户是这样的:

<script src="{{ STATIC_URL }}bower_components/angular/angular.js"></script>

My settings.py:

我的settings.py:

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

Also urls.py:

还urls . py:

urlpatterns += patterns('',
                        (r'^static/(?P<path>.*)$', 'django.views.static.serve',
                         {'document_root': settings.STATIC_ROOT}),)

#2


2  

There is no recommended way - it depends on your project. If you are using bower, node for more than the django project, it might make sense to place it in your project root (above django) so that it may be reused elsewhere.

没有推荐的方法——这取决于你的项目。如果您正在使用bower, node,而不是django项目,那么将它放在项目根(django之上)中可能是有意义的,以便它可以在其他地方重用。

If it's purely for django's static files, then it might make sense to place it in a src/ outside of the staticfiles system which builds to the static directory which is exported via collectstatic.

如果只是针对django的静态文件,那么可以将它放在src/ staticfiles系统之外,该系统构建到通过collectstatic导出的静态目录中。

#3


2  

You should list the installed bower packages in the settings.py using key BOWER_INSTALLED_APPS.

您应该在设置中列出已安装的bower包。py BOWER_INSTALLED_APPS使用钥匙。

Now, in your development server, using the {% static %} templatetag finds them from their installed directory. In production server, the collectstatic will collect the correct static files from the installed directory (bower_components).

现在,在您的开发服务器中,使用{% static %} templatetag从其已安装的目录中查找它们。在生产服务器中,collectstatic将从已安装的目录(bower_components)收集正确的静态文件。

See more: http://django-bower.readthedocs.org/en/latest/usage.html

看到更多:http://django-bower.readthedocs.org/en/latest/usage.html

#4


2  

If you're afraid of the bower.json being included, the collectstatic command has an --ignore option that you can use to exclude whatever you want.

如果你害怕凉亭。包含json的collectstatic命令有一个——ignore选项,您可以使用它排除您想要的任何东西。

#5


2  

There is no need for apps like django-bower, or other specialized tools that take up server resources, slow build time, and greatly limit the usefulness of bower. Especially, when you have nested django apps with their own bower dependancies.

不需要像django-bower这样的应用程序,也不需要其他专门的工具来占用服务器资源、降低构建时间、极大地限制bower的用处。特别是,当您已经将django应用程序嵌套到它们自己依赖于bower的应用程序中时。

You can check out my tutorial on how to seamlessly integrate Django + Bower + Heroku here. Although this tutorial targets heroku, the methodology applies to any deployment scenario.

您可以在这里查看我关于如何无缝集成Django + Bower + Heroku的教程。尽管本教程针对的是heroku,但是该方法适用于任何部署场景。

#1


6  

I followed this blog post to setup my django bower project:

我跟随这篇博文来建立我的django bower项目:

Project structure:

项目结构:

|-root
  |-app
     |-assets
     |-static
     |-templates
     |settings.py
     |urls.py
     |views.py
     |wsgi.py
  |manage.py
  |bower.json
  |.bowerrc

My .bowerrc:

我的.bowerrc:

{
    "directory": "app/static/bower_components"
}

And I user bower components like this:

我的用户是这样的:

<script src="{{ STATIC_URL }}bower_components/angular/angular.js"></script>

My settings.py:

我的settings.py:

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

Also urls.py:

还urls . py:

urlpatterns += patterns('',
                        (r'^static/(?P<path>.*)$', 'django.views.static.serve',
                         {'document_root': settings.STATIC_ROOT}),)

#2


2  

There is no recommended way - it depends on your project. If you are using bower, node for more than the django project, it might make sense to place it in your project root (above django) so that it may be reused elsewhere.

没有推荐的方法——这取决于你的项目。如果您正在使用bower, node,而不是django项目,那么将它放在项目根(django之上)中可能是有意义的,以便它可以在其他地方重用。

If it's purely for django's static files, then it might make sense to place it in a src/ outside of the staticfiles system which builds to the static directory which is exported via collectstatic.

如果只是针对django的静态文件,那么可以将它放在src/ staticfiles系统之外,该系统构建到通过collectstatic导出的静态目录中。

#3


2  

You should list the installed bower packages in the settings.py using key BOWER_INSTALLED_APPS.

您应该在设置中列出已安装的bower包。py BOWER_INSTALLED_APPS使用钥匙。

Now, in your development server, using the {% static %} templatetag finds them from their installed directory. In production server, the collectstatic will collect the correct static files from the installed directory (bower_components).

现在,在您的开发服务器中,使用{% static %} templatetag从其已安装的目录中查找它们。在生产服务器中,collectstatic将从已安装的目录(bower_components)收集正确的静态文件。

See more: http://django-bower.readthedocs.org/en/latest/usage.html

看到更多:http://django-bower.readthedocs.org/en/latest/usage.html

#4


2  

If you're afraid of the bower.json being included, the collectstatic command has an --ignore option that you can use to exclude whatever you want.

如果你害怕凉亭。包含json的collectstatic命令有一个——ignore选项,您可以使用它排除您想要的任何东西。

#5


2  

There is no need for apps like django-bower, or other specialized tools that take up server resources, slow build time, and greatly limit the usefulness of bower. Especially, when you have nested django apps with their own bower dependancies.

不需要像django-bower这样的应用程序,也不需要其他专门的工具来占用服务器资源、降低构建时间、极大地限制bower的用处。特别是,当您已经将django应用程序嵌套到它们自己依赖于bower的应用程序中时。

You can check out my tutorial on how to seamlessly integrate Django + Bower + Heroku here. Although this tutorial targets heroku, the methodology applies to any deployment scenario.

您可以在这里查看我关于如何无缝集成Django + Bower + Heroku的教程。尽管本教程针对的是heroku,但是该方法适用于任何部署场景。