当前位置: 澳门新濠3559 > 编程 > 正文

1.在项目目录创建media文件夹

时间:2019-11-29 05:50来源:编程
Demo:  hello_pycharm 根目录文件:hello_pycharm[__init__.py  __pycache__  settings.py  urls.py  wsgi.py] App:hello [admin.py  apps.py __init__.py  migrations  models.py  tests.py urls.py  views.py  __pycache__ ] 模板

Demo:  hello_pycharm

根目录文件:hello_pycharm [__init__.py  __pycache__  settings.py  urls.py  wsgi.py]

App:hello [admin.py  apps.py  __init__.py  migrations  models.py  tests.py  urls.py  views.py  __pycache__ ]

模板文件:templates

 

 

 

 

根路由urls中:

from django.conf.urls import url,include

from django.contrib import admin

from hello import views

 

urlpatterns = [

    url(r'^admin/', admin.site.urls),

    url(r'^hi/',views.hi),

    url(r'^hell/',views.hello_use_template),

    url(r'^hello/',include("hello.urls"))

 

]

 

App文件夹hello中的子路由urls中:二个应用app能够有叁个路由 称为主应用的子路由也叫二级路由

urlpatterns = [

    

    url(r'^a/',views.hi),

    url(r'^b/',views.hello_use_template)

 

]

 

 

 

 

 

views中:

 

from django.shortcuts import render

from django.http import HttpResponse

 

def hello_use_template(request):

 

    return  render(request,"hello.html",context={"name":"Gavin"})

 

def hi(request):

 

    html = '''<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <title>Title</title>

</head>

<body>

<h1>Hello World!</h1>

 

</body>

</html>

    '''

1.在项目目录创建media文件夹。return HttpResponse(html)

 

 

模板(templates)中:hello.html

 

<!DOCTYPE html>

<html lang='en'>

 

<head>

    <meta charset='UTF-8>

    <title>Title</title>

</head>

 

<body>

<h1>Hello {{name}}!</h1>

</body>

 

</html>

 

 

 

 

Settings中:

 

import os

 

print(__file__)

print(os.path.abspath(__file__))

print(os.path.dirname(os.path.abspath(__file__)))

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

 

SECRET_KEY= 'ja=_kx#x^i0swyl_1b0f8*%p^8g5pj)d2!ovwk7hpopnqyaa'

 

DEBUG = True

ALLOWED_HOSTS = []

INSTALLED_APPS = [

    'django.contrib.admin',

    'django.contrib.auth',

    'django.contrib.contenttypes',

    'django.contrib.sessions',

    'django.contrib.messages',

    'django.contrib.staticfiles',

    'hello.apps.HelloConfig',或者用app.py文件的name值简写:‘hello’

]

 

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 = 'hello_pycharm.urls'

 

TEMPLATES = [

    {

        'BACKEND': 'django.template.backends.django.DjangoTemplates',

        '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 = 'hello_pycharm.wsgi.application'

 

DATABASES = {

    'default': {

        'ENGINE': 'django.db.backends.sqlite3',

        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),

    }

}

 

 

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

    },

]

LANGUAGE_CODE = 'zh-Hans'

 

TIME_ZONE = 'UTC'

 

USE_I18N = True

 

USE_L10N = True

 

USE_TZ = True

 

STATIC_URL = '/static/'

print(BASE_DIR)

print(os.path.join(BASE_DIR, 'templates'))

 

 

路由(b)经过模板渲染输出结果:

127.0.0.1:8000/hello/b/

 

‘’Hello August !”

4.在urls进行安装
导入 from django.views.static import serve
导入 from mx.settings import MEDIA_ROOT

Internationalization

2.在项目标布署文件settings.py中设置
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, "media")

SECURITY WARNING: keep the secret key used in production secret!

SECRET_KEY = 'djn)1-4%55mpw=j=42@ju13*)87osfdk=zvuhp-zvc4@=%^hqk'

urlpatterns = [
#构造上传文件的拜见管理函数
url(r'^media/(?P<path>.*)$', serve, {"document_root":MEDIA_ROOT}),
]

settings.py

"""
Django settings for mysite3 project.

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

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

1.在品种目录创立media文件夹,作为上传图片保存路径

Database

3.在settings中的TEMPLATES目录下增加' django.core.context_processors.media',
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'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',
'django.core.context_processors.media',
],
},
},
]

models.py

from django.db import models

sex_choices=(
('f','famale'),
('m','male'),
)
class User(models.Model):
name = models.CharField(max_length=30)
sex = models.CharField(max_length=1,choices=sex_choices)

def __unicode__(self):
    return self.name

Quick-start development settings - unsuitable for production

urls.py

from django.conf.urls import patterns, include, url

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
# Examples:
# url(r'^$', 'mysite3.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),

url(r'^admin/', include(admin.site.urls)),

)

SECURITY WARNING: don't run with debug turned on in production!

DEBUG = True

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []

Static files (CSS, JavaScript, Images)

Application definition

INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog',
'django.contrib.admin',
)

MIDDLEWARE_CLASSES = (
'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 = 'mysite3.urls'

WSGI_APPLICATION = 'mysite3.wsgi.application'

https://docs.djangoproject.com/en/1.6/topics/i18n/

LANGUAGE_CODE = 'zh-cn'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

admin.py

from django.contrib import admin
from blog.models import User

admin.site.register(User)

https://docs.djangoproject.com/en/1.6/howto/static-files/

STATIC_URL = '/static/'

django-admin.py startapp XXX

django-admin.py startproject XXXX

Build paths inside the project like this: os.path.join(BASE_DIR, ...)

import os
BASE_DIR = os.path.dirname(os.path.dirname(file))

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

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'mysite3.db',
}
}

See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/

编辑:编程 本文来源:1.在项目目录创建media文件夹

关键词: