Skip to content

Commit b265f0c

Browse files
committed
'添加了Django示例代码'
1 parent ef8cf2e commit b265f0c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

109 files changed

+337
-0
lines changed

Diff for: Day21-30/code/demo01/img/Thumbs.db

-8 KB
Binary file not shown.

Diff for: Day21-30/code/demo02/img/Thumbs.db

-5.5 KB
Binary file not shown.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

Diff for: Day41-55/code/hellodjango/demo/admin.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from django.contrib import admin
2+
3+
from demo.models import Teacher
4+
5+
6+
class TeacherAdmin(admin.ModelAdmin):
7+
list_display = ('no', 'name', 'job', 'intro', 'motto')
8+
search_fields = ('name', 'intro')
9+
ordering = ('no', )
10+
11+
12+
admin.site.register(Teacher, TeacherAdmin)

Diff for: Day41-55/code/hellodjango/demo/apps.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from django.apps import AppConfig
2+
3+
4+
class DemoConfig(AppConfig):
5+
name = 'demo'
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Generated by Django 2.0.6 on 2018-07-03 02:20
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
initial = True
9+
10+
dependencies = [
11+
]
12+
13+
operations = [
14+
migrations.CreateModel(
15+
name='Teacher',
16+
fields=[
17+
('no', models.AutoField(db_column='tno', primary_key=True, serialize=False)),
18+
('name', models.CharField(db_column='tname', max_length=20)),
19+
('job', models.CharField(db_column='tjob', max_length=10)),
20+
('intro', models.CharField(db_column='tintro', max_length=1023)),
21+
('motto', models.CharField(db_column='tmotto', max_length=255)),
22+
],
23+
options={
24+
'db_table': 'tb_teacher',
25+
},
26+
),
27+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Generated by Django 2.0.6 on 2018-07-03 03:42
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('demo', '0001_initial'),
10+
]
11+
12+
operations = [
13+
migrations.AddField(
14+
model_name='teacher',
15+
name='photo',
16+
field=models.CharField(db_column='tphoto', max_length=511, null=True),
17+
),
18+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Generated by Django 2.0.6 on 2018-07-03 05:55
2+
3+
from django.db import migrations
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('demo', '0002_teacher_photo'),
10+
]
11+
12+
operations = [
13+
migrations.AlterModelTable(
14+
name='teacher',
15+
table=None,
16+
),
17+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Generated by Django 2.0.6 on 2018-07-03 06:12
2+
3+
from django.db import migrations
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('demo', '0003_auto_20180703_1355'),
10+
]
11+
12+
operations = [
13+
migrations.AlterModelOptions(
14+
name='teacher',
15+
options={'ordering': ('-no',)},
16+
),
17+
migrations.AlterModelTable(
18+
name='teacher',
19+
table='tb_teacher',
20+
),
21+
]

Diff for: Day41-55/code/hellodjango/demo/models.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from django.db import models
2+
3+
# Django框架中包含了ORM(对象关系映射)框架
4+
# ORM可以帮助我们完成对象模型到关系模型的双向转换
5+
6+
7+
class Teacher(models.Model):
8+
no = models.AutoField(primary_key=True, db_column='tno', verbose_name='编号')
9+
name = models.CharField(max_length=20, db_column='tname', verbose_name='姓名')
10+
job = models.CharField(max_length=10, db_column='tjob', verbose_name='职位')
11+
intro = models.CharField(max_length=1023, db_column='tintro', verbose_name='简介')
12+
motto = models.CharField(max_length=255, db_column='tmotto', verbose_name='教学理念')
13+
photo = models.CharField(max_length=511, db_column='tphoto', null=True)
14+
15+
class Meta(object):
16+
db_table = 'tb_teacher'
17+
ordering = ('-no', )
File renamed without changes.

Diff for: Day41-55/code/hellodjango/demo/views.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from django.shortcuts import render
2+
3+
from demo.models import Teacher
4+
5+
6+
def home(request):
7+
# 通过ORM框架实现持久化操作CRUD
8+
ctx = {'teachers_list': list(Teacher.objects.all())}
9+
return render(request, 'demo/home.html', ctx)
File renamed without changes.

Diff for: Day41-55/code/hellodjango/hellodjango/settings.py

+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
"""
2+
Django settings for hellodjango project.
3+
4+
Generated by 'django-admin startproject' using Django 2.0.6.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/2.0/topics/settings/
8+
9+
For the full list of settings and their values, see
10+
https://docs.djangoproject.com/en/2.0/ref/settings/
11+
"""
12+
13+
import os
14+
15+
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
16+
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
17+
18+
19+
# Quick-start development settings - unsuitable for production
20+
# See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/
21+
22+
# SECURITY WARNING: keep the secret key used in production secret!
23+
SECRET_KEY = 'j*jr(3-it8$lrp&u@e^!f%8!ws*=jx)ga*ln%l6aqftu-uy1=1'
24+
25+
# SECURITY WARNING: don't run with debug turned on in production!
26+
DEBUG = True
27+
28+
ALLOWED_HOSTS = []
29+
30+
31+
# Application definition
32+
33+
INSTALLED_APPS = [
34+
'django.contrib.admin',
35+
'django.contrib.auth',
36+
'django.contrib.contenttypes',
37+
'django.contrib.sessions',
38+
'django.contrib.messages',
39+
'django.contrib.staticfiles',
40+
'demo',
41+
]
42+
43+
MIDDLEWARE = [
44+
'django.middleware.security.SecurityMiddleware',
45+
'django.contrib.sessions.middleware.SessionMiddleware',
46+
'django.middleware.common.CommonMiddleware',
47+
'django.middleware.csrf.CsrfViewMiddleware',
48+
'django.contrib.auth.middleware.AuthenticationMiddleware',
49+
'django.contrib.messages.middleware.MessageMiddleware',
50+
'django.middleware.clickjacking.XFrameOptionsMiddleware',
51+
]
52+
53+
ROOT_URLCONF = 'hellodjango.urls'
54+
55+
TEMPLATES = [
56+
{
57+
'BACKEND': 'django.template.backends.django.DjangoTemplates',
58+
'DIRS': [os.path.join(BASE_DIR, 'templates')],
59+
'APP_DIRS': True,
60+
'OPTIONS': {
61+
'context_processors': [
62+
'django.template.context_processors.debug',
63+
'django.template.context_processors.request',
64+
'django.contrib.auth.context_processors.auth',
65+
'django.contrib.messages.context_processors.messages',
66+
],
67+
},
68+
},
69+
]
70+
71+
WSGI_APPLICATION = 'hellodjango.wsgi.application'
72+
73+
74+
# Database
75+
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases
76+
77+
DATABASES = {
78+
'default': {
79+
'ENGINE': 'django.db.backends.mysql',
80+
'NAME': 'demo',
81+
'HOST': '120.77.222.217',
82+
'PORT': 3306,
83+
'USER': 'root',
84+
'PASSWORD': '123456',
85+
}
86+
}
87+
88+
89+
# Password validation
90+
# https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators
91+
92+
AUTH_PASSWORD_VALIDATORS = [
93+
{
94+
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
95+
},
96+
{
97+
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
98+
},
99+
{
100+
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
101+
},
102+
{
103+
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
104+
},
105+
]
106+
107+
108+
# Internationalization
109+
# https://docs.djangoproject.com/en/2.0/topics/i18n/
110+
111+
LANGUAGE_CODE = 'zh-hans'
112+
113+
TIME_ZONE = 'Asia/Chongqing'
114+
115+
# internationalization
116+
USE_I18N = True
117+
118+
# localization
119+
USE_L10N = True
120+
121+
USE_TZ = True
122+
123+
124+
# Static files (CSS, JavaScript, Images)
125+
# https://docs.djangoproject.com/en/2.0/howto/static-files/
126+
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
127+
STATIC_URL = '/static/'

Diff for: Day41-55/code/hellodjango/hellodjango/urls.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""hellodjango URL Configuration
2+
3+
The `urlpatterns` list routes URLs to views. For more information please see:
4+
https://docs.djangoproject.com/en/2.0/topics/http/urls/
5+
Examples:
6+
Function views
7+
1. Add an import: from my_app import views
8+
2. Add a URL to urlpatterns: path('', views.home, name='home')
9+
Class-based views
10+
1. Add an import: from other_app.views import Home
11+
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
12+
Including another URLconf
13+
1. Import the include() function: from django.urls import include, path
14+
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
15+
"""
16+
from django.contrib import admin
17+
from django.urls import path
18+
19+
from demo import views
20+
21+
urlpatterns = [
22+
path('', views.home),
23+
path('admin/', admin.site.urls),
24+
]

Diff for: Day41-55/code/hellodjango/hellodjango/wsgi.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
WSGI config for hellodjango project.
3+
4+
It exposes the WSGI callable as a module-level variable named ``application``.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/2.0/howto/deployment/wsgi/
8+
"""
9+
10+
import os
11+
12+
from django.core.wsgi import get_wsgi_application
13+
14+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hellodjango.settings")
15+
16+
application = get_wsgi_application()

Diff for: Day41-55/code/hellodjango/manage.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env python
2+
import os
3+
import sys
4+
5+
if __name__ == "__main__":
6+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hellodjango.settings")
7+
try:
8+
from django.core.management import execute_from_command_line
9+
except ImportError as exc:
10+
raise ImportError(
11+
"Couldn't import Django. Are you sure it's installed and "
12+
"available on your PYTHONPATH environment variable? Did you "
13+
"forget to activate a virtual environment?"
14+
) from exc
15+
execute_from_command_line(sys.argv)

Diff for: Day41-55/code/hellodjango/static/images/andrew.png

140 KB

Diff for: Day41-55/code/hellodjango/static/images/dennis.png

176 KB

Diff for: Day41-55/code/hellodjango/static/images/ken.png

186 KB

Diff for: Day41-55/code/hellodjango/static/images/linus.png

224 KB

Diff for: Day41-55/code/hellodjango/templates/demo/home.html

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!DOCTYPE html>
2+
{% load staticfiles %}
3+
<html lang="en">
4+
<head>
5+
<meta charset="UTF-8">
6+
<title>讲师信息</title>
7+
</head>
8+
<body>
9+
{% for x in teachers_list %}
10+
<h1>{{ x.name }}老师 - {{ x.job }}</h1>
11+
<p><strong>讲师简介</strong></p>
12+
<p>{{ x.intro }}</p>
13+
<p><strong>教学理念</strong></p>
14+
<p>{{ x.motto }}</p>
15+
<p>
16+
{% if x.photo %}
17+
<img src="{% static x.photo %}">
18+
{% endif %}
19+
</p>
20+
<hr>
21+
{% endfor %}
22+
</body>
23+
</html>
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

Diff for: Day41-55/code/shop/cart/migrations/__init__.py

Whitespace-only changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

Diff for: Day41-55/code/shop/shop/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import pymysql
2+
3+
pymysql.install_as_MySQLdb()
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

Diff for: Day41-55/code/shop_origin/cart/__init__.py

Whitespace-only changes.
File renamed without changes.
File renamed without changes.

Diff for: Day41-55/code/shop_origin/cart/migrations/__init__.py

Whitespace-only changes.
File renamed without changes.

Diff for: Day41-55/code/shop_origin/cart/tests.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.test import TestCase
2+
3+
# Create your tests here.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)