I am using Django 1.8 and I am having trouble displaying my JavaScript and CSS when I run the development Django server. I think the main problem is not having a good understanding of what to set my static root and static root dir as. I have noticed that my folder/file paths are created differently compared to others when I create a Django project and app. What would I make my STATIC_ROOT and STATICFILES_DIR be assigned to?
Settings.py:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(_file_)))
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'Myapp'
)
ROOT_URLCONF = 'MyProject.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates' )],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
MEDIA_ROOT = os.path.join(BASE_DIR, "media/")
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static")
urls.py:
from django.conf.urls import include, url
from django.contrib import admin
from Myapp.views import startpage
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
# Examples:
# url(r'^$', 'MyProject.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^$', startpage)
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
views.py:
from django.shortcuts import render, render_to_response
# Create your views here.
def startpage(request):
return render_to_response('htmlpage.html');
Here is my current directories:
C:\Users\Me\DjangoProjects\
|-----MyProject\
|------MyApp\
|----_init_.py
|----admin.py
|----models.py
|----tests.py
|----views.py
|----_pycache_\
|----migrations\
|------MyProject\
|----_init_.py
|----settings.py
|----urls.py
|----wsgi.py
|----_pycache_\
|------staticfiles\
|------templates\
|----htmlpage.html
|------db.sqlite3
|------manage.py
I also don't know where I would put my JSON file, because I am using jquery/javascript to grab data from the JSON file. Also, my javascript code is embedded within my html file, is that okay or do I need to make a separate javascript file?
Any help would be much appreciated. Thank you.
MADE UPDATES.