To deploy Django static files on Apache

Source: Internet
Author: User

have been optimizing the code of their own blog, yesterday, the CSS style sheet was separated, as a static

File, but didn't learn how Django uses static files, after some google

Finally solved.

There are two ways Django uses static files, one that is deployed on Django, and the other

is deployed on Apache, the previous method has a certain flaw, inefficient, and will create

To a certain security threat, I do not know, you can read the official documents, but still

Let me introduce you to you.

    1. You can build a static directory under your project and add it in settings.py:
      #填写你静态文件的绝对路径

Static_path = '/your Project path/static/'

    1. Open Url.py:import Settings

Then add in the Urlpatterns:

(r‘^static/(?P<path>.*)$‘,‘django.views.static.serve‘,                        {‘document_root‘:settings.STATIC_PATH})
    1. Finally, modify the template, where the static files are needed, modify the code to:

But I think the second method is more simple, a lot of information on the Internet is not very clear, I figured out

Here:

    1. Configure the files in the Mod_wsgi, which is your previously configured script:

If it's Ubuntu, that should be the http.conf file, and the Fedora Word is the wsgi.conf file

Just press divert, just like a static file that adds a Django admin interface, plus

Alias /static/ /usr/www/your project/static<Directory "/static/">    Order allow,deny    Options Indexes    Allow from all    IndexOptions FancyIndexing</Directory>

The code means that you build a static directory under your project's directory and then put your static files

Put it in there.

Finally, modify the place where you want to load the static files, such as the CCS stylesheet:

<link rel="stylesheet" type="text/css" href="/static/your css file" />

To deploy Django static files on Apache

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.