staticfile不能在其他页面上工作,而是索引

时间:2015-04-10 08:14:57

标签: python django

我是Django上的Python新手。我正在建立一个简单的网站,但我有一个静态文件的问题。我可以让它在静态文件夹上加载boostrap和一些图像,但它只是在索引页面中正常工作,如果我点击其他页面,静态文件不再工作。这是我的setting.py。请告诉我要解决这个问题需要做些什么,谢谢

这是我的setting.py:

STATIC_URL = '/static/'

STATIC_ROOT = '/static/static_root/'

TEMPLATE_DIRS = (
    os.path.join(BASE_DIR, 'templates'),
)

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
    '/static/',
)

views.py:

def index(request):
    list_post_Anh = Blog.objects.filter(category_id=1).order_by('-posted')
    list_post_Uc = Blog.objects.filter(category_id=2).order_by('-posted')
    list_post_Ca = Blog.objects.filter(category_id=3).order_by('-posted')
    list_post_Us = Blog.objects.filter(category_id=4).order_by('-posted')

    context = {'listbloganh':list_post_Anh, 'listbloguc':list_post_Uc, 'listblogca':list_post_Ca, 'listblogus':list_post_Us}
    return render(request, 'index.html', context)

def view_post(request, slug):
    getpost = get_object_or_404(Blog, slug=slug)
    context = {'post':getpost}
    return render(request, 'content.html', context)

urls.py:

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'WebDuHoc.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),

    url(r'^admin/', include(admin.site.urls)),
    url(r'^$', 'blog.views.index', name='index'),
    url(r'^baiviet/(?P<slug>[a-zA-Z0-9_-]+)/$', 'blog.views.view_post', name='content'),
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

1 个答案:

答案 0 :(得分:2)

嗨,我使用这个(我是Django中的新手,但它对我有用):

<强> settings.py

"""
    Django settings for simplesite project.

    For more information on this file, see
    https://docs.djangoproject.com/en/1.7/topics/settings/

    For the full list of settings and their values, see
    https://docs.djangoproject.com/en/1.7/ref/settings/
    """

    # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
    import os
    import simplesite
    BASE_DIR = os.path.dirname(os.path.dirname(__file__))

    TEMPLATE_DIRS = (
                     os.path.join(os.path.dirname(__file__), "templates").replace ('\\','/'),
    )



    # Quick-start development settings - unsuitable for production
    # See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/

    # SECURITY WARNING: keep the secret key used in production secret!
    SECRET_KEY = '8x*)8mtzd86&634n=kt!gr=9m**916trc&wd*qb(4uvgpcad3n'

    # SECURITY WARNING: don't run with debug turned on in production!
    DEBUG = True

    TEMPLATE_DEBUG = True


    ALLOWED_HOSTS = []


    # Application definition

    INSTALLED_APPS = (
        'bootstrap3',              
        'django_admin_bootstrapped.bootstrap3',
        'django_admin_bootstrapped',         
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'homepage',
        'simpleapp',
    )

    MIDDLEWARE_CLASSES = (
        'django.contrib.sessions.middleware.SessionMiddleware',
        'django.middleware.common.CommonMiddleware',
        'django.middleware.csrf.CsrfViewMiddleware',
        'django.contrib.auth.middleware.AuthenticationMiddleware',
        'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
        'django.contrib.messages.middleware.MessageMiddleware',
        'django.middleware.clickjacking.XFrameOptionsMiddleware',
        'django.middleware.locale.LocaleMiddleware',
    )

    # http://django-bootstrap3.readthedocs.org/en/latest/settings.html
    BOOTSTRAP3= {

                'base_url': '/static/bootstrap3',
                'css_url': '/static/bootstrap3/css/bootstrap.min.css',

    }


    SESSION_EXPIRE_AT_BROWSER_CLOSE = True

    ROOT_URLCONF = 'simplesite.urls'

    WSGI_APPLICATION = 'simplesite.wsgi.application'


    # Database
    # https://docs.djangoproject.com/en/1.7/ref/settings/#databases

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
        }
    }

    # Internationalization
    # https://docs.djangoproject.com/en/1.7/topics/i18n/
    # default language, it will be used, if django can't recognize user's language
    LANGUAGE_CODE = 'en-us'

    # list of activated languages
    LANGUAGES = (
        ('en', 'English'),
        ('it', 'Italian'),
    )


    TIME_ZONE = 'UTC'

    # enable django’s translation system
    USE_I18N = True

    USE_L10N = True

    USE_TZ = True


    # specify path for translation files
    LOCALE_PATHS = (
        os.path.join(os.path.dirname(__file__), 'locale'), 
    )


    # Static files (CSS, JavaScript, Images)
    # https://docs.djangoproject.com/en/1.7/howto/static-files/
    STATICFILES_DIRS = (
        os.path.join(BASE_DIR, "static"),
    )



    STATIC_URL = '/static/'

<强> urls.py

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


admin.autodiscover()


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

<强> some.html

{% extends 'base.html' %}
{% load staticfiles %}
{%block content%}
...
<script  src="{% static 'jquery-1.11.2.js' %}"></script>  //here I call js files which I put in static folder
<script src="{% static 'js/conta.js' %}"></script>
<a href="{% url 'conta' %}" class="btn btn-primary" role="button" id="btnGo">GO</a><div id="loading"></div>
...
{% endblock content %}

目录树的示例

MyProject
    -MyProject
        -settings.py
        -...
    -MyApp
        - ...
    -static
        -js
           -...
    -manage.py
    -...

另请参阅THIS QUESTION