简体   繁体   English

html和静态文件Django的Settings.py和路径

[英]Settings.py and paths for html and static files Django

I am using Django 1.8 and I am having trouble displaying my JavaScript and CSS when I run the development Django server. 我正在使用Django 1.8,并且在运行开发Django服务器时无法显示我的JavaScript和CSS。 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. 我已经注意到,创建Django项目和应用程序时,文件夹/文件路径的创建方式与其他路径不同。 What would I make my STATIC_ROOT and STATICFILES_DIR be assigned to? 我将STATIC_ROOT和STATICFILES_DIR分配给什么?

Settings.py: 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: 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: 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. 我也不知道将JSON文件放在哪里,因为我正在使用jquery / javascript从JSON文件中获取数据。 Also, my javascript code is embedded within my html file, is that okay or do I need to make a separate javascript file? 另外,我的javascript代码嵌入在html文件中,可以吗?还是需要制作一个单独的javascript文件?

Any help would be much appreciated. 任何帮助将非常感激。 Thank you. 谢谢。

MADE UPDATES. 进行了更新。

Okay, answering your question in the reverse order of your questions. 好的,按照与问题相反的顺序回答您的问题。

Firstly, its always a better convention and practice to keep your html, javascript and css in separate files. 首先,将html,javascript和css放在单独的文件中始终是更好的惯例。 So all your HTML files goes in the templates directory and the javascript and CSS in js and css directory inside static directory. 因此,所有HTML文件都放在模板目录中,而javascript和CSS则在静态目录中的js和css目录中。

Now, I don't think you'll be requiring to access some json file everytime your web-app is loaded. 现在,我认为您不需要在每次加载Web应用程序时访问一些json文件。 If it is, then again its something wrong as for that purpose, you have your database. 如果是的话,那么出于此目的又有问题,您就拥有了数据库。 So move that data to your DB. 因此,将数据移至您的数据库。 Else if it is for one time purpose, say data upload, then you can keep it inside a directory say json inside the static directory. 否则,如果它是一次性的,比如说数据上传,那么您可以将其保存在静态目录中的json目录中。

Now lastly, coming to your main question of STATIC_ROOT vs STATIC_DIRS vs STATIC_URL, then there already is an excellent explanation of the same here-> Differences between STATICFILES_DIR, STATIC_ROOT and MEDIA_ROOT 现在,最后,谈到您的STATIC_ROOT与STATIC_DIRS与STATIC_URL的主要问题,那么这里已经有一个很好的解释-> STATICFILES_DIR,STATIC_ROOT和MEDIA_ROOT之间的区别

plus you need to rename your 'staticfiles' directory to 'static' as in your settings.py you have mentioned the STATIC_URL = '/static/' and not /'staticfiles'/ or else you can change the STATIC_URL = /'staticfiles'/, but the former is a followed convention, so try stick to that. 另外,您需要像在settings.py中那样将“ staticfiles”目录重命名为“ static”。您提到STATIC_URL ='/ static /'而不是/'staticfiles'/,否则您可以更改STATIC_URL = /'staticfiles' /,但前者是遵循的约定,因此请坚持该原则。

This is the solution to your problem of reading data from a json file using your javascript in general ie, it is not dependent on django or php or something like that. 通常,这是解决您使用javascript从json文件读取数据的问题的解决方案,即,它不依赖django或php或类似的东西。

function getAllSupportedItems( ) {
    return $.getJSON("staticfiles/json_folder/name_of_your_json_file.json").then(function (data) {
        return data.items;
    });
}

getAllSupportedItems().done(function (items) {
    // you have your items here
});

See this for a better explanation -> Using Jquery to get JSON objects from local file 请参阅此以获得更好的解释-> 使用Jquery从本地文件获取JSON对象

One more thing, which you might get stuck if you overcome this problem. 还有一件事,如果克服了这个问题,您可能会陷入困境。

In your settings.py you have mentioned your STATIC_URL = '/static/' whereas you've named that folder as 'staticfiles'. 在settings.py中,您提到了STATIC_URL ='/ static /',而您将该文件夹命名为'staticfiles'。 So either you rename your 'staticfiles' to 'static' or vice-versa. 因此,您可以将“ staticfiles”重命名为“ static”,反之亦然。 Though former is the convention, so rename it to 'static' else your settings.py will look for a folder named 'static' in the BASE Directory, but it will never find one as you have named it 'staticfiles' 尽管前者是惯例,所以将其重命名为“ static”,否则您的settings.py将在BASE Directory中查找名为“ static”的文件夹,但由于您将其命名为“ staticfiles”,因此它将永远找不到。

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

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