2016-04-14 7 views
0

Ich habe sorgfältig documentation und mehrere Diskussionen im Zusammenhang mit dem Thema gelesen, kann aber immer noch nicht herausfinden, wo der Dollar aufhört. Ich möchte nur eine statische Datei in Django-Ansicht öffnen, einige Inhalte in sie schreiben und esDjango - statische Datei vom Server öffnen

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

Methode zu speichern.

Meine Einstellungen:

""" 
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"), 
    ) 

wie alles Sieht aus dem entspricht, was Dokumentation sagt. Hilf mir bitte ! Wo ist der Fehler ???

Antwort

0

Um eine Datei zu öffnen, müssen Sie den Pfad der Datei an open übergeben. Sie übergeben die URL.

Es sollte so aussehen:

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

Danke. Es kann os.join nicht erkennen, trotz der Tatsache, dass ich os importiert –

+0

Sorry, sollte 'os.path.join' (wie in Ihrer settings.py) – ilse2005

+0

Nein, hat nicht funktioniert [Errno 2] Keine solche Datei oder Verzeichnis: '\\\\ 10.8.0.1 \\ share/egais_files/temp.txt \\ w' –

0

In der Anfangscodeschnipsel Sie versuchen, eine Datei zu öffnen für in einem Unterverzeichnis aus dem Stammverzeichnis des Dateisystems zu schreiben. Sie müssen zwei Dinge sicherstellen. Der erste ist, dass Sie an den richtigen Ort schreiben. Wahrscheinlich müssen Sie die open-Anweisung in etwas mit einem absoluten Pfad ändern. Sie müssen außerdem sicherstellen, dass STATIC_ROOT/egais_files vorhanden ist, bevor Sie versuchen, die Datei zu erstellen. Für 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') 

Für 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

Nein, es erkennt das Schlüsselwort exist_ok nicht –

+0

Ich nehme an, dass Sie dann Python 2.x verwenden. Der Ansatz, den ich oben zum Schutz beim Erstellen des Verzeichnisses skizziert habe, ist nicht ideal, wird aber als Beispiel bereitgestellt. –