首页 > 代码库 > DJANGO学习一则

DJANGO学习一则

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

相关文件:

settings.py

"""Django settings for djangoweb project.For more information on this file, seehttps://docs.djangoproject.com/en/1.7/topics/settings/For the full list of settings and their values, seehttps://docs.djangoproject.com/en/1.7/ref/settings/"""# Build paths inside the project like this: os.path.join(BASE_DIR, ...)import osBASE_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 = TrueTEMPLATE_DEBUG = TrueALLOWED_HOSTS = []# Application definitionINSTALLED_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.urlsWSGI_APPLICATION = djangoweb.wsgi.applicationTEMPLATE_DIRS = (    os.path.join(os.path.split(os.path.dirname(__file__))[0], template),)# Database# https://docs.djangoproject.com/en/1.7/ref/settings/#databasesDATABASES = {    default: {        ENGINE: django.db.backends.mysql,        NAME: djangodb,        USER: root,        PASSWORD: password,        HOST: 127.0.0.1,        PORT: 3306,        #‘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-usTIME_ZONE = Asia/ShanghaiUSE_I18N = TrueUSE_L10N = TrueUSE_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, urlfrom django.contrib import adminfrom djangoweb.view import hello,homepage,ctime,current_datetimeurlpatterns = 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(rcurrent_datetime/$, current_datetime),)

VIEW.PY

from django.http import HttpResponseimport datetimefrom django import templatedef 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学习一则