settings.py
"""
Django Settings for guest project.
Generated by ' Django-admin startproject ' using Django 2.0.7.
For more information on the this file, see
https://docs.djangoproject.com/en/2.0/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.0/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/2.0/howto/deployment/checklist/
# SECURITY Warning:keep The secret key used in production secret!
Secret_key = ' bl%t7) d*tz=_ww) i^[email protected]*9azb$f%t0h2+ (E!GZMTNM-+2J '
# SECURITY Warning:don ' t run with debug turned on 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 ',
' Sign ', #添加sign应用
]
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 = ' Guest.urls '
TEMPLATES = [
{
' Backend ': ' Django.template.backends.django.DjangoTemplates ',
' DIRS ': [],
' 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 = ' guest.wsgi.application '
# Database
# https://docs.djangoproject.com/en/2.0/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.0/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.0/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.0/howto/static-files/
Static_url = '/static/'
urls.py
"" "Guest URL Configuration
The ' urlpatterns ' list routes URLs to views. For more information. See:
https://docs.djangoproject.com/en/2.0/topics/http/urls/
Examples:
Function views
1. Add an import:from My_app import views
2. Add a URL to Urlpatterns:path (", Views.home, Name= ' home ')
class-based views
1. Add an import:from other_app.views import Home
2. Add a URL to Urlpatterns:path ("', Home.as_view (), name= ' Home ')
including another URLconf
1. Import the Include () Function:from django.urls Import include, path
2. Add a URL to Urlpatterns:path (' blog/', include (' Blog.urls '))
"""
From Django.contrib Import admin
From Django.conf.urls import URL
From Django.urls Import path
From sign import views #导入sign应用views文件
Urlpatterns = [
#path (' admin/', admin.site.urls),
#path (' admin/$ ', views.index), #添加index/path configuration
#url (R ' ^index/$ ', Views.index),
URL (r ' ^admin/', admin.site.urls), #40页
URL (r ' ^index/$ ', Views.index),
URL (r ' ^login_action/$ ', views.login_action),
]
views.py
from django.shortcuts import render
from django.shortcuts import HttpResponse
from django.http Import HttpResponse
# Create your views here.
# #
#def Index (Request):
#return httpresponse ("Hello django!")
# # Return render (Request, "index.html")
# #
#登录动作
def login_action (request):
If Request.method = = ' Post ':
Username = = Request. Post.get (' username ', ')
Password = = Request. Post.get (' password ', ')
if username = = ' admin ' and password = = ' admin123 ':
Return HttpResponse (' L Ogin success! ')
Else:
Return render (Request, ' index.html ', {' Error ': ' Username or password error! '})
index.html
<! DOCTYPE html>
<meta charset= "UTF-8" >
<title>django page</title>
<body>
<form method= "POST" action= "/login_action/" >
<input name= "username" type= "text" placeholder= "username" ><br>
<input name= "password" type= "password" placeholder= "password" ><br>
{{Error}}<br>
<button id= "BTN" type= "submit" > Login </button>
{% Csrf_token%}
</form>
</body>
Recently in contact with Django, wrote a small example web page can't open, ask God guidance