2016-04-14 71 views
0

我仔細閱讀了documentation以及與該主題相關的多個討論,但仍無法確定降價停止的位置。我只想在Django視圖中打開一個靜態文件,向其中寫入一些內容並將其保存使用Django - 從服務器打開靜態文件

f = open('/static/egais_files/temp.txt', 'w') 

方法。

我的設置:

""" 
Django settings for supermarket_project project. 

Generated by 'django-admin startproject' using Django 1.9.1. 

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

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

#for gmail or goole apps 
from supermarket_project.email_info import EMAIL_USE_TLS,EMAIL_HOST,EMAIL_HOST_USER,EMAIL_HOST_PASSWORD,EMAIL_PORT 
from django.conf.global_settings import ALLOWED_HOSTS 

#What Tigran wrote --------------------------------------------------------------------------------- 
EMAIL_USE_TLS = EMAIL_USE_TLS 
EMAIL_HOST = EMAIL_HOST 
EMAIL_HOST_USER = EMAIL_HOST_USER 
EMAIL_HOST_PASSWORD = EMAIL_HOST_PASSWORD 
EMAIL_PORT = EMAIL_PORT 
#What Tigran wrote END ------------------------------------------------------------------------------ 


import os 
#from django.conf.global_settings import TEMPLATE_DEBUG 

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


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

# SECURITY WARNING: keep the secret key used in production secret! 
SECRET_KEY = 'pvpsr%xjy3ki8ecut2&x=!+&(1q*=u9=(tnz8o371m^%^jntn&' 

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

#ALLOWED_HOSTS= ['*'] 

# Application definition 

INSTALLED_APPS = [ 
    'django.contrib.admin', 
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.messages', 
    'django.contrib.staticfiles', 
    'person', 
    'legal_entity', 
    'additional', 
    'address', 
    'employee', 
    'agreement', 
    'report', 
    'vendor', 
    'bootstrap3_datetime', 
    'computer_repair', 
    'task', 
    'egais', 
] 

MIDDLEWARE_CLASSES = [ 
    'django.middleware.security.SecurityMiddleware', 
    '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', 
] 

ROOT_URLCONF = 'supermarket_project.urls' 
#ANONYMOUS_USER_ID=-1 
TEMPLATES = [ 
    { 
     'BACKEND': 'django.template.backends.django.DjangoTemplates', 
     'DIRS': [os.path.join(os.path.dirname(BASE_DIR),"static","templates")], 
     #'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', 
      ], 
     }, 
    }, 
] 

WSGI_APPLICATION = 'supermarket_project.wsgi.application' 
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__)) 

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

#=============================================================================== 
# DATABASES = { 
#  'default': { 
#   'ENGINE': 'django.db.backends.sqlite3', 
#   'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), 
#  } 
# } 
#=============================================================================== 
DATABASES = { 
    'default': { 
     'ENGINE': 'django.db.backends.mysql', 
     'NAME': 'supermarkets_schema', 
     'USER': 'edgar', 
     'PASSWORD': '123', 
     'HOST': '10.8.0.1', # Or an IP Address that your DB is hosted on 
     'PORT': '3306', 
     'default-character-set': 'utf8', 
    }, 
    'computer_repair': { 
     'ENGINE': 'django.db.backends.mysql', 
     'NAME': 'computer_repair', 
     'USER': 'edgar', 
     'PASSWORD': '123', 
     'HOST': '10.8.0.1', # Or an IP Address that your DB is hosted on 
     'PORT': '3306', 
     'default-character-set': 'utf8', 
    } 
} 
#DATABASES = { 
# 'default': { 
#  'ENGINE': 'django.db.backends.mysql', 
#  'NAME': 'test', 
#  'USER': 'root', 
#  'PASSWORD': '1234', 
# } 
#} 

# Password validation 
# https://docs.djangoproject.com/en/1.9/ref/settings/#auth-password-validators 

AUTH_PASSWORD_VALIDATORS = [ 
    { 
     'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 
    }, 
    { 
     'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 
    }, 
    { 
     'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 
    }, 
    { 
     'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 
    }, 
] 


# Internationalization 
# https://docs.djangoproject.com/en/1.9/topics/i18n/ 

LANGUAGE_CODE = 'ru' 

TIME_ZONE = 'Europe/Moscow' 

USE_I18N = True 

USE_L10N = True 

USE_TZ = False 


# Static files (CSS, JavaScript, Images) 
# https://docs.djangoproject.com/en/1.9/howto/static-files/ 

STATIC_URL = '/static/' 

if DEBUG: 
    MEDIA_URL = '/media/' 
    STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR),"static","static") 
#STATIC_ROOT = [os.path.join(BASE_DIR,"static-only")] 
    MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR),"static","media") 
#MEDIA_ROOT = [os.path.join(BASE_DIR,"media")] 
    STATICFILES_DIRS = (
         os.path.join(os.path.dirname(BASE_DIR),"static","static"), 
    ) 

看起來一切都對應於什麼文件說。請幫幫我 !錯誤在哪裏?

回答

0

要打開文件,您必須將文件的路徑傳遞到open。你傳遞的網址。

它應該是這樣的:

f = open(os.path.join(STATIC_ROOT,'egais_files/temp.txt'), 'w') 
+0

謝謝。它不能識別os.join,儘管事實上我輸入了os –

+0

對不起,應該是'os.path.join'(就像在你的settings.py中) – ilse2005

+0

不行,沒有工作[錯誤2]沒有這樣的文件或目錄: '\\\\ 10.8.0.1 \\ share/egais_files/temp.txt \\ w' –

0

在初始代碼段你試圖打開一個文件中的子目錄註銷文件系統的根目錄。你需要確保兩件事。首先是您正在寫入正確的位置。很可能您需要將打開的語句更改爲絕對路徑。在嘗試創建文件之前,還需要確保STATIC_ROOT/egais_files存在。 對於Python 3:

import os 
import os.path 
from django.conf import settings 

os.makedirs(os.path.join(settings.STATIC_ROOT,'egais_files'), exist_ok=True) 
f = open(os.path.join(settings.STATIC_ROOT,'egais_files','temp.txt'), 'w') 

對於Python 2:

import errno 
import os 
import os.path 
from django.conf import settings 

try: 
    os.makedirs(os.path.join(settings.STATIC_ROOT,'egais_files')) 
except OSError as xcpt: 
    if rcpt.errno == errno.EEXIST: 
     pass 
    else: 
     raise 

f = open(os.path.join(settings.STATIC_ROOT,'egais_files','temp.txt'), 'w') 
+0

不,它不承認exist_ok關鍵字 –

+0

我假設你正在使用Python 2.x。我在上面列出的創建目錄時進行防護的方法並不理想,但僅作爲示例提供。 –

相關問題