First, we use Pycharm to create a Jdango project
The app name, called Jdango_second, creates a good format as shown in the picture:
Two, we create the project is the MTV mode
Model (corresponding database), templates (page display), view (logical processing)
models.py:
From django.db import Models
# Create your models here.
Class Stu (models. Model):
id = models. Charfield (max_length=16,primary_key=true)
name=models. Charfield (default= ' dqd ', max_length=16)
views.py:
From django.shortcuts import render to
jdango_second.models import Stu
# Create your views here.
def showinfo (Request):
stu_list = Stu.objects.all ()
content = {' list ': Stu_list} return
render (request, ' Index.html ', content)
In admin.py, the class in models.py is registered:
From django.contrib import Admin to
jdango_second.models import Stu
# Register your models here.
Admin.site.register (STU)
In the __init__.py of the project we can do some initialization (second):
Import Pymysql
pymysql.install_as_mysqldb ()
db = Pymysql.connect ("localhost", ' root ', ', ', ' Py_second ')
cursor = Db.cursor ()
cursor.execute (' SELECT version () ')
data = Cursor.fetchone ()
print ("VERSION is%s"% Data)
Db.close ()
Also in the setting.py of the project we set up the linked database and the path to the static path and templates:
"" "Django settings for second project.
Generated by ' Django-admin Startproject ' using Django 1.11.2. For more information on this file, and https://docs.djangoproject.com/en/1.11/topics/settings/For the full list of Setti NGS and their values, "" "" "" Import OS # Build paths inside the PR
Oject 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 # and https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/# Security Warning:keep the SEC
RET key used in production secret! Secret_key = ' z=%7& (7pk#$) =2h+gs#kz9479#cvd1%1f#sd98nzz-#18ke0 (2 ' # security Warning:don ' t run with debug turned on
In production! DEBUG = True allowed_hosts = [] # application definition Installed_apps = [' django.contrib.admin ', ' django.co Ntrib.auth ', ' django.contrib.contenttypes ', ' django.contrib.sesSions ', ' django.contrib.messages ', ' django.contrib.staticfiles ', ' jdango_Second.apps.JdangoSecondConfig ',] M Iddleware = [' Django.middleware.security.SecurityMiddleware ', ' Django.contrib.sessions.middleware.SessionMiddlewa Re ', ' django.middleware.common.CommonMiddleware ', ' django.middleware.csrf.CsrfViewMiddleware ', ' Django.contrib . Auth.middleware.AuthenticationMiddleware ', ' django.contrib.messages.middleware.MessageMiddleware ', ' Django.middleware.clickjacking.XFrameOptionsMiddleware ',] root_urlconf = ' second.urls ' TEMPLATES = [{' BAC
Kend ': ' django.template.backends.django.DjangoTemplates ', ' dirs ': [Os.path.join (Base_dir, ' templates ')], ' App_dirs ': True, ' OPTIONS ': {' context_processors ': [' Django.template.contex T_processors.debug ', ' django.template.context_processors.request ', ' django.contrib.auth.co
Ntext_processors.auth ', ' Django.contrib.messages.context_processors.messages ',],},},] wsgi_application = ' SECOND.W Sgi.application ' # Database # https://docs.djangoproject.com/en/1.11/ref/settings/#databases databases = {# ' Defaul T ': {# ' ENGINE ': ' Django.db.backends.sqlite3 ', # ' NAME ': Os.path.join (Base_dir, ' Db.sqlite3 '), #} ' Default ': {' ENGINE ': ' Django.db.backends.mysql ', ' NAME ': ' Py_second ', ' USER ': ' Root ', ' PA ssWOrd ': ', ' HOST ': ' 127.0.0.1 ', ' Port ': ' 3306 ',}} # Password validation # Https://docs.djangopro ject.com/en/1.11/ref/settings/#auth-password-validators auth_password_validators = [{' NAME ': ' Django.contri B.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/LANGUA Ge_code = ' en-us ' time_zone = ' UTC ' use_i18n = True use_l10n = True Use_tz = true # Static files (CSS, JavaScript, I Mages) # Https://docs.djangoproject.com/en/1.11/howto/static-files/STATIC_URL = '/static/' Static_path = Os.path.join
(Base_dir, ' static ') Staticfiles_dirs = (Static_path,) Template_path = Os.path.join (base_dir, ' templates ') template_dirs=[template_p ATH,]
The path to the configuration browser address bar in the project's urls.py corresponds to the function name in views.py:
From django.conf.urls import URL from
django.contrib import admin from
jdango_second.views import Showinfo
Urlpatterns = [
url (R ' ^admin/', admin.site.urls),
url (r ' ^index/', showinfo),
]
Static paths are used to place a variety of statically files, such as:
Static
Img
1.png
Js
Css
The path to templates is:
Templates
Index.html
Index.html:
<! DOCTYPE html>
{% load staticfiles%}