从Django开发服务器的根服务提供静态文件

时间:2023-02-05 12:46:20

My goal is to have an Angular project being served from the root of my development server. The files will be completely static as far as Django is concerned, no Django template processing is needed. The angular project will then make resource calls to a Django project located at /api/ on the same development server, which will then return json results generated from a view for the Angular project to process.

我的目标是从我的开发服务器的根目录中提供Angular项目。就Django而言,文件将是完全静态的,不需要Django模板处理。然后,角度项目将对位于同一开发服务器上的/ api /的Django项目进行资源调用,然后将返回从Angular项目的视图生成的json结果进行处理。

I assumed it would be as easy as adding the following to my urls.py file.

我认为这就像在urls.py文件中添加以下内容一样简单。

url(r'^/', 'django.views.static.serve', {
        'document_root':'/Users/kyle/Development/project/site/app',
}),

Or

+ static("^/$", document_root="/Users/kyle/Development/project/site/app")

To the end of the urlpatterns.

到urlpatterns的结尾。

With /project/site/app being the directory with the Angularjs files.

/ project / site / app是Angularjs文件的目录。

However, both of these leave me with 404 errors.

但是,这两个都让我有404错误。

I'm open to changing the structure of the project if a more obvious solution exists.

如果存在更明显的解决方案,我愿意改变项目的结构。

3 个解决方案

#1


1  

  1. note that by default Django won't serve a directory listing. Do you still get a 404 if file /Users/kyle/Development/project/site/app/beer.jpg doesn't appear as http://localhost/beer.jpg ?

    请注意,默认情况下,Django不会提供目录列表。如果文件/Users/kyle/Development/project/site/app/beer.jpg没有显示为http://localhost/beer.jpg,你还得到404吗?

  2. in urls.py URLs don't start with a slash; compare url(r'beer') with url(r'^/beer')

    在urls.py中,URL不以斜杠开头;将url(r'beer')与url(r'^ / beer')进行比较

I suggest just going for the standard STATIC support. It's awkward, but lets you serve file simply during development, and switch to a 3rd party server (ie Nginx) for production:

我建议你去寻找标准的STATIC支持。这很尴尬,但是让你在开发过程中简单地提供文件,并切换到第三方服务器(即Nginx)进行生产:

https://docs.djangoproject.com/en/dev/howto/static-files/

#2


5  

You need to serve both index.html and your static files on / which is done like this in Django 1.10:

您需要同时提供index.html和静态文件/在Django 1.10中这样做:

from django.contrib.staticfiles.views import serve
from django.views.generic import RedirectView

urlpatterns = [

    # / routes to index.html
    url(r'^$', serve,
        kwargs={'path': 'index.html'}),

    # static files (*.css, *.js, *.jpg etc.) served on /
    url(r'^(?!/static/.*)(?P<path>.*\..*)$',
        RedirectView.as_view(url='/static/%(path)s')),
]

See this answer where I wrote a more complete explanation of such a configuration – especially if you want to use it for production.

看到这个答案,我写了一个更完整的解释,特别是如果你想用它来生产。

#3


3  

It turned out that it was a combination of 2 things, as shavenwarthog said, it shouldn't have the slash. Also, it needed a regular expression to direct it to the file. The final line ended up being:

事实证明,这是两件事的组合,正如shavenwarthog所说,它不应该有斜线。此外,它需要一个正则表达式来将其定向到文件。最后一行最终成为:

url(r'^(?P<path>.*)$', 'django.views.static.serve', {
        'document_root':'/Users/kyle/Development/project/site/app',
}),

I can then access files like

然后我可以访问像这样的文件

http://localhost/beer.jpg

#1


1  

  1. note that by default Django won't serve a directory listing. Do you still get a 404 if file /Users/kyle/Development/project/site/app/beer.jpg doesn't appear as http://localhost/beer.jpg ?

    请注意,默认情况下,Django不会提供目录列表。如果文件/Users/kyle/Development/project/site/app/beer.jpg没有显示为http://localhost/beer.jpg,你还得到404吗?

  2. in urls.py URLs don't start with a slash; compare url(r'beer') with url(r'^/beer')

    在urls.py中,URL不以斜杠开头;将url(r'beer')与url(r'^ / beer')进行比较

I suggest just going for the standard STATIC support. It's awkward, but lets you serve file simply during development, and switch to a 3rd party server (ie Nginx) for production:

我建议你去寻找标准的STATIC支持。这很尴尬,但是让你在开发过程中简单地提供文件,并切换到第三方服务器(即Nginx)进行生产:

https://docs.djangoproject.com/en/dev/howto/static-files/

#2


5  

You need to serve both index.html and your static files on / which is done like this in Django 1.10:

您需要同时提供index.html和静态文件/在Django 1.10中这样做:

from django.contrib.staticfiles.views import serve
from django.views.generic import RedirectView

urlpatterns = [

    # / routes to index.html
    url(r'^$', serve,
        kwargs={'path': 'index.html'}),

    # static files (*.css, *.js, *.jpg etc.) served on /
    url(r'^(?!/static/.*)(?P<path>.*\..*)$',
        RedirectView.as_view(url='/static/%(path)s')),
]

See this answer where I wrote a more complete explanation of such a configuration – especially if you want to use it for production.

看到这个答案,我写了一个更完整的解释,特别是如果你想用它来生产。

#3


3  

It turned out that it was a combination of 2 things, as shavenwarthog said, it shouldn't have the slash. Also, it needed a regular expression to direct it to the file. The final line ended up being:

事实证明,这是两件事的组合,正如shavenwarthog所说,它不应该有斜线。此外,它需要一个正则表达式来将其定向到文件。最后一行最终成为:

url(r'^(?P<path>.*)$', 'django.views.static.serve', {
        'document_root':'/Users/kyle/Development/project/site/app',
}),

I can then access files like

然后我可以访问像这样的文件

http://localhost/beer.jpg