參考:http://www.cnblogs.com/benshan/p/4445074.htmlhtml
python3.7python
1.建立項目目錄以下mysql
web01目錄下:web
-------------------------------------------------------------------------------------------------------------sql
settings.py:項目配置文件數據庫
ALLOWED_HOSTS = ['*'] # 容許任意主機訪問web項目django
INSTALLED_APPS = [服務器
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites', # 這邊是allauth的依賴包,必須在app和登陸模塊前面session
'web.apps.WebConfig', # WebConfig文件中指定了項目名稱
]app
SITE_ID = 1
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'xx', # 所使用的的數據庫的名字
'USER': 'xx', #數據庫服務器的用戶
'PASSWORD': 'xx', #密碼
'HOST': 'xx', #主機
'PORT': '3306', #端口
}
}
-------------------------------------------------------------------------------------------------------------
urls.py:url配置文件
from web.views import say_hello, show_students, show_real_students # web.views是web目錄下的views.py, import後面的都是views.py裏面的方法
urlpatterns = [
path('admin/', admin.site.urls),
path('sayHello/', say_hello),
path('showStudents/', show_students),
path('showRealStudents/', show_real_students),
]
-------------------------------------------------------------------------------------------------------------
_init_.py:配置導入數據庫模塊
import pymysql
pymysql.install_as_MySQLdb()
-------------------------------------------------------------------------------------------------------------
web目錄下:項目配置
-------------------------------------------------------------------------------------------------------------
views.py:業務處理
from web.models import Student # models.py裏面的Student與數據庫表web_student映射
def say_hello(request):
s = 'Hello World!'
current_time = datetime.datetime.now()
html = '<html><head></head><body><h1> %s </h1><p> %s </p></body></html>' % (s, current_time)
return HttpResponse(html)
def show_students(request):
student = [{id: 1, 'name': 'Jack', 'age': 30}, {id: 2, 'name': 'Rose', 'age': 200}]
return render_to_response('student.html', {'students': student})
def show_real_students(request):
student = Student.objects.all()
return render_to_response('student.html', {'students': student})
-------------------------------------------------------------------------------------------------------------
models.py:與數據庫映射
from django.db import models
class Student(models.Model):
id = models.BigIntegerField
name = models.CharField(max_length=20, default='a')
age = models.PositiveSmallIntegerField()
-------------------------------------------------------------------------------------------------------------
apps.py:web項目配置
class WebConfig(AppConfig):
name = 'web' # 項目名稱
-------------------------------------------------------------------------------------------------------------
admin.py:項目管理頁面配置
from web.models import Student
class StudentAdmin(admin.ModelAdmin):
list_display = ('id', 'name', 'age') #添加字段顯示
search_fields = ('name', 'age') #添加快速查詢欄
admin.site.register(Student, StudentAdmin) # 參數分別是實體類,管理方法
-------------------------------------------------------------------------------------------------------------
templates目錄:頁面
-------------------------------------------------------------------------------------------------------------
student.html:頁面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<ul>
{% for student in students %}
<li>
id:{{ student.id }} 姓名:{{ student.name }} age:{{ student.age }}
</li>
{% endfor %}
</ul>
</body>
</html>
-------------------------------------------------------------------------------------------------------------
manage.py:管理(生成數據庫表,建立web管理用戶)
-------------------------------------------------------------------------------------------------------------
# 配置完models後執行命令:
python manage.py makemigrations
python manage.py migrate
# 建立管理用戶
python manage.py createsuperuser
-------------------------------------------------------------------------------------------------------------