1. 程式人生 > >Python Django log日誌

Python Django log日誌

eat namespace decorator spa ora esp sca 建表 user

log日誌

log開發日誌

一、創建項目
    1、python -m venv ll_env  # 創建虛擬環境
    2、source ll_env/bin/activate  # 激活虛擬環境
    3、pip install django  # 安裝django    
    4、django-admin.py startproject project .  # 創建項目
    5、python manage.py migrate  # 創建數據庫
    6、python manage.py runserver  # 測試項目
二、創建應用程序
    
1、python manage.py startapp app # 創建應用程序 2、vi project/settings.py # 添加應用程序到INSTALLED_APPS列表中 INSTALLED_APPS.append(app) 3、vi project/urls.py # 添加URL映射 from django.urls import include urlpatterns.append(‘‘, include(app.urls, namespace=app)) 4、vi app/urls.py #
添加命名空間 app_name = app 5、vi app/views.py # 創建視圖 def view(request): ... 6、vi app/urls.py # 創建URL映射 urlpatterns.append(path(‘‘, views.view, name=view)) 7、vi app/templates/app/view.html # 創建視圖 8、vi app/models.py # 創建模型 9、vi app/admin.py # 註冊模型 10、python manage.py makemigrations app #
激活模型 python manage.py migrate 11、vi app/forms.py # 創建表單 from django import forms class form(forms.ModelForm): class Meta: model = model fields = [] labels = {} 12、vi app/views.py # 導入表單並使用 from .forms import form 13、vi app/templates/app/form.html # 在html中創建表單並將數據返回到視圖函數之中處理數據 三、創建用戶 1、python manage.py createsuperuser # 創建超級用戶 2、python manage.py startapp users # 創建用戶賬戶 3、vi project/settings.py # 將users添加到INSTALLED_APPS裏 4、vi project/urls.py # 添加users的URL映射 urlpatterns.append(path(users/, inculde(users.urls, namespace=users))) 5、vi users/urls.py # 添加users的命名空間 app_name = users 6、vi users/urls.py # 添加登錄URL映射 from django.urls import path from django.contrib.auth.views import LoginView urlpatterns.append(path(login/, LoginView.as_view(template_name=users/login.html), name=login})) 7、vi users/templates/users/login.html # 添加模板 8、vi app/templates/app/base.html # 添加狀態欄 {% if user.is_authenticated %} Hello, {{ user.username }} {% else %} <a href={% url users:login %}>log in</a> {% endif %} 9、vi project/settings.py # 重定向設置 LOGIN_URL = /users/login/ 10、vi app/models.py # 將模型關聯到客戶 from django.contrib.auth.models import User owner = models.ForeignKey(User, on_delete=models.CASCADE) 11、vi app/views.py # 添加訪問限制 from django.contrib.auth.decorators import login_required @login_required if topic.owner != request.user: raise Http404

Python Django log日誌