美文网首页Django
python笔记 | Django新闻网站搭建

python笔记 | Django新闻网站搭建

作者: 力卉编程 | 来源:发表于2020-02-09 15:09 被阅读0次

一、环境搭建

环境:python + Django + mysql + nginx

1、Django 启动

/usr/local/sbin/python-3.7/bin/uwsgi --ini /etc/uwsgi.ini

2、nginx 启动

systemctl restart nginx

3、mysql 启动

mysql -uuser -p

4、python 启动

启动文件:view.main

二、代码搭建

1、uwsgi.ini

[uwsgi]
socket = 0.0.0.0:8000
chdir = /root/web/ARank
module = ARank.wsgi:application
#wsgi-file = demo.py
processes = 4
#stats = 127.0.0.1:9000
daemonize = /tmp/uwsgiServer.log
pidfile = /tmp/uwsgi.pid
vacuum = true
log-maxsize = 50000000
disable-logging = true
#callable = app

2、python

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/2.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '-tbcn*ec=fkmtps-gl)*sqphrdrkz34no$-$-ztgqz7i2z@!m7'

# 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',
]

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

ROOT_URLCONF = 'ARank.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [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',
            ],
        },
    },
    # {
    #  'OPTIONS': {
    #      'builtins': [
    #          'django.templatetags.static'
    #      ]
    #  },
    # },
]

WSGI_APPLICATION = 'ARank.wsgi.application'


# Database
# https://docs.djangoproject.com/en/2.2/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/2.2/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/2.2/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/2.2/howto/static-files/

STATIC_URL = '/static/'
# JSON_PATH = r'd:\python\Django2\static\json/'
JSON_PATH = r'/root/web/static/json/'

STATIC_ROOT = (
#    os.path.join(BASE_DIR, 'static'),
    r'/root/web'
)

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static')
    # STATIC_URL,
    # STATIC_ROOT,
    # BASE_DIR
)

3、urls.py

urlpatterns = [
    url(r'^$', view.main),
    # test
    path('hello/', view.hello),
}] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

相关文章

网友评论

    本文标题:python笔记 | Django新闻网站搭建

    本文链接:https://www.haomeiwen.com/subject/ggagxhtx.html