DJANGO学习一则

时间:2023-12-11 20:23:50

这个WEB框架,可以好好研究,相信很快就会用在工作上的。

相关文件:

settings.py

"""
Django settings for djangoweb project.

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

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

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '!v*++8l%=al$7=9)-mct$=!ig^e+9hv)k&iomr&jtlul-@6^-y'

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

TEMPLATE_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_CLASSES = (
    '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 = 'djangoweb.urls'

WSGI_APPLICATION = 'djangoweb.wsgi.application'

TEMPLATE_DIRS = (
    os.path.join(os.path.split(os.path.dirname(__file__))[0], 'template'),
)
# Database
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'djangodb',
        'USER': 'root',
        'PASSWORD': 'password',
        'HOST': '127.0.0.1',
        ',
        #'ENGINE': 'django.db.backends.sqlite3',
        #'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

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

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'Asia/Shanghai'

USE_I18N = True

USE_L10N = True

USE_TZ = True

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.7/howto/static-files/
PROJECT_PATH = os.path.abspath(os.path.dirname(__file__))
STATIC_ROOT = os.path.join(os.path.dirname(PROJECT_PATH), 'static')
STATICFILES_DIRS = (
    ("css", os.path.join(STATIC_ROOT, 'css')),
    ("js", os.path.join(STATIC_ROOT, 'js')),
    ("img", os.path.join(STATIC_ROOT, 'img')),
)
STATIC_URL = '/static/'

URLs.py

from django.conf.urls import patterns, include, url
from django.contrib import admin
from djangoweb.view import hello,homepage,ctime,current_datetime

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

    url(r'^admin/', include(admin.site.urls)),
    url(r'^hello/$', hello),
    url(r'^$', homepage),
    url(r'^time/$', ctime),
    url(r'^time/(\d{1,2})/$', ctime),
    url(r'current_datetime/$', current_datetime),
)

VIEW.PY

from django.http import HttpResponse
import datetime
from django import template

def current_datetime(req):
    now = datetime.datetime.now()
    fp = open('D:/py/django/djangoweb/template/mytemplate.html')
    t = template.Template(fp.read())
    fp.close()
    html = t.render(template.Context({'current_date':now}))
    return HttpResponse(html)

def ctime(req, num):
    try:
        num = str(num)
    except ValueError:
        raise Http404

    cutime = datetime.datetime.now()
    txt = "it's %s." % cutime
    txt2 = "url is [http://127.0.0.1:8000/time/%s/]" % num
    assert False
    return HttpResponse(txt + txt2)
def hello(req):
    return HttpResponse("<h1>Hello, python django world!</h1>")
def homepage(req):
    return HttpResponse("<h1>This is homepage.</h1>")

DJANGO学习一则

DJANGO学习一则

DJANGO学习一则

DJANGO学习一则

DJANGO学习一则

DJANGO学习一则

DJANGO学习一则