暗黑模式
Install Django
- Way 1: use cookiecutter-django(popular django project template)
- Way 2: use django itself
Start Coding
- Django Start
- Code samples: Intro to Django
- Django Full Docs
- Must Read: Django Overview
- Must Read: Django tutorial
Templates and Views
python
TEMPLATES = [
{
'DIRS': [BASE_DIR / 'templates'], # 项目级模板目录,优先在这里寻找模板文件
'APP_DIRS': True, # 启用应用级模板目录,DIRS 如果找不到,就在这里找
},
]
1
2
3
4
5
6
2
3
4
5
6
python
class UserUpdateView(LoginRequiredMixin, SuccessMessageMixin, UpdateView):
model = User
fields = ["name"]
success_message = _("Information successfully updated")
template_name = "custom_templates/user_update.html" # 自定义模板路径,如果省略则默认:<app_label>/<model_name>_form.html,例如 {DIRS}/users/user_form.html
...
def get_template_names(self) -> list[str]: # 动态选择模板
if self.request.user.is_staff:
return ["admin_templates/user_update.html"]
return ["user_templates/user_update.html"]
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
URL Config
python
# 入口文件:ROOT_URLCONF,ex. config/urls.py
urlpatterns = [...]
1
2
2