(Django)取得目前URL/app_name

1
2
3
4
5
6
7
8
from django.urls import resolve

def get_current_url(request):
...
current_url = resolve(request.path_info).url_name
app_name = resolve(request.path_info).app_name
print("current_url: %s" % current_url)
print("app_name: %s" % app_name)

在template取得app_name

1
{{request.resolver_match.app_name}}

(Django)資料庫內密碼欄位加密 - make_password

  1. import make_password
  2. use make_password() function
1
2
3
4
5
6
7
from django.contrib.auth.hashers import make_password

class MyView(CreateView):
...
def form_valid(self, form):
pwd = form.cleaned_data['password']
pwd = make_password(pwd)