python django bootstrap_导入 201901
參考?http://www.liujiangblog.com/course/django/124
?
AdminLTE-2.4.5
http://www.liujiangblog.com/course/django/125
?
1.
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
mkdir static
為了以后擴展的方便,將AdminLTE源文件包里的bootstrap (bower_components)、dist和plugins三個文件夾,全部拷貝到 static目錄中,這樣做的話文件會比較大,比較多,但可以防止出現(xiàn)引用文件找不到、插件缺失等情況的發(fā)生,等以后對AdminLTE非常熟悉了,可以對static中無用的文件進行刪減。
?
2.
mkdir templates
在cmdb根目錄下的templates目錄下,新建base.html文件,
將AdminLTE源文件包中的index.html中的內(nèi)容拷貝過去。
然后,根據(jù)我們項目的具體情況修改文件引用、頁面框架、title、CSS、主體和script塊。
cp index.html base.html
?
3.
二、創(chuàng)建路由、視圖
這里設計了三個視圖和頁面,分別是:
dashboard:儀表盤,圖形化的數(shù)據(jù)展示
index:資產(chǎn)總表,表格的形式展示資產(chǎn)信息
detail:單個資產(chǎn)的詳細信息頁面
/urls.py修改成下面的樣子:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from people import views
app_name = 'people'
urlpatterns = [
path('admin/', admin.site.urls),
# path(r'^report/', views.report, name='report'),
path(r'^dashboard/', views.dashboard, name='dashboard'),
path(r'^index/', views.index, name='index'),
path(r'^detail/(?P<asset_id>[0-9]+)/$', views.detail, name="detail"),
path(r'^$', views.dashboard),
]
?
############
https://blog.csdn.net/wjy397/article/details/48976137
django 模板找不到TemplateDoesNotExist報錯!
然后需要先獲取settings文件的上一級目錄也就是AssetsPool同級目錄
之后設
置TEMPLATE_DIRS將AssetsPool同級目錄AssetsPoolApp下加入到模板路徑中,
BASE_TEMPLATE_DIRS = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))#
獲取當前腳本的父目錄
TEMPLATE_DIRS = (
os.path.join(BASE_TEMPLATE_DIRS,'AssetsPoolApp'),
)
之后再views之中就可以寫入
return render_to_response('AppHtml/Login.html', {})
如果TEMLATE_DIRS中是‘AssetsPoolApp/AppHtml’
那么在views之中就不用謝AppHtml了
就是:return render_to_response(''Login.html,{})
---------------------
#########
html feeedback
Template-loader postmortem
Django tried loading these templates, in this order:
Using engine django:
django.template.loaders.filesystem.Loader: D:\Program Files\JetBrains\learn_models\learn_models\templates\people\dashboard.html (Source does not exist)
?
########
settings.py 靜態(tài)文件相關示例代碼及說明:
1https://code.ziqiangxuetang.com/django/django-static-files.html
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/
STATIC_URL = '/static/'
# 當運行 python manage.py collectstatic 的時候
# STATIC_ROOT 文件夾 是用來將所有STATICFILES_DIRS中所有文件夾中的文件,以及各app中static中的文件都復制過來
# 把這些文件放到一起是為了用apache等部署的時候更方便
STATIC_ROOT = os.path.join(BASE_DIR, 'collected_static')
# 其它 存放靜態(tài)文件的文件夾,可以用來存放項目中公用的靜態(tài)文件,里面不能包含 STATIC_ROOT
# 如果不想用 STATICFILES_DIRS 可以不用,都放在 app 里的 static 中也可以
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "common_static"),
'/path/to/others/static/', # 用不到的時候可以不寫這一行
)
# 這個是默認設置,Django 默認會在 STATICFILES_DIRS中的文件夾 和 各app下的static文件夾中找文件
# 注意有先后順序,找到了就不再繼續(xù)找了
STATICFILES_FINDERS = (
"django.contrib.staticfiles.finders.FileSystemFinder",
"django.contrib.staticfiles.finders.AppDirectoriesFinder"
)
靜態(tài)文件放在對應的 app 下的 static 文件夾中 或者 STATICFILES_DIRS 中的文件夾中。
?
################
0118
https://blog.csdn.net/u012041204/article/details/80208949?utm_source=blogxgwz7
WARNINGS:
?: (2_0.W001) Your URL pattern '^$' has a route that contains '(?P<', begins with a '^', or ends with a '$'. This was likely an oversight when migrating to
django.urls.path().
?: (2_0.W001) Your URL pattern '^detail/(?P<asset_id>[0-9]+)/$' [name='detail'] has a route that contains '(?P<', begins with a '^', or ends with a '$'. Thi
s was likely an oversight when migrating to django.urls.path().
今天在使用Django時遇到上面的警告,雖然只是警告,但是卻會導致網(wǎng)站無法訪問。這問題是Django新版本改變導致URL中不需要再使用正則表達式了,只需要路徑就OK了。
urlpatterns = [
path('admin/', admin.site.urls),
path('login/', views.login,name='login'),
path('',views.index,name='index'),
#此處設置為首頁,以前寫法是'^$',新版本不再使用^、$,只需要''就可以
]
---------------------
?
https://blog.csdn.net/judyge/article/details/49618147?utm_source=blogkpcl8
django--靜態(tài)文件路徑和模板路徑配置
https://blog.csdn.net/jzdzhiyun/article/details/5282512
https://docs.djangoproject.com/en/2.1/howto/static-files/
https://docs.djangoproject.com/en/2.1/ref/contrib/staticfiles/
python manage.py findstatic dist\css\adminlte.css
Found 'dist\css\adminlte.css' here:
D:\Program Files\JetBrains\learn_models\learn_models\static\dist\css\adminlte.css
D:\Program Files\JetBrains\learn_models\learn_models\people\static\dist\css\adminlte.css
?
python manage.py collectstatic
return os.path.exists(self.path(name))
File "D:\Program Files\JetBrains\learn_models\venv\lib\site-packages\django\contrib\staticfiles\storage.py", line 43, in path
raise ImproperlyConfigured("You're using the staticfiles app "
django.core.exceptions.ImproperlyConfigured: You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path.
STATIC_ROOT = os.path.join(BASE_DIR,'static')
?
0124
https://code.ziqiangxuetang.com/django/django-static-files.html
clone 項目,基本測試 python manage.py runserver (重要,學習造車)
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'collected_static')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "common_static"),
)
靜態(tài)文件放在對應的 app 下的 static 文件夾中 或者 STATICFILES_DIRS 中的文件夾中。
當 DEBUG = True 時,Django 就能自動找到放在里面的靜態(tài)文件。(Django 通過 STATICFILES_FINDERS 中的“查找器”,找到符合的就停下來,尋找的過程 類似于 Python 中使用 import xxx 時,找 xxx 這個包的過程)。
示例項目 dj18static, 應用 app 下面有一個 static 里面有一個 zqxt.png 圖片:
按照文件夾的順序,自上而下的找。
?
base.html 是在根目錄下
{% extends 'base.html' %}
{% block title %}儀表盤{% endblock %}
{% load static %}
?
0126 導入models
TypeError: __init__() missing 1 required positional argument: 'on_delete'
將第十一行的代碼改為:
herobook=models.ForeignKey('BookInfo',on_delete=models.CASCADE,)
即在外鍵值的后面加上 on_delete=models.CASCADE 'on_delete'
舉例說明:
user=models.OneToOneField(User)
owner=models.ForeignKey(UserProfile)
需要改成:
user=models.OneToOneField(User,on_delete=models.CASCADE) --在老版本這個參數(shù)(models.CASCADE)是默認值
owner=models.ForeignKey(UserProfile,on_delete=models.CASCADE) --在老版本這個參數(shù)(models.CASCADE)是默認值
?
# Django 1.7 及以上的版本需要用以下命令 blogs.py 解析數(shù)據(jù)庫
python manage.py makemigrations
python manage.py migrate
?
people.Article.tags: (fields.E300) Field defines a relation with model 'Tag', which is either not installed, or is abstract.
people.Asset.tags: (fields.E300) Field defines a relation with model 'Tag', which is either not installed, or is abstract.
solution:
comment old tables;
解釋forgrin-key ,aand ManyToManyField
ForeignKey?
https://docs.djangoproject.com/en/1.8/ref/models/fields/
class ForeignKey(othermodel, **options)[source]?
A many-to-one relationship. Requires a positional argument: the class to which the model is related.
To create a recursive relationship – an object that has a many-to-one relationship with itself – use models.ForeignKey('self').
If you need to create a relationship on a model that has not yet been defined, you can use the name of the model, rather than the model object itself:
from django.db import models
class Car(models.Model):
manufacturer = models.ForeignKey('Manufacturer')
# ...
class Manufacturer(models.Model):
# ...
pass
?
#sample
class Blog(models.Model):
name = models.CharField(max_length=100)
tagline = models.TextField()
class Entry(models.Model):
blog = models.ForeignKey(Blog)
headline = models.CharField(max_length=255)
當有一對多,多對一,或者多對多的關系的時候,先把相關的對象查詢出來
>>> from blog.models import Entry
>>> entry = Entry.objects.get(pk=1)
>>> cheese_blog = Blog.objects.get(name="Cheddar Talk")
>>> entry.blog = cheese_blog
>>> entry.save()
?
?
https://docs.djangoproject.com/en/1.8/topics/db/examples/many_to_many/
https://www.cnblogs.com/zhaogaolong/p/5220975.html
class Publication(models.Model):
title = models.CharField(max_length=30)
def __str__(self): # __unicode__ on Python 2
return self.title
class Meta:
ordering = ('title',)
class Article(models.Model):
headline = models.CharField(max_length=100)
publications = models.ManyToManyField(Publication)
?
>>> p1 = Publication(title='The Python Journal')
>>> p1.save()
>>> p2 = Publication(title='Science News')
>>> p2.save()
>>> p3 = Publication(title='Science Weekly')
>>> p3.save()
>>> a1 = Article(headline='Django lets you build Web apps easily')
a1.publications.add(p1)
?
>>> a2 = Article(headline='NASA uses Python')
>>> a2.save()
>>> a2.publications.add(p1, p2)
>>> a2.publications.add(p3)
?
Create and add a Publication to an Article in one step using create():
>>> new_publication = a2.publications.create(title='Highlights for Children')
?
>>> a1.publications.all()
[<Publication: The Python Journal>]
>>> a2.publications.all()
[<Publication: Highlights for Children>, <Publication: Science News>, <Publication: Science Weekly>, <Publication: The Python Journal>]
?
使用UTF-8 建庫
mysql> show create database people
-> ;
+----------+----------------------------------------------------------------+
| Database | Create Database |
+----------+----------------------------------------------------------------+
| people | CREATE DATABASE `people` /*!40100 DEFAULT CHARACTER SET gbk */ |
+----------+----------------------------------------------------------------+
1 row in set (0.00 sec)
mysql>drop database people;
mysql>create database people default character set=utf8;
?
admin 平臺添加超級管理員
python manage.py createsuperuser
下面,我們通過后臺admin界面,多增加幾個服務器實例,并修改其類型、業(yè)務線、狀態(tài)、廠商、機房、標簽,再刷新資產(chǎn)總表,可以看到效果如下:
add following in <app_name>/admin.py
people/admin.py
from django.contrib import admin
# Register your models here.
from .models import Asset
admin.site.register(Asset)
and visit:
http://127.0.0.1:8000/admin/people/
?
?
django.urls.exceptions.NoReverseMatch: 'assets' is not a registered namespace
點擊local vars:
url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app) ...
▼ Local vars
view_name
'assets:detail'
index url 含有如下字塊:
<td><a href="{% url 'assets:detail' asset.id %}">{{ asset.name }}</a></td>
解決辦法:
URLS 拆分每個app 有urls , template 拆分,主目錄和app目錄都有template
根目錄URL 加入name.space https://blog.csdn.net/mwmoo/article/details/53783140
path('assets/', include('people.urls', namespace='asset')),
?
?
?
https://www.cnblogs.com/qunxiadexiaoxiangjiao/p/8891227.html
Django中使用locals()函數(shù)的技巧
對 current_datetime 的一次賦值操作:
1
2
3
def current_datetime(request):
now = datetime.datetime.now()
return render_to_response('current_datetime.html', {'current_date': now})
很多時候,就像在這個范例中那樣,你發(fā)現(xiàn)自己一直在計算某個變量,保存結果到變量中(比如前面代碼中的 now ),然后將這些變量發(fā)送給模板。 尤其喜歡偷懶的程序員應該注意到了,不斷地為臨時變量和臨時模板命名有那么一點點多余。 不僅多余,而且需要額外的輸入。
如果你是個喜歡偷懶的程序員并想讓代碼看起來更加簡明,可以利用 Python 的內(nèi)建函數(shù) locals() 。它返回的字典對所有局部變量的名稱與值進行映射。 因此,前面的視圖可以重寫成下面這個樣子:
1
2
3
def current_datetime(request):
current_date = datetime.datetime.now()
return render_to_response('current_datetime.html', locals())
在此,我們沒有像之前那樣手工指定 context 字典,而是傳入了 locals() 的值,它囊括了函數(shù)執(zhí)行到該時間點時所定義的一切變量。 因此,我們將 now 變量重命名為 current_date ,因為那才是模板所預期的變量名稱。 在本例中, locals() 并沒有帶來多 大 的改進,但是如果有多個模板變量要界定而你又想偷懶,這種技術可以減少一些鍵盤輸入。
使用 locals() 時要注意是它將包括 所有 的局部變量,它們可能比你想讓模板訪問的要多。 在前例中, locals() 還包含了 request 。對此如何取舍取決你的應用程序。
?
##########
demo.js:14 Uncaught TypeError: $(...).controlSidebar is not a function
at HTMLDocument.<anonymous> (demo.js:14)
at i (jquery-2.2.3.min.js:2)
at Object.fireWith [as resolveWith] (jquery-2.2.3.min.js:2)
at Function.ready (jquery-2.2.3.min.js:2)
at HTMLDocument.J (jquery-2.2.3.min.js:2)
替代APP下的static 的 demo.js ,原先是19k, 替代完成是17K
D:\Program Files\JetBrains\learn_models\learn_models\people\static\dist\js
參考https://stackoverflow.com/questions/36025814/sidebar-is-not-a-function-error
Ask Question
1
I'm trying to get the most basic sidebar example running using examples from here. Here's my HTML:
<!DOCTYPE HTML>
<html>
<head>
<link rel="stylesheet" type="text/css" href="./css/sidebar.min.css" />
<script type="text/javascript" src="./css/sidebar.min.js"></script>
</head>
<body>
<div class="ui right vertical menu sidebar">
<div class="item">foo</div>
<div class="item">bar</div>
</div>
<div class="pusher">
Your site's actual content
</div>
</body>
</html>
This shows no sidebar; when I type $('.ui.sidebar').sidebar('toggle') into the developer console I get this error:
VM387:2 Uncaught TypeError: $(...).sidebar is not a function(…)
Using Chrome 49 on Windows 8.
How do I get the sidebar to show correctly?
jquery sidebar semantic-ui
shareimprove this question
asked Mar 16 '16 at 2:30
Nate Glenn
3,27952978
First off, in the network tab, are you getting a 200 OK status code from the sidebar js file? – Ben Sewards Mar 16 '16 at 2:32
Plz check the path of js file included. Are you sure it is in the css folder? – Aju John Mar 16 '16 at 2:33
@BenSewards yes, resources are loading correctly. – Nate Glenn Mar 16 '16 at 2:34
2
@NateGlenn Could be that you're not including the Jquery lib – Robin Carlo Catacutan Mar 16 '16 at 2:43
@RobinCarloCatacutan You're right. I thought it was included, since $('.ui.sidebar') didn't throw an error. Now that this is working, it looks completely crazy though. The entire page except for the line where "Your site's actual content" is is all grayed out. – Nate Glenn Mar 16 '16 at 2:52
###########20190129
###########20190130
模板語言
注意是占比,不是數(shù)量!
############
js 說明:
1.
按照AdminLTE中提供的示例,在HTML中添加相應的標簽,在script中添加相應的JS代碼(jQueryKnob)。JS代碼基本照抄,不需要改動。對于顯示的圓圈,可以修改其顏色、大小、形態(tài)、是否只讀等屬性,可以參照AdminLTE中的范例。
最重要的是,需要從數(shù)據(jù)庫中獲取相應的數(shù)據(jù),修改assets/views.py中的dashboard視圖,最終如下:
2.
關鍵是series列表,其中的type指定該charts是什么類型,bar表示柱狀圖,而data就是至關重要的具體數(shù)據(jù)了,利用模板語言,將從數(shù)據(jù)庫中獲取的具體數(shù)值傳入進來,Echarts插件會根據(jù)數(shù)值進行動態(tài)調(diào)整。
?
3.
series中的type指定為pie類型表示餅圖,data列表動態(tài)傳入各種資產(chǎn)類型的數(shù)量。其它的設置可參考官方文檔。
為了展示的方便,我們在admin中新建一些網(wǎng)絡設備、安全設備、軟件資產(chǎn)等其它類型的資產(chǎn),然后查看資產(chǎn)總表和餅圖。這里我分別添加了一臺網(wǎng)絡、安全和存儲設備和兩個軟件資產(chǎn)。
?
######
模型說明:
4.機房、制造商、業(yè)務線、合同、資產(chǎn)標簽等數(shù)據(jù)模型
說明:
每臺安全、網(wǎng)絡、存儲設備都通過一對一的方式唯一關聯(lián)這一個資產(chǎn)對象。 (asset_id 來關聯(lián))
通過sub_asset_type又細分設備的子類型
對于軟件,它沒有物理形體,因此無須關聯(lián)一個資產(chǎn)對象;
軟件只管理那些大型的收費軟件,關注點是授權數(shù)量和軟件版本。對于那些開源的或者免費的軟件,顯然不算公司的資產(chǎn)
?
?
說明
機房可以有很多其它字段,比如城市、樓號、樓層和未知等等,如有需要可自行添加;
業(yè)務線可以有子業(yè)務線,因此使用一個外鍵關聯(lián)自身模型;
合同模型主要存儲財務部門關心的數(shù)據(jù);
資產(chǎn)標簽模型與資產(chǎn)是多對多的關系。
?
########項目需求
二、項目需求分析
本項目不是一個完整的的CMDB系統(tǒng),重點針對服務器資產(chǎn)的自動數(shù)據(jù)收集、報告、接收、審批、更新和展示,搭建一個基礎的面向運維的主機管理平臺。
下面是項目需求的總結:
盡可能存儲所有的IT資產(chǎn)數(shù)據(jù),但不包括外設、優(yōu)盤、顯示器這種屬于行政部門管理的設備;
硬件信息可自動收集、報告、分析、存儲和展示;
具有后臺管理人員的工作界面;
具有前端可視化展示的界面;
具有日志記錄功能;
數(shù)據(jù)可手動添加、修改和刪除。
當然,實際的CMDB項目需求絕對不止這些,還有諸如用戶管理、權限管理、API安全認證、REST設計等等。
三、資產(chǎn)分類
資產(chǎn)種類眾多,不是所有的都需要CMDB管理,也不是什么都是CMDB能管理的。
下面是一個大致的分類,不一定準確、全面:
資產(chǎn)類型包括:
服務器
存儲設備
安全設備
網(wǎng)絡設備
軟件資產(chǎn)
服務器又可分為:
刀片服務器
PC服務器
小型機
大型機
其它
存儲設備包括:
磁盤陣列
網(wǎng)絡存儲器
磁帶庫
磁帶機
其它
安全設備包括:
防火墻
入侵檢測設備
互聯(lián)網(wǎng)網(wǎng)關
漏洞掃描設備
數(shù)字簽名設備
上網(wǎng)行為管理設備
運維審計設備
加密機
其它
網(wǎng)絡設備包括:
路由器
交換器
負載均衡
VPN
流量分析
其它
軟件資產(chǎn)包括:
操作系統(tǒng)授權
大型軟件授權
數(shù)據(jù)庫授權
其它
其中,服務器是運維部門最關心的,也是CMDB中最主要、最方便進行自動化管理的資產(chǎn)。
服務器又可以包含下面的部件:
CPU
硬盤
內(nèi)存
網(wǎng)卡
除此之外,我們還要考慮下面的一些內(nèi)容:
機房
業(yè)務線
合同
管理員
審批員
資產(chǎn)標簽
其它未盡事宜
大概對資產(chǎn)進行了分類之后,就要詳細考慮各細分數(shù)據(jù)條目了。
共有數(shù)據(jù)條目:
有一些數(shù)據(jù)條目是所有資產(chǎn)都應該有的,比如:
資產(chǎn)名稱
資產(chǎn)sn
所屬業(yè)務線
設備狀態(tài)
制造商
管理IP
所在機房
資產(chǎn)管理員
資產(chǎn)標簽
合同
價格
購買日期
過保日期
批準人
批準日期
數(shù)據(jù)更新日期
備注
另外,不同類型的資產(chǎn)還有各自不同的數(shù)據(jù)條目,例如服務器:
服務器:
服務器類型
添加方式
宿主機
服務器型號
Raid類型
操作系統(tǒng)類型
發(fā)行版本
操作系統(tǒng)版本
其實,在開始正式編寫CMDB項目代碼之前,對項目的需求分析準確與否,數(shù)據(jù)條目的安排是否合理,是決定整個CMDB項目成敗的關鍵。這一部分工作看似簡單其實復雜,看似無用其實關鍵,做好了,項目基礎就牢固,沒做好,推到重來好幾遍很正常!
?
( 重要
通過前面的內(nèi)容,我們可以看出CMDB數(shù)據(jù)模型的設計非常復雜,我們這里還是省略了很多不太重要的部分,就這樣總共都有400多行代碼。其中每個模型需要保存什么字段、采用什么類型、什么關聯(lián)關系、定義哪些參數(shù)、數(shù)據(jù)是否可以為空,這些都是踩過各種坑后總結出來的,不是隨便就能定義的。所以,請務必詳細閱讀和揣摩這些模型的內(nèi)容。
一切沒有問題之后,注冊app,然后makemigrations以及migrate!)
?
#######################20190207
( 今天學習實習下windows 收集信息和linux 下收集信息,封裝字典,二級路由,json格式,構造,轉(zhuǎn)換,csrf安全令牌,打包到data字典)
(分散管理,函數(shù)報錯,調(diào)試單個.py 文件)
本模塊基于windows操作系統(tǒng),依賴wmi和win32com庫,需要提前使用pip進行安裝,
或者下載安裝包手動安裝。
pip install wmi
pip install win32com
conf/setting.conf
30.1.245.138
or
21.15.26.211
python manage.py runserver 30.1.245.138:8000
or
python manage.py runserver 21.15.26.211:8000
?
File "main.py", line 16, in <module>
handler.ArgvHandler(sys.argv)
?
#######################20190219
( 今天學習實習下django 的后端構造,admin 展示管理,創(chuàng)建,邏輯,流程,對象,方法,views 過于龐大,.py 核心業(yè)務,特別好用的update_or_create(),字典,發(fā)送數(shù)據(jù))
(自定義admin,調(diào)用實例方法,創(chuàng)建測試用例,如何上線,添加,入口方法,利用request.user,提前獲取,核心方法,資產(chǎn)對象,擴展接口)
#######################20190220
(今天繼續(xù)學習django 后臺端構造,Exception,report_data,擴展接口,日志類型,反射,更新方法,記錄日志,字典格式,復用的函數(shù),set類型的差集功能,key,
defaults數(shù)據(jù)字典,內(nèi)存對象,運行腳本,報告數(shù)據(jù),pass)
轉(zhuǎn)載于:https://www.cnblogs.com/feiyun8616/p/10362217.html
總結
以上是生活随笔為你收集整理的python django bootstrap_导入 201901的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Autodesk Forge Viewe
- 下一篇: CentOS 7 更换 yum 源