简体   繁体   English

django 中的静态 url 路径设置

[英]static url path setting in django

I am new to django, presently I am learning and trying to build a basic site with django-cms.我是 django 的新手,目前我正在学习并尝试使用 django-cms 构建一个基本站点。 Below are my project folder structure and code files下面是我的项目文件夹结构和代码文件

project_folder
    manage.py 
    main_folder 
         static
             css
                new-styles.css
             js
                new-styles.js
         templates                      
             base.html
             home.html
         media
             pic_1.gif 
             .....
         settings.py
         urls.py
         ....... 

settings.py设置.py

import os
PROJECT_DIR = os.path.abspath(os.path.dirname(__file__))
DEBUG = True
MEDIA_ROOT = os.path.join(PROJECT_DIR,'media')
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(PROJECT_DIR,'static')
STATIC_URL = '/static/'
TEMPLATE_DIRS = (os.path.join(PROJECT_DIR,'templates'))
TEMPLATE_CONTEXT_PROCESSORS = (
       'django.contrib.auth.context_processors.auth',
       'django.core.context_processors.i18n',
       'django.core.context_processors.request',
       'django.core.context_processors.media',
       'django.core.context_processors.static',
       'django.core.context_processors.debug',
       'django.contrib.messages.context_processors.messages',
       'cms.context_processors.media',
       'sekizai.context_processors.sekizai',
)
......
.......

urls.py网址.py

from django.conf.urls import patterns, include, url
from django.conf import settings

urlpatterns = patterns('',
            url(r'^admin/', include(admin.site.urls)),
            url(r'^$', ...........),               
)

if settings.DEBUG:
    urlpatterns = patterns('',
    url(r'^media/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
    url(r'', include('django.contrib.staticfiles.urls')),
) + urlpatterns

base.html基本文件

{% load cms_tags sekizai_tags menu_tags %}
<html>
  <head>
    <title>{%block "title" %}{% endblock %}</title>
    <link rel="stylesheet" href="{{ STATIC_URL }}css/new-styles.css">
  {% render_block "css" %}
  </head> 
  <body>
  {% cms_toolbar %}
    {% block "base_content" %}
    {% endblock %}
  {% render_block "js" %}  
  </body>
</html>

home.html主页.html

{% extends "base.html" %}
{% load cms_tags %}
{% block "title" %}Welcome to Services{% endblock %}
{% block "base_content" %}
  <p>Hi this is paragraph</p>
  <img src="{{ MEDIA_URL }}images/promo3.jpg" />
 {% placeholder "number_one" %} 
 {% endblock %}

new-styles.css新样式.css

body {  
    padding:0px; margin:0px; margin-top:40px;
        background-color:#b0c4de;
     }
p {
      background-color:#e0ffff;
  }     

So above are my complete project structure and code files, but my actual problem is new-styles.css file is not working, even though I had written some css code in the file it is not linking to the template, so can anyone please let me know whats wrong and why base.html template file is unable to access the new-styles.css file, whether the path given in link tag is wrong or the path setting in the settings.py is wrong?所以上面是我完整的项目结构和代码文件,但我的实际问题是new-styles.css文件不起作用,即使我在文件中写了一些 css 代码它没有链接到模板,所以任何人都可以让我知道出了什么问题,为什么base.html模板文件无法访问new-styles.css文件,是link标签中给出的路径错误还是settings.py的路径设置错误?

urls.py网址.py

from django.conf.urls import patterns, include, url
from django.conf.urls.static import static
from django.contrib import admin
from django.contrib.staticfiles.urls import staticfiles_urlpatterns

from project_name import settings

admin.autodiscover()

urlpatterns = patterns('',
    .....................
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

urlpatterns += staticfiles_urlpatterns()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM