Django静态文件无法在Heroku上运行

时间:2015-07-05 18:25:16

标签: python django heroku

我的django应用程序在本地服务器上运行良好。但是,当我在Heroku上部署它时,静态文件没有被提供(得到404错误)。请帮忙!

from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.conf.urls.static import static
from django.conf import settings

urlpatterns = patterns('',
       url(r'^$', 'product.views.home', name='home'),
       url(r'^admin/', include(admin.site.urls)),

   )
if settings.DEBUG:
       urlpatterns += static(settings.STATIC_URL,document_root=settings.STATIC_ROOT)
       urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

静态文件设置:

  TEMPLATE_DIRS = (
      os.path.join(BASE_DIR, 'templates'),
  )

  STATIC_URL = '/static/'

  MEDIA_URL = '/media/'

  MEDIA_ROOT = os.path.join(BASE_DIR, "static", "media")

  STATIC_ROOT = os.path.join(BASE_DIR, "static", "static_root")

  STATICFILES_DIRS = (
      os.path.join(BASE_DIR, "static", "static_dirs"),
  )

WSGI文件 -

  import os
  os.environ.setdefault("DJANGO_SETTINGS_MODULE", "acton.settings")

  from django.core.wsgi import get_wsgi_application
  application = get_wsgi_application()

  try:
      from dj_static import Cling
      application = Cling(get_wsgi_application())

  except:
      pass

3 个答案:

答案 0 :(得分:1)


这是我在Heroku上部署静态文件的设置。

希望它会有所帮助:

  

导入操作系统   BASE_DIR = os.path.dirname(os.path.abspath(文件))
  STATIC_ROOT ='staticfiles'
  STATIC_URL ='/ static /'
  MEDIA_ROOT = os.path.join(PROJECT_PATH,“media”)
  MEDIA_URL =“/ media /”
  STATICFILES_DIRS =(
     os.path.join(BASE_DIR,'static'),
  )

答案 1 :(得分:1)

您的setting.py文件配置不正确。静态和媒体文件应为

    package web.controller.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import service.CommonManagementService;
import utils.Result;
import web.view.QuestionItem;
import domain.Question;

@Controller
public class ComboController  {



    private static final Logger LOGGER = LogManager.getLogger();

    @Autowired
    @Qualifier("commonManagementService")
    private CommonManagementService commonManagementService;

    public CommonManagementService getCommonManagementService() {
        return this.commonManagementService;
    }

    @RequestMapping(value = "/unsecure/getQuestion.htm", method = RequestMethod.GET)
    public ModelAndView getQuestion(ModelMap model) {
        LOGGER.info("debut methode getQuestion");
        final List<QuestionItem> results = new ArrayList<QuestionItem>();
        final List<Question> questions = this.commonManagementService.getQuestions();
        for (final Question question : questions) {
            results.add(new QuestionItem(question.getQuestion(), question.getId().toString()));
        }
        final Map<String, Object> modelToPass = new HashMap<String, Object>();
        model.put("items", results);
        model.put("results", new ArrayList<Result>());
        LOGGER.info("fin methode getQuestion");
        return new ModelAndView("jsonResultView", model);

    }

    public void setCommonManagementService(
            CommonManagementService commonManagementService) {
        this.commonManagementService = commonManagementService;
    }
}

答案 2 :(得分:1)

对于遇到此问题的其他人,对我而言,我错过了wsgi.py文件中的whitenoise配置。

具体来说,wsgi.py文件中缺少以下内容:

from whitenoise.django import DjangoWhiteNoise application = DjangoWhiteNoise(application)

文档在这里:http://whitenoise.evans.io/en/stable/