为什么我不能连接到我的本地主机django dev服务器?

时间:2023-01-07 22:45:18

I'm creating a django app and in the process of setting up my local test environment. I can successfully get manage.py runserver working but pointing my browser to any variation of http://127.0.0.1:8000/, http://0.0.0.0:8000/, or http://localhost:8000/ returns a "This site can’t be reached" error.

我正在创建一个django应用程序,并在创建本地测试环境的过程中。我能成功地管理。py runserver工作,但将我的浏览器指向任何变化的http://127.0.0.1:8000/, http://0.0.0.0:8000/,或http://localhost:8000/返回一个“这个站点无法到达”的错误。

Simultaneously, django will throw a 301 error:

同时,django将抛出301错误:

Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.
[11/Jul/2017 23:35:37] "GET / HTTP/1.1" 301 0

I've successfully deployed the app to Heroku (all URLs work there), but I can't get it running on my local machine. I've also tried heroku local's included dev server to the same effect.

我已经成功地将应用程序部署到Heroku(所有url都在那里工作),但是我不能在本地机器上运行它。我还尝试了heroku本地的包含开发服务器的相同效果。

For reference my django urls.py file looks like:

为引用我的django url。py文件看起来像:

from django.conf.urls import url
from django.contrib import admin
from recs.views import Request1, Check1, index

urlpatterns = [
    url(r'^$', index, name='index'),
    url(r'^admin/', admin.site.urls),
    url(r'^analyze1/', Request1),
    url(r'^analyze1/status/', Check1),
]

Any help appreciated!

任何帮助表示赞赏!

Edit: Posting Settings.py

编辑:发布Settings.py

import os
import recs.environment_vars as e_v

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = 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 = 'KEY HIDDEN'

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

# Honor the 'X-Forwarded-Proto' header for request.is_secure()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

ALLOWED_HOSTS = ['*']


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'recs',
]

MIDDLEWARE_CLASSES = [
    'sslify.middleware.SSLifyMiddleware',
    '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 = 'recs.urls'

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


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


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

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'America/Los_Angeles'

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_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, './static'),
)
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'

# Logs
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format': ('Application Log: ' + '[%(levelname)s] %(asctime)s [%(process)d] ' +
                       'pathname=%(pathname)s lineno=%(lineno)s ' +
                       'funcname=%(funcName)s %(message)s'),
            'datefmt': '%Y-%m-%d %H:%M:%S'
        },
        'simple': {
            'format': '%(levelname)s %(message)s'
        }
    },
    'handlers': {
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'verbose'
        }
    },
    'loggers': {
        'clothing_recommendation.clothing_recommendation': {
            'handlers': ['console'],
            'level': 'DEBUG'
        }
    }
}

import urlparse
# Celery + Redis - For long-lived asynchronous tasks (e.g. email parsing)
# Redis
redis_url = urlparse.urlparse(os.environ.get('REDIS_URL'))
CACHES = {
    "default": {
         "BACKEND": "redis_cache.RedisCache",
         "LOCATION": "{0}:{1}".format(redis_url.hostname, redis_url.port),
         "OPTIONS": {
             "PASSWORD": redis_url.password,
             "DB": 0,
         }
    }
}

# Celery
#CELERYD_TASK_SOFT_TIME_LIMIT = 60
BROKER_URL=os.environ['REDIS_URL']
CELERY_RESULT_BACKEND=os.environ['REDIS_URL']
CELERY_ACCEPT_CONTENT=['json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TASK_RESULT_EXPIRES = 300
CELERYD_MAX_TASKS_PER_CHILD = 2
BROKER_TRANSPORT_OPTIONS = {'confirm_publish': True} # Hack to prevent failed 'success' of tasks, per MT's experience with RabbitMQ - probably doesnt work with Redis? But worth trying

1 个解决方案

#1


0  

The OP posted the solution in the original question' comments but he/she seem to have forgotten to post it as an aswer. So here it is:

OP在最初的问题上发布了解决方案,但是他/她似乎忘记了把它作为一个答案发布。所以在这里:

Ok so the problem is because of the https, thus getting redirected, as localhost is working on http, try to comment out this line and check SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https') and also comment out the sslify from middleware as said by @ShobhitSharma

因此,问题是由于https,因此被重定向,因为localhost正在处理http,试着注释掉这一行并检查SECURE_PROXY_SSL_HEADER = (' http_x_ded_proto ', 'https'),并从中间件中注释掉sslify,正如@ShobhitSharma所说。

#1


0  

The OP posted the solution in the original question' comments but he/she seem to have forgotten to post it as an aswer. So here it is:

OP在最初的问题上发布了解决方案,但是他/她似乎忘记了把它作为一个答案发布。所以在这里:

Ok so the problem is because of the https, thus getting redirected, as localhost is working on http, try to comment out this line and check SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https') and also comment out the sslify from middleware as said by @ShobhitSharma

因此,问题是由于https,因此被重定向,因为localhost正在处理http,试着注释掉这一行并检查SECURE_PROXY_SSL_HEADER = (' http_x_ded_proto ', 'https'),并从中间件中注释掉sslify,正如@ShobhitSharma所说。