显示年月,注册页面和后台数据交互,不涉及数据库

第一步urls

"""mysite01 URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/1.11/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.conf.urls import url, include
    2. Add a URL to urlpatterns:  url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url
from django.contrib import admin
from blog import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^show_time/', views.show_time),
    url(r'article/(\d{4})$', views.article_year),#$代表以某4位数结尾,如2001,只以他结尾
    # 无命名分组形式访问
    # \d一个占位符,占4个如2004,
    #r代表正则
    url(r'article/(?P<year>\d{4})/(?P<month>\d{2})', views.article_year_month),
    #命名分组,就是加上?P<>这种固定格式,且尖括号里内容为函数参数名
    url(r'register', views.register,name="reg"),#别名reg,以后用它就行
]

2 views里

from django.shortcuts import render, HttpResponse
import time


# Create your views here.
def show_time(req):
    # return HttpResponse('Hello')
    t = time.ctime()
    # return render(req,"index.html",locals())
    return render(req, 'index.html', {'time': t})  # 其实就是键值对与locals相同
def article_year(req,year):
    return HttpResponse("year;%s "%(year))
def article_year_month (req,year,month):#必须与前端urls页面命名相同
    return HttpResponse("year;%s month:%s" % (year, month))
def register(req):
    if req.method=="POST":
        print(req.POST.get("user"))
        print(req.POST.get("age"))
        return HttpResponse('success')

    return render(req,"register.html")#学会使用render

register.html

<!DOCTYPE html>
<html lang="en">
<head>
    {% load staticfiles %}
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1 align="center">学生注册页面</h1>

<form action="{% url 'reg' %}" method="post"align="center">
    <p>姓名<input type="text"name="user"></p>
    <p>年龄<input type="text"name="age"></p>
    <p>爱好<input type="checkbox"name="hobby"value="1">篮球
            <input type="checkbox"name="hobby"value="2">足球
            <input type="checkbox"name="hobby"value="3">网球
    </p>
    <p><input type="submit">提交</p>
</form>
</body>
</html>

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>hello,tom</h1>
<h1>{{ time }}</h1>
<script src="/blog/static/jquery-3.1.1.js"></script>//加上它就可以使用$符号
<script>
    $("h1").css("color","red")
</script>
</body>
</html>

settings

"""
Django settings for mysite01 project.

Generated by 'django-admin startproject' using Django 1.11.3.

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

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

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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '^e05n!#hnt0$l_haxv9f^d)5k$e--b6nn3&)iubu2f$e+p12r^'

# 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',
    'blog.apps.BlogConfig',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    #'django.middleware.csrf.CsrfViewMiddleware',#当html页面使用post方法此中间件必须注释,否则,forbbden
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

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


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

STATIC_URL = '/static/'
#  要想哪static文件夹内容,用/static/这一串来拿,参考index页面引方式,其实这是别名,以后都用别名

STATICFILES_DIRS = (
    #  必须加
    os.path.join(BASE_DIR, 'blog/static'),
    #  路劲拼接,base相当于在mysite这一层,即最外层,按这个路径往下找static.逗号必须加,少了不能执行
)

 

posted @ 2018-08-01 15:40  未来的技术  阅读(152)  评论(1编辑  收藏  举报