django 專案,app 構建,admin 工具使用,根據model生成表
阿新 • • 發佈:2019-02-15
環境:安裝了django1.11,mysql,Mysql-python
建立一個django專案
django-admin.py startproject shuili
建立app(在專案的根目錄下),注意以下步驟都是基於app
python3 manage.py startapp back
目錄結構
shuili |-- back | |-- admin.py | |-- admin.pyc | |-- apps.py | |-- __init__.py | |-- __init__.pyc | |-- migrations | | |-- 0001_initial.py | | |-- 0001_initial.pyc | | |-- __init__.py | | `-- __init__.pyc | |-- models.py | |-- models.pyc | |-- tests.py | `-- views.py |-- manage.py |-- run.py `-- shuili |-- __init__.py |-- __init__.pyc |-- settings.py |-- settings.pyc |-- urls.py |-- urls.pyc |-- wsgi.py `-- wsgi.pyc
訪 http://127.0.0.1:8000/ (或者http://localhost:8000/)即可得到 It worked!
python manage.py runserver 0.0.0.0:8000
修改settings.py的db配置
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'shuili', 'USER': 'root', 'PASSWORD': '123456', 'HOST': '127.0.0.1', 'PORT': '3306', } }
修改靜態檔案配置,admin的static靜態檔案可以在python的site-packages下Django-1.11-py2.7.egg\django\contrib\admin\static找到。可以把此目錄下的檔案拷貝到當前專案下。
admin的templates 預設使用的是Django-1.11-py2.7.egg\django\contrib\admin\templates。
在urls.py 最下面加入STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR, 'static')#static 目錄的絕對位置 print STATIC_ROOT
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()
建立資料庫
python manage.py makemigrations
python manage.py migrate
建立admin賬號密碼
python manage.py createsuperuser
啟動專案
python manage.py runserver 0.0.0.0:8000
登入介面
建立新的資料庫 model,models.py(注意此處沒有max_length=255等引數,是因為在資料庫中寂靜建立一下三個表,如果沒有建立,則需要寫這些引數)
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models
# Create your models here.
class SiteInfo(models.Model):
"""
站點資訊
"""
class Meta:
db_table = 'site_info'
verbose_name = "站點資訊"
verbose_name_plural = "站點資訊"
id = models.CharField(max_length=255,db_column='id',primary_key=True)
site_num = models.CharField(max_length=255,db_column='site_num')
site_name = models.CharField(max_length=255,db_column='site_name')
province = models.CharField(max_length=255,db_column='province')
city = models.CharField(max_length=255,db_column='city')
area = models.CharField(max_length=255,db_column='area')
lo = models.FloatField(db_column='lo')
la = models.FloatField(db_column='la')
remarks = models.CharField(max_length=255,db_column='remarks',blank=True)
type = models.CharField(max_length=255,db_column='type')
#around_site_ids = models.CharField(max_length=255,db_column='around_site_ids')
site_info = models.ManyToManyField("self",verbose_name = "相關站點",blank=True)
create_time = models.DateTimeField(db_column='create_time')
update_time =models.DateTimeField(db_column='update_time')
def __str__(self):
return self.site_name#外來鍵會呼叫
class River(models.Model):
"""
水位
"""
class Meta:
db_table = 'river'
verbose_name = "水位"
verbose_name_plural = "水位"
id = models.CharField(max_length=255,db_column='id',primary_key=True)
data = models.FloatField(db_column='data',verbose_name='data1')
#site_id = models.CharField(db_column='site_id')
site=models.ForeignKey(SiteInfo,verbose_name='站點')
remarks = models.CharField(max_length=255,db_column='remarks',blank=True)
create_time = models.DateTimeField(db_column='create_time')
date_hour = models.CharField(max_length=255,db_column='date_hour')
class Rain(models.Model):
"""
降水
"""
class Meta:
db_table = 'rain'
verbose_name = "降水"
verbose_name_plural = "降水"
id = models.CharField(max_length=255,db_column='id', primary_key=True)
data = models.FloatField(db_column='data')
#site_id = models.CharField(max_length=20,db_column='site_id')
site = models.ForeignKey(SiteInfo)
remarks = models.CharField(max_length=255,db_column='remarks',blank=True)
create_time = models.DateTimeField(db_column='create_time')
date_hour = models.CharField(max_length=255,db_column='date_hour')
根據model建立資料庫表(admin工具自動建立),輸入以下命令
python manage.py makemigrations
python manage.py migrate
如果執行python manage.py makemigrations提示沒有改變No changes detected,執行以下步驟:
1、刪除 app目錄下migrations目錄下除了__init__.py檔案以外的所有檔案。
如果提示執行 python manage.py migrate提示 No migrations to apply,執行如下步驟
1、從資料庫刪除自己app的記錄
delete from django_migrations where app='back'; #back是你app的名字
把model註冊給admin管理
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.contrib import admin
import models
# Register your models here.
class RiverAdmin(admin.ModelAdmin):
list_display = ('id', 'data', 'site','remarks','create_time','date_hour') #新增欄位顯示
search_fields = ('data', 'remarks') #新增快速查詢欄
class RainAdmin(admin.ModelAdmin):
list_display = ('id', 'data', 'site','remarks','create_time','date_hour') #新增欄位顯示
search_fields = ('data', 'remarks') #新增快速查詢欄
admin.site.register(models.SiteInfo)
admin.site.register(models.Rain, RainAdmin)
admin.site.register(models.River, RiverAdmin)
加入自己的app包路徑
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
"back"#models.py的目錄
]
啟動專案,並登陸可以看到