環境:Ubuntu18.04LTS
IDE:Jetbrain Pycharm 2018pro
Django:jango version 2.2.4
1、urls.py 路徑映射文件
'Specifying a namespace in include() without providing an app_name ' django.core.exceptions.ImproperlyConfigured: Specifying a namespace in include() without providing an app_name is not supported. Set the app_name attribute in the included module, or pass a 2-tuple containing the list of patterns and app_name instead.
原來代碼爲:
path('user/', include('user.urls', namespace='user')),
更改爲:
path('user/', include(('user.urls', 'user'), namespace='user')), # 用戶模塊
參考博客
https://blog.csdn.net/zoulonglong/article/details/79612973
2、忘記加什麼了吧
RuntimeError at /home/register_over
You called this URL via POST, but the URL doesn't end in a slash and you have APPEND_SLASH set. Django can't redirect to the slash URL while maintaining POST data. Change your form to point to localhost:8000/home/register_over/ (note the trailing slash), or set APPEND_SLASH=False in your Django settings.
結尾加"/".
參考博客:
https://blog.csdn.net/feng88724/article/details/7221925
3、版本問題:
?: (2_0.W001) Your URL pattern 'active/(?P<token>.*)' [name='active'] has a route that contains '(?P<', begins with a '^', or ends with a '$'. This was likely an oversight when migrating to django.urls.path().
解決:
導入:
from django.urls import path, re_path
將:
path('active/(?P<token>.*)', ActiveView.as_view(), name='active'), # 用戶郵箱激活,token是取到口令的名字
改成:
re_path(r'^active/(?P<token>.*)$', ActiveView.as_view(), name='active'), # 用戶郵箱激活,token是取到口令的名字
4、又是版本問題吧,Django的默認的AbstractUser的注意問題
按住Ctrl+鼠標點擊查看源碼,默認字段有:
- id :默認遞增
- password:默認加密
- last_login:默認NULL
- is_super:默認0
- username:默認None
- first_name:默認None
- last_name:默認None
- email:默認None
- is_staff:默認False
- is_active:默認True
問題:註冊一個賬戶,使用郵箱激活,要使用到默認的is_active字段,但是默認是True,所以在開始註冊用戶的時候改成了False,目的是想通過郵件激活,但是在使用其默認的authenticate()函數做驗證的時候,username和password都對,但是查出的用戶竟是None,原因就是 : django2.1版本以後authticate函數也會對用戶的is_active進行判斷,如果is_active爲false,則也會返回None。
解決:知道新特性,知道返回None的原因還有可能是is_actice爲false。
參考博客:
https://blog.csdn.net/qq_33445330/article/details/92078480