注意:一個項目基本都設計增刪改查,且第一個須要作的就是設計表結構 html
思惟導圖: 前端
組件使用: Django + bootStrap + Jquery python
數據庫表結構設計: sql
外鍵關聯: 2種方式, 數據庫
1. oneTooneField -->底層也是ForenignKey,可是有Unique限制 django
2. ForeingnKey bootstrap
Django自帶的用戶認證模塊: app
from django.contrib.auth.models import User ssh
user = models.OneToOneField(User, on_delete=True) # 至關於繼承了auth.models裏面的User表 學習
注意多對可能是不能在列上顯示的
問: 爲何是request.user?
答: 這裏是利用了Django自帶認證系統,前端模板裏嵌套了request對象,除了獲取request.user還能夠獲取request.method等方法。
若是是自定義的User【未繼承auth.model】,想在前端利用request.userprofile獲取用戶信息,則須要在request對象裏面添加userprofile對象的信息
models.py
問:爲何是request.user.userprofile.roles.all ?
答:由於Django中只能從request屬性中獲取user[這裏的user是Django封裝後,auth裏面的user],而user和userprofiel是一對一的外鍵關係,因此這裏是反向查值,一對一的反向不是【字段名_set】來獲取的,而是根據request.user.userprofile獲取,再拿到帳號信息後根據多對多的Role查找角色信息最後找到menus菜單信息。
index.html
models.py
問: 爲何是 {% url menu.url %} ?
答:由於{% url menu.url %}是個URL,而{{ menu.url }}僅僅顯示一個字符串
Index.html中{% url menu.url %}顯示效果:
Index.html中{{ menu.url }}顯示效果:
問:爲何要在urls.py裏面寫別名?
答:由於咱們生成動態菜單的時候,是根據url來跳轉對應的界面的,若是這裏不寫別名的話,則須要在a表的href裏面寫死url路徑,不方便後期的維護。
問:通用模板進行前端頁面的顯示? -->【king_admin】
答: DjangoAdmin的URL由2部分組成,app名稱 + 表名
DjangoAdmin的頁面能夠定義,由 表名 + 類名組成
admin.site.register(models.UserProfile,UserProfileAdmin) -->UserAdmin是自定義的類
問:Python如何查看類中的方法
要點一:傳遞字典給前臺進行渲染,由於字典能夠根據key獲取內容,若是列表則須要循環來實現,效率低
{ 'App名稱[CRM]':{ '表名[userprofile]': '類對象[admin_class]' # admin_class定義了咱們顯示的字段 } }
要點二: 根據對象獲取App名以及表名
>>>from crm.models import UserProfile >>>UserProfile._meta.app_config <CrmConfig: crm> >>>UserProfile._meta.app_label 'crm' >>>UserProfile._meta.model_name 'userprofile' >>>UserProfile._meta.verbose_name '用戶表'
要點三:自定義前臺的字符串格式
{ 'crm': { 'customer': < class 'king_admin.king_admin.CustomerAdmin' > , 'customerfollowup': < class 'king_admin.king_admin.CustomerFollowUpAdmin' > } print(king_admin.enable_admins['crm']) # 上面就是enable_admin內容 # <class 'king_admin.king_admin.CustomerFollowUpAdmin'> print(king_admin.enable_admins['crm']['customerfollowup']) # <class 'crm.models.CustomerFollowUp'> -->獲取model對象,關聯對象和admin類 print(king_admin.enable_admins['crm']['customerfollowup'].model)
要點四: 根據app名稱和table名稱【即字符串】反射導入model類文件
方法一: 利用iportlib庫導入文件,而後根據for循環.meta_model_name表名進行內容匹配
>>>import importlib >>>dir(importlib) ['_RELOADING', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__import__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '_bootstrap', '_bootstrap_external', '_imp', '_r_long', '_w_long', 'abc', 'find_loader', 'import_module', 'invalidate_caches', 'machinery', 'reload', 'sys', 'types', 'util', 'warnings'] >>>dir(importlib.import_module) ['__annotations__', '__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__get__', '__getattribute__', '__globals__', '__gt__', '__hash__', '__init__', '__kwdefaults__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__'] >>>help(importlib.import_module('crm')) Help on package crm: NAME crm PACKAGE CONTENTS admin apps migrations (package) models tests urls views FILE f:\django\crm_01\crm\__init__.py >>>importlib.import_module('crm') // 查找到crm的App <module 'crm' from 'F:\\Django\\CRM_01\\crm\\__init__.py'> >>>importlib.import_module('crm.models') // 導入models文件 <module 'crm.models' from 'F:\\Django\\CRM_01\\crm\\models.py'> >>>m=importlib.import_module('crm.models') >>>m.CustomerFollowUp <class 'crm.models.CustomerFollowUp'> >>>m.CustomerFollowUp._meta.model_name // 查看錶名,小寫的哈 'customerfollowup'
方案二: 從咱們自定義的king_admin中的字典獲取model類
要點五: 如何根據前臺顯示的列的順序進行內容渲染
接上面的操做,根據反射獲取內容
>>>u = m.Customer.objects.all()[0] >>>getattr(u, 'name') '李客戶' >>>u.source 0 >>>u._meta.get_field('source') <django.db.models.fields.SmallIntegerField: source> >>>dir(u._meta.get_field('source')) ['__class__', '__copy__', '__deepcopy__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_check_backend_specific_checks', '_check_choices', '_check_db_index', '_check_deprecation_details', '_check_field_name', '_check_max_length_warning', '_check_null_allowed_for_primary_keys', '_check_validators', '_clear_cached_lookups', '_db_tablespace', '_description', '_error_messages', '_get_default', '_get_flatchoices', '_get_lookup', '_unique', '_unregister_lookup', '_validators', '_verbose_name', 'attname', 'auto_created', 'auto_creation_counter', 'blank', 'cached_col', 'cast_db_type', 'check', 'choices', 'class_lookups', 'clean', 'clone', 'column', 'concrete', 'contribute_to_class', 'creation_counter', 'db_check', 'db_column', 'db_index', 'db_parameters', 'db_tablespace', 'db_type', 'db_type_parameters', 'db_type_suffix', 'deconstruct', 'default', 'default_error_messages', 'default_validators', 'description', 'editable', 'empty_strings_allowed', 'empty_values', 'error_messages', 'flatchoices', 'formfield', 'get_attname', 'get_attname_column', 'get_choices', 'get_col', 'get_db_converters', 'get_db_prep_save', 'get_db_prep_value', 'get_default', 'get_filter_kwargs_for_object', 'get_internal_type', 'get_lookup', 'get_lookups', 'get_pk_value_on_save', 'get_prep_value', 'get_transform', 'has_default', 'help_text', 'hidden', 'is_relation', 'many_to_many', 'many_to_one', 'max_length', 'merge_dicts', 'model', 'name', 'null', 'one_to_many', 'one_to_one', 'pre_save', 'primary_key', 'register_lookup', 'rel_db_type', 'related_model', 'remote_field', 'run_validators', 'save_form_data', 'select_format', 'serialize', 'set_attributes_from_name', 'system_check_deprecated_details', 'system_check_removed_details', 'to_python', 'unique', 'unique_for_date', 'unique_for_month', 'unique_for_year', 'validate', 'validators', 'value_from_object', 'value_to_string', 'verbose_name'] >>>u._meta.get_field('source').get_choices(0) [(0, '轉介紹'), (1, 'QQ羣'), (2, '官網'), (3, '百度推廣'), (4, '51CTO'), (5, '知乎'), (6, '市場推廣')] >>>getattr(u, "get_source_display")() '轉介紹' >>>dir(u) ['DoesNotExist', 'MultipleObjectsReturned', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_check_column_name_clashes', '_check_field_name_clashes', '_check_fields', '_check_id_field', '_check_index_together', '_check_local_fields', '_check_long_column_names', '_check_m2m_through_same_relationship', '_check_managers', '_check_model', '_check_model_name_db_lookup_clashes', '_check_ordering', '_check_swappable', '_check_unique_together', '_do_insert', '_do_update', '_get_FIELD_display', '_get_next_or_previous_by_FIELD', '_get_next_or_previous_in_order', '_get_pk_val', '_get_unique_checks', '_meta', '_perform_date_checks', '_perform_unique_checks', '_save_parents', '_save_table', '_set_pk_val', '_state', 'check', 'clean', 'clean_fields', 'consult_course', 'consult_course_id', 'consultant', 'consultant_id', 'content', 'customerfollowup_set', 'date', 'date_error_message', 'delete', 'enrollment_set', 'from_db', 'full_clean', 'get_deferred_fields', 'get_next_by_date', 'get_previous_by_date', 'get_source_display', 'get_status_display', 'id', 'memo', 'name', 'objects', 'payment_set', 'phone', 'pk', 'prepare_database_save', 'qq', 'qq_name', 'referral_from', 'refresh_from_db', 'save', 'save_base', 'serializable_value', 'source', 'source_choice', 'status', 'status_choices', 'tags', 'unique_error_message', 'validate_unique']
問題一: 建立的用戶沒法登錄
答:實則少個權限,界面勾選staff status 解決
問題二:TemplateSyntaxError at /king_admin/
Variables and attributes may not begin with underscores: 'admin.admin_obj._meta.model_name'
經過自定義標籤解決:
問題三: 模板語言中寫a標籤格式錯誤:
解決: