A simple example of Django and CSS

前端 未结 5 2031

I\'m new to Django, and am trying to set up a really simple Django app.

Now, I\'m up to chapter 5 in the Django online book : http://www.djangobook.com/en/2.0/chapte

5条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-08 03:16

    For the examples you pointed at to work, your static files need to be in a location accessible to Django's built-in staticfiles app.

    There are a couple steps to make this happen:

    First, within your project directory (ie beside your manage.py file), you'll need to create a directory to hold your static files. Call it "static_files".

    Next, you'll need to let Django know to look in that directory, by specifying it in the list of STATICFILES_DIRS within your settings.py file.

    Something like this:

    STATICFILES_DIRS = [
        '/full/path/to/your/project/static_files/',
    ]
    

    Within that static_files directory, you can create whatever structure you want, so that is where your css and js directories could go.

    After that, you should be able to use the {{ STATIC_URL }} tag in your templates to get access to the base URL of your static files.

    So, say, for example, you create project/static_files/css/base.css, you would use it in your template like so:

    
    

    Hope that helps!

    Edit

    With the default settings for STATICFILES_FINDERS, Django should automatically serve up any files from directories listed in your STATICFILES_DIRS -- see the docs for details.

    If this doesn't work, some things to check:

    • Have you edited your STATICFILES_FINDERS setting to something other than the default?
    • Is django.contrib.staticfiles in your list of INSTALLED_APPS in settings.py?
    • Are you using Django's built-in server (python manage.py runserver)?
    • Do you have DEBUG = True in your settings.py? If not, you'll need to either set it to True or use the insecure option (python manage.py runserver --insecure). When going to production, check out the collectstatic command.

提交回复
热议问题