0

I am new to Python on Django. I am making a simple website, but I have a problem with staticfile. I can make it load boostrap and some images on static folder, but it just work correctly when it is in index page, if I click to other page, staticfile is not working anymore. This is my setting.py. pls tell me what I have to do to solve this problem, thanks

This is my 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)
Ryan can fly
  • 123
  • 1
  • 11
  • 1. Please don't post screenshots; it's code, post the actual text. 2. That is not nearly enough information. Where is the code for the view? And the template? Are you in DEBUG mode? Have you run collectstatic? – Daniel Roseman Apr 10 '15 at 08:18
  • @DanielRoseman ok thanks, I fixed my post. I ran it on DEBUG mode. I ran collectstatic already – Ryan can fly Apr 10 '15 at 08:24

1 Answers1

2

Hi I use this (I am new in Django but it works for me):

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 %}

example of directory tree

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

Also look at THIS QUESTION.

Community
  • 1
  • 1
Trix
  • 587
  • 1
  • 6
  • 27