python_wiki:django_configuration

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Next revision Both sides next revision
python_wiki:django_configuration [2018/08/03 15:53]
billdozor [Database Setup]
python_wiki:django_configuration [2018/08/14 23:47]
billdozor [Django: Jquery]
Line 3: Line 3:
 **General Information** **General Information**
  
-Configuring the Django Web Framework and its dependencies. +Configuring the Django Web Framework and its dependencies.
  
 +\\
 +The **EXAMPLE** sections of code end up building an inventory website.
 +
 +\\
 **Checklist** **Checklist**
   * [[python_wiki:django_install|Django installed]]   * [[python_wiki:django_install|Django installed]]
Line 24: Line 28:
   * Run secure setup<code bash>mysql_secure_installation</code>   * Run secure setup<code bash>mysql_secure_installation</code>
     * Prompts for the following:     * Prompts for the following:
 +      * Current password for root (should be none, just press enter)
       * Set database root password       * Set database root password
       * Remove anonymous users       * Remove anonymous users
Line 46: Line 51:
 Configuring Django. Configuring Django.
  
-  * Verify django works<code bash>python+  * Verify django works<code python>python
 >>> import django >>> import django
 >>> print(django.get_version()) >>> print(django.get_version())
Line 113: Line 118:
   #asset_type choices   #asset_type choices
   DEVICE_TYPES = (    DEVICE_TYPES = ( 
-    ('Firewall', 'Firewall'),  +    ('Firewalls', 'Firewalls'),  
-    ('Router', 'Router'),  +    ('Routers', 'Routers'),  
-    ('Server', 'Server'), +    ('Servers', 'Servers'), 
     ('Storage', 'Storage'),      ('Storage', 'Storage'), 
-    ('Switch', 'Switch'),  +    ('Switches', 'Switches'),  
-    ('Workstation', 'Workstation'),+    ('Workstations', 'Workstations'),
   )   )
  
Line 231: Line 236:
   * Create logging directory and setup ownership/permissions<code bash>mkdir /var/log/myprojecthere   * Create logging directory and setup ownership/permissions<code bash>mkdir /var/log/myprojecthere
 chown :apache /var/log/myprojecthere chown :apache /var/log/myprojecthere
-chmod g+rwx /var/log/myprojecthere</code>+chmod g+rwxs /var/log/myprojecthere</code>
  
 ===== Admin Interface ===== ===== Admin Interface =====
Line 237: Line 242:
   * Create an admin user<code bash>python manage.py createsuperuser</code>   * Create an admin user<code bash>python manage.py createsuperuser</code>
  
-  * Make your models/objects available for editing in the admin portal (/home/django/myprojecthere/myapphere/admin.py)<code bash># -*- coding: utf-8 -*-+  * Make your models/objects available for editing in the admin portal (/home/django/myprojecthere/myapphere/admin.py)<code python># -*- coding: utf-8 -*-
 from __future__ import unicode_literals from __future__ import unicode_literals
  
Line 290: Line 295:
   url(r'^(?P<env_name>(dev|test|prod))/$', views.env, name='env'),   url(r'^(?P<env_name>(dev|test|prod))/$', views.env, name='env'),
  
-  # /myapphere/device/  - Specific Device, All Environments +  # /myapphere/device_type/  - Specific Device, All Environments 
-  url(r'^(?P<device>(servers|workstations|switches|routers|firewalls))/$', views.device, name='device'),+  url(r'^(?P<device_type>(firewalls|routers|servers|storage|switches|workstations))/$', views.asset_type_all_env, name='asset_type_all_env'),
  
-  # /myapphere/device/env_name  -  Specific Device, Specific Environment +  # /myapphere/device_type/env_name  -  Specific Device, Specific Environment 
-  url(r'^(?P<device>(servers|workstations|switches|routers|firewalls))/(?P<env_name>(dev|test|prod))/$', views.device_env, name='device_env'),+  url(r'^(?P<device_type>(firewalls|routers|servers|storage|switches|workstations))/(?P<env_name>(dev|test|prod))/$', views.type_env, name='type_env'), 
 + 
 +  # /myapphere/<asset_hardware> - Physical or Virtual 
 +  url(r'^(?P<asset_hardware>(physical|virtual))/$', views.asset_hardware, name='asset_hardware'),
 ]</code> ]</code>
  
Line 345: Line 353:
   asset_type_list = [key for key,value in AssetEntry.DEVICE_TYPES]   asset_type_list = [key for key,value in AssetEntry.DEVICE_TYPES]
  
-  # Get all os names +  # Get all os names, Linux only, Windows only
   os_list = [key for key,value in AssetEntry.OS_NAMES]   os_list = [key for key,value in AssetEntry.OS_NAMES]
 +  os_list_linux = [key for key,value in AssetEntry.OS_NAMES_LINUX]
 +  os_list_windows = [key for key,value in AssetEntry.OS_NAMES_WINDOWS]
  
   # Log for debug purposes   # Log for debug purposes
Line 361: Line 371:
   sysstats_grand_physical_linux = 0   sysstats_grand_physical_linux = 0
   sysstats_grand_physical_windows = 0   sysstats_grand_physical_windows = 0
 +  sysstats_grand_physical_other = 0
   sysstats_grand_virtual = 0   sysstats_grand_virtual = 0
   sysstats_grand_virtual_linux = 0   sysstats_grand_virtual_linux = 0
   sysstats_grand_virtual_windows = 0   sysstats_grand_virtual_windows = 0
 +  sysstats_grand_virtual_other = 0
   sysstats_grand_vmware = 0   sysstats_grand_vmware = 0
  
Line 387: Line 399:
     count_physical_linux = 0     count_physical_linux = 0
     count_physical_windows = 0     count_physical_windows = 0
 +    count_physical_other = 0
     count_virtual = 0     count_virtual = 0
     count_virtual_linux = 0     count_virtual_linux = 0
     count_virtual_windows = 0     count_virtual_windows = 0
 +    count_virtual_other = 0
     for node in device_stats:     for node in device_stats:
       if node.asset_hardware == "Physical":       if node.asset_hardware == "Physical":
         count_physical += 1         count_physical += 1
  
-        if node.asset_os.startswith("CentOS") or node.asset_os.startswith("Oracle") or node.asset_os.startswith("VMware")+        if node.asset_os in os_list_linux
-          # Physical+Linux Count (include physical virtual vmware hosts)+          # Physical+Linux Count
           count_physical_linux += 1           count_physical_linux += 1
-        elif node.asset_os.startswith("Win"):+        elif node.asset_os in os_list_windows:
           # Physical+Windows Count           # Physical+Windows Count
           count_physical_windows += 1           count_physical_windows += 1
 +        else:
 +          # Physical+Other OS
 +          count_physical_other += 1
  
       elif node.asset_hardware == "Virtual":       elif node.asset_hardware == "Virtual":
Line 405: Line 422:
         count_virtual += 1         count_virtual += 1
  
-        if node.asset_os.startswith("CentOS") or node.asset_os.startswith("Oracle"):+        if node.asset_os in os_list_linux:
           # Virtual+Linux Count           # Virtual+Linux Count
           count_virtual_linux += 1           count_virtual_linux += 1
-        elif node.asset_os.startswith("Win"):+        elif node.asset_os in os_list_windows:
           # Virtual+Windows Count           # Virtual+Windows Count
           count_virtual_windows += 1           count_virtual_windows += 1
 +        else:
 +          # Virtual+Other OS
 +          count_virtual_other += 1
  
     #- Add to grand total physical     #- Add to grand total physical
Line 416: Line 436:
     sysstats_grand_physical_linux += count_physical_linux     sysstats_grand_physical_linux += count_physical_linux
     sysstats_grand_physical_windows += count_physical_windows     sysstats_grand_physical_windows += count_physical_windows
 +    sysstats_grand_physical_other += count_physical_other
  
     #- Add to grand total virtual     #- Add to grand total virtual
Line 421: Line 442:
     sysstats_grand_virtual_linux += count_virtual_linux     sysstats_grand_virtual_linux += count_virtual_linux
     sysstats_grand_virtual_windows += count_virtual_windows     sysstats_grand_virtual_windows += count_virtual_windows
 +    sysstats_grand_virtual_other += count_virtual_other
  
     ####---- OS Counts Table ----####     ####---- OS Counts Table ----####
Line 441: Line 463:
     ####---- After All Stats Calculations: Add Device Stats to stats_list ----####     ####---- After All Stats Calculations: Add Device Stats to stats_list ----####
     # Add device statistics to stats_list     # Add device statistics to stats_list
-    stats_list.append({ 'asset_type': device_type, 'total': count_total, 'physical': count_physical, 'physical_linux': count_physical_linux, 'physical_windows': count_physical_windows, 'virtual': count_virtual, 'virtual_linux': count_virtual_linux, 'virtual_windows': count_virtual_windows, 'os_count_stats': os_counts })+    stats_list.append({ 'asset_type': device_type, 'total': count_total, 'physical': count_physical, 'physical_linux': count_physical_linux, 'physical_windows': count_physical_windows, 'physical_other': count_physical_other, 'virtual': count_virtual, 'virtual_linux': count_virtual_linux, 'virtual_windows': count_virtual_windows, 'virtual_other': count_virtual_other, 'os_count_stats': os_counts })
  
     # Grand Total Only: VMware ESXi Host Count     # Grand Total Only: VMware ESXi Host Count
Line 469: Line 491:
  
   ####---- Add grand totals to stats_list ----####   ####---- Add grand totals to stats_list ----####
-  stats_list.append({ 'asset_type': 'Total', 'total': sysstats_grand_total, 'physical': sysstats_grand_physical, 'physical_linux': sysstats_grand_physical_linux, 'physical_windows': sysstats_grand_physical_windows, 'virtual': sysstats_grand_virtual, 'virtual_linux': sysstats_grand_virtual_linux, 'virtual_windows': sysstats_grand_virtual_windows, 'os_count_stats': os_counts_total })+  stats_list.append({ 'asset_type': 'Total', 'total': sysstats_grand_total, 'physical': sysstats_grand_physical, 'physical_linux': sysstats_grand_physical_linux, 'physical_windows': sysstats_grand_physical_windows, 'physical_other': sysstats_grand_physical_other, 'virtual': sysstats_grand_virtual, 'virtual_linux': sysstats_grand_virtual_linux, 'virtual_windows': sysstats_grand_virtual_windows, 'virtual_other': sysstats_grand_virtual_other, 'os_count_stats': os_counts_total })
  
   #- Other Grand Totals: Calculate -#   #- Other Grand Totals: Calculate -#
   sysstats_grand_linux = sysstats_grand_physical_linux + sysstats_grand_virtual_linux   sysstats_grand_linux = sysstats_grand_physical_linux + sysstats_grand_virtual_linux
   sysstats_grand_windows = sysstats_grand_physical_windows + sysstats_grand_virtual_windows   sysstats_grand_windows = sysstats_grand_physical_windows + sysstats_grand_virtual_windows
 +  sysstats_grand_other = sysstats_grand_physical_other + sysstats_grand_virtual_other
  
   # Map template variable names (1st) to the Python variables (2nd) for use in templates   # Map template variable names (1st) to the Python variables (2nd) for use in templates
-  context = {'device_stats': stats_list, 'total_linux': sysstats_grand_linux, 'total_windows': sysstats_grand_windows, 'total_vmware': sysstats_grand_vmware, 'os_list': os_list}+  context = {'device_stats': stats_list, 'total_linux': sysstats_grand_linux, 'total_windows': sysstats_grand_windows, 'total_other': sysstats_grand_other, 'total_vmware': sysstats_grand_vmware, 'os_list': os_list}
  
   # Logging   # Logging
Line 884: Line 907:
  <thead>  <thead>
   <tr class="myapphere">   <tr class="myapphere">
-    <th class="myapphere">Device</th>+    <th class="myapphere">Device Type</th>
     <th class="myapphere">Name</th>     <th class="myapphere">Name</th>
     <th class="myapphere">Description</th>     <th class="myapphere">Description</th>
     <th class="myapphere">Environment</th>     <th class="myapphere">Environment</th>
     <th class="myapphere">OS</th>     <th class="myapphere">OS</th>
-    <th class="myapphere">Type</th>+    <th class="myapphere">Hardware</th>
   </tr>   </tr>
  </thead>  </thead>
  <tbody  <tbody
-  {% for name in all_assets_list %}+  {% for name in all_asset_list %}
     <tr class="myapphere">     <tr class="myapphere">
       <td class="myapphere">{{ name.asset_type }}</td>       <td class="myapphere">{{ name.asset_type }}</td>
Line 1028: Line 1051:
     <th class="myapphere">Physical Linux</th>     <th class="myapphere">Physical Linux</th>
     <th class="myapphere">Physical Windows</th>     <th class="myapphere">Physical Windows</th>
 +    <th class="myapphere">Physical Other</th>
     <th class="myapphere">Virtual</th>     <th class="myapphere">Virtual</th>
     <th class="myapphere">Virtual Linux</th>     <th class="myapphere">Virtual Linux</th>
     <th class="myapphere">Virtual Windows</th>     <th class="myapphere">Virtual Windows</th>
 +    <th class="myapphere">Virtual Other</th>
   </tr>   </tr>
 </thead> </thead>
Line 1042: Line 1067:
         <td class="myapphere center totals">{{ asset.physical_linux }}</td>         <td class="myapphere center totals">{{ asset.physical_linux }}</td>
         <td class="myapphere center totals">{{ asset.physical_windows }}</td>         <td class="myapphere center totals">{{ asset.physical_windows }}</td>
 +        <td class="myapphere center totals">{{ asset.physical_other }}</td>
         <td class="myapphere center totals">{{ asset.virtual }}</td>         <td class="myapphere center totals">{{ asset.virtual }}</td>
         <td class="myapphere center totals">{{ asset.virtual_linux }}</td>         <td class="myapphere center totals">{{ asset.virtual_linux }}</td>
         <td class="myapphere center totals">{{ asset.virtual_windows }}</td>         <td class="myapphere center totals">{{ asset.virtual_windows }}</td>
 +        <td class="myapphere center totals">{{ asset.virtual_other }}</td>
       {% else %}       {% else %}
         <td class="myapphere">{{ asset.asset_type }}</td>         <td class="myapphere">{{ asset.asset_type }}</td>
Line 1051: Line 1078:
         <td class="myapphere center">{{ asset.physical_linux }}</td>         <td class="myapphere center">{{ asset.physical_linux }}</td>
         <td class="myapphere center">{{ asset.physical_windows }}</td>         <td class="myapphere center">{{ asset.physical_windows }}</td>
 +        <td class="myapphere center">{{ asset.physical_other }}</td>
         <td class="myapphere center">{{ asset.virtual }}</td>         <td class="myapphere center">{{ asset.virtual }}</td>
         <td class="myapphere center">{{ asset.virtual_linux }}</td>         <td class="myapphere center">{{ asset.virtual_linux }}</td>
         <td class="myapphere center">{{ asset.virtual_windows }}</td>         <td class="myapphere center">{{ asset.virtual_windows }}</td>
 +        <td class="myapphere center">{{ asset.virtual_other }}</td>
       {% endif %}       {% endif %}
     </tr>     </tr>
Line 1064: Line 1093:
     <td class="myapphere totals">Total Windows</td>     <td class="myapphere totals">Total Windows</td>
     <td class="myapphere center">{{ total_windows }}</td>     <td class="myapphere center">{{ total_windows }}</td>
 +  </tr>
 +  <tr class="myapphere">
 +    <td class="myapphere totals">Total Other</td>                                          
 +    <td class="myapphere center">{{ total_other }}</td>
   </tr>   </tr>
   <tr class="myapphere">   <tr class="myapphere">
Line 1518: Line 1551:
 <a href="#" class="plain" onclick="$('#assetlist').tableExport({type:'excel',escape:'false'});"><img src="{% static 'myapphere/xls.png' %}" width="24px" title="Export Excel"></a></code> <a href="#" class="plain" onclick="$('#assetlist').tableExport({type:'excel',escape:'false'});"><img src="{% static 'myapphere/xls.png' %}" width="24px" title="Export Excel"></a></code>
  
 +----
 +
 +====== Django: Static Files ======
 +
 +No matter which steps from above you skip, you absolutely need to collect static files to copy them into the location Apache expects them to be.
 +
 +This is for Apache's access to javascript, css, etc.
   * Collect static files<code bash>cd /home/django/myprojecthere/   * Collect static files<code bash>cd /home/django/myprojecthere/
 python manage.py collectstatic</code> python manage.py collectstatic</code>
 +
 +----
 +
 +====== DEBUG: TURN OFF ======
 +
 +Lastly, when you are not developing/debugging your site, **turn off debug mode** to avoid dumping sensitive information to the screen in the event of an error.
 +
 +\\
 +Edit the project settings (/home/django/myprojecthere/myprojecthere/settings.py)<code python># SECURITY WARNING: don't run with debug turned on in production!
 +DEBUG = False</code>
  
 ---- ----
  
  • python_wiki/django_configuration.txt
  • Last modified: 2019/05/25 23:50
  • (external edit)