2016-10-06 2 views
1

Ich habe Probleme mit der Bereitstellung einer Django-Anwendung auf einem Debian 8 VPS. Python Version 2.7, Django 1.10.2.Django, statische Dateien uwsgi, die nicht einmal nach Collectstatic geliefert werden

Mein Problem ist, dass es statische Dateien im Produktionsmodus nicht liefern wird (DEBUG = False), selbst nachdem "collectstatic" ausgeführt und ein STATIC_ROOT-Verzeichnis zugewiesen wurde.

Ich habe alle Anweisungen in Bezug auf diese Bereitstellung (nginx, uwsgi, python) gefolgt, aber immer noch 404 für alle meine statischen Dateien. Wenn collectstatic ausgeführt wird, werden alle Dateien in ein/static/Verzeichnis am Anfang der Anwendung geschrieben. Wenn ich uwsgi oder den Entwicklungsserver benutze, funktionieren HTML und Python einwandfrei, aber auf alle meine statischen CSS, JS, IMG kann nicht zugegriffen werden.

Wenn ich zurück zu DEBUG = True und den Dev-Server ausführen, sind meine statischen Dateien wieder vorhanden.

Kann jemand schauen und sehen, was ich falsch machen könnte? Wenn Sie mehr Kontext von Dateien benötigen, lassen Sie es mich wissen.

Meine settings.py lautet wie folgt:

""" 
Django settings for mysite project. 

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

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

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

import os 
# 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.10/howto/deployment/checklist/ 

# SECURITY WARNING: keep the secret key used in production secret! 
SECRET_KEY = 'removedforstackoverflow' 

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

ALLOWED_HOSTS = ['removedforstackoverflow'] 


# Application definition 

INSTALLED_APPS = [ 
    'main', 
    'instant', 
    'opengig', 
    'widget_tweaks', 
    'django.contrib.admin', 
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.messages', 
    'django.contrib.staticfiles', 
] 

MIDDLEWARE = [ 
    'django.middleware.security.SecurityMiddleware', 
    'django.contrib.sessions.middleware.SessionMiddleware', 
    'django.middleware.common.CommonMiddleware', 
    'django.middleware.csrf.CsrfViewMiddleware', 
    'django.contrib.auth.middleware.AuthenticationMiddleware', 
    'django.contrib.messages.middleware.MessageMiddleware', 
    'django.middleware.clickjacking.XFrameOptionsMiddleware', 
] 

ROOT_URLCONF = 'mysite.urls' 

TEMPLATES = [ 
    { 
     'BACKEND': 'django.template.backends.django.DjangoTemplates', 
     'DIRS': [], 
     '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 = 'mysite.wsgi.application' 


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

DATABASES = { 
    'default': { 
     'ENGINE': 'django.db.backends.sqlite3', 
     'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), 
    } 
} 


# Password validation 
# https://docs.djangoproject.com/en/1.10/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.10/topics/i18n/ 

LANGUAGE_CODE = 'en-us' 

TIME_ZONE = 'UTC' 

USE_I18N = True 

USE_L10N = True 

USE_TZ = True 


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

STATIC_URL = '/static/' 
STATIC_ROOT = os.path.join(BASE_DIR, "static/") 

Hier ist die Datei header.html, wo ich meine statische Dateien aufrufen. Wenn es nur Bootstrap wäre, würde ich das CDN verwenden, aber ich habe ein paar Bilder, die ich benutze, und ich würde lieber keinen Server einrichten, nur um meine statischen Dateien zu hosten, wenn diese Anwendung so klein ist.

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <title>Instant Backoffice</title> 
    <meta charset="utf-8" /> 
    {% load staticfiles %} 
    <link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}" type = "text/css"/> 
    <meta name="viewport" content = "width=device-width, initial-scale=1.0"> 
</head> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 
<script type="text/javascript" src="{% static 'js/bootstrap.min.js' %}"></script> 
<body> 
    <nav class="navbar navbar-default"> 
     <div class="container-fluid"> 
      <div class="navbar-header"> 
       <a class="navbar-brand" href="/"> 
        <img alt="Brand" src="{% static 'img/logo.svg' %}" height="25"> 
       </a> 
      </div> 
      <ul class="nav navbar-nav navbar-right"> 
       <ul class="nav nav-pills"> 
        <li><a href="/instant/allorders">List of Orders</a></li> 
        <li><a href="/instant/payment">Change Payment Status</a></li> 
        <li><a href="/instant/review">Add a Review</a></li> 
        <li><a href="/instant/cancel">Cancel an Order</a></li> 
        <li><a href="/logout/">Logout</a></li> 
       </ul> 
      </li> 
     </ul> 
    </div> 
</nav> 
<div class="row"> 
    <div class='container-fluid'> 
     <div class="col-sm-12"> 
      {% block content %} 
      {% endblock %} 
     </div> 
    </div> 
    </div> 
</body> 
</html> 

Fehlerliste aus dem Entwicklungsserver:

[06/Oct/2016 23:40:43] "GET /static/css/bootstrap.min.css HTTP/1.1" 404 102 
[06/Oct/2016 23:40:43] "GET /static/js/bootstrap.min.js HTTP/1.1" 404 100 
[06/Oct/2016 23:40:44] "GET /static/img/logo.svg HTTP/1.1" 404 93 
[06/Oct/2016 23:40:44] "GET /static/js/bootstrap.min.js HTTP/1.1" 404 100 
[06/Oct/2016 23:40:44] "GET /static/img/logo.svg HTTP/1.1" 404 93 
[06/Oct/2016 23:40:44] "GET /static/img/bg.jpg HTTP/1.1" 404 91 

Wie gesagt, ich bin immer nur 404 ist, und ich verstehe nicht, warum. Jede Hilfe wäre großartig.

+0

Wenn Sie ein NginX als Reverse-Proxy verwenden, sollten Sie uWSGI sowieso nicht verwenden, um statische Inhalte bereitzustellen. Poste deine uWSGI- und NginX-Konfigurationen bitte. – Selcuk

Antwort

2

Sie müssen sicherstellen, urls.py Datei Ihres Projekts wird aktualisiert, um die statische Datei aus der Produktion zu dienen.

Aktualisieren Sie die urls.py-Datei Ihres Projekts mit dem folgenden Code wie unten gezeigt.

from django.conf import settings 
from django.conf.urls.static import static 

urlpatterns = [ 
    # ... the rest of your URLconf goes here ... 
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) 

Eine andere Sache, ich sehe keine STATICFILES_DIRS Verzeichnisse in Ihrem settings.py als auch Datei. Sie müssen das auch aktualisieren, so dass, wenn Sie ./manage.py collectstatic Befehl ausführen alle statische Assets von statischen Verzeichnissen gesammelt werden, um Ihre Root static Verzeichnis, das ist die beste Praxis in Django, obwohl direkt die statischen Dateien auf STATIC_ROOT Verzeichnis funktioniert für Produktion.

+0

Danke, das funktioniert für mich in Django 1.10.5. – zhihong

+0

Wenn ich wsgi konfigurieren, um statische Dateien zu liefern. Warum muss ich Django noch konfigurieren, um statische Dateien zu liefern? – Stephan

Verwandte Themen