How to make my css files to work in django

后端 未结 2 461
深忆病人
深忆病人 2021-01-21 05:36

I am new to django but have some relative skills in python. I just started building a project in django and so far I have been able to create an app within my project. Also, I h

相关标签:
2条回答
  • 2021-01-21 05:57

    Thanks to all those that responded to my question, my static files are now active. I had to add RequestContext in my project view to make the {{STATIC_URL}} in the html file active.

    def index(request): return render_to_response('Calculator/index.html', context_instance=RequestContext(request))

    0 讨论(0)
  • 2021-01-21 06:04

    Just a small demo for very basic css/img usage with the development-server: python manage.py runserver. You should not use this for production. For the user input/output you should have a look at the tutorial.

    filestructure

    project
      |- static
      |   |- css
      |   |   |- stuff.css
      |- media
      |   |- images
      |   |   |- love.jpg
      |- templates
      |- urls.py
      |- __init__.py
      |- manage.py
      |- settings.py
    

    base.html

    <html>
    <head>
       <link href="{{ STATIC_URL }}css/stuff.css" />
    </head>
    <body>
       <h1>I like django!</h1>
       <img src="{{ MEDIA_URL }}images/love.jpg" />
    </body>
    </html>
    

    settings.py

    TEMPLATE_CONTEXT_PROCESSORS = (
        'django.contrib.auth.context_processors.auth',
        'django.core.context_processors.debug',
        'django.core.context_processors.i18n',
        'django.core.context_processors.media',
        'django.core.context_processors.static',
        'django.core.context_processors.request',
        'django.contrib.messages.context_processors.messages',
    )
    
    STATIC_URL = '/static/'
    MEDIA_URL = '/media/'
    

    urls.py

    # example windows
    (r'^media/(?P<path>.*)$', 'django.views.static.serve',
            {'document_root': 'C:/python25/lib/site-packages/project/media/', 'show_indexes': True}),
    # example *ix
    (r'^static/(?P<path>.*)$', 'django.views.static.serve',
            {'document_root': '/usr/lib/python25/site-packages/project/static/', 'show_indexes': True}),
    
    0 讨论(0)
提交回复
热议问题