找到setting.py
加上
mysite/settings.py
TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates')]这是目录:.├── db.sqlite3
├── jason_auth
│ ├── admin.py
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── migrations
│ │ └── __init__.py
│ ├── models.py
│ ├── tests.py
│ ├── urls.py
│ ├── urls.pyc
│ ├── views.py
│ └── views.pyc
├── jason_pro
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── settings.py
│ ├── settings.pyc
│ ├── urls.py
│ ├── urls.pyc
│ ├── wsgi.py
│ └── wsgi.pyc
├── manage.py
├── static
│ └── jason_auth
└── templates
└── jason_auth
├── login_failed.html
└── login_success.html2:About parameter.For example:I want to pass string 'jason' to my templatein a view function:we use render(a shortcut)code:return render(request,'app_name/template_name.html',{'jason':'data'}##note:jason is the name that template will receive and recoginze and data is the data you want to pass. It can be an object
3:See the Model function!It is powerful.Come on!
Record the main process:1:write your home template(contains a form)in the form you need to add {%csrf_token%} to avoid sctfif not ,you will receive 403 errorThe action is the view you want to handle the request2:I try to create a view named "login" failed. And struggled for a long time:Because I import a login model from django, When I want to use the login from django, it will call the login I defined.
So first change the name to login_view in view.pyNext change urls.py views.login to view.login_view
Conclusion: Make sure each view endswith _view. That will be much better3:Got a good point: The @ decorator:For example:One user want to see a login_required view.you add @login_required :::: from django.contrib.auth.decorators import login_required@login_requireddef my_view(): youR busssiness....4::user create#1 Using default userCode:from django.contrib.auth.models import User#return a User User.objects.create(username="username",email="test@gmail.com")
5:Custom my User modelTIPS::About UserChangeForm
Class MyUserAdmin(UserAdmin): #with it can change user profile form = UserChangeForm add_form = UserCreationForm6:Add static css file 1:In settings.py add STATICFILES_DIRS = [os.path.join(BASE_DIR,"static")] 2:In a template file add {%load staticfiles%} 3:When you need to use a static file <link rel="stylesheet" type="text/css" href="{%static "loginsys/style.css"%}">
Here is Tree :├── db.sqlite3
├── loginsys
│ ├── admin.py
│ ├── admin.pyc
│ ├── forms.py
│ ├── forms.pyc
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── migrations
│ │ ├── 0001_initial.py
│ │ ├── 0001_initial.pyc
│ │ ├── __init__.py
│ │ └── __init__.pyc
│ ├── models.py
│ ├── models.pyc
│ ├── tests.py
│ ├── urls.py
│ ├── urls.pyc
│ ├── views.py
│ └── views.pyc
├── manage.py
├── static
│ └── loginsys
│ └── style.css
├── templates
│ └── loginsys
│ └── register.html
└── usersys
├── __init__.py
├── __init__.pyc
├── settings.py
├── settings.pyc
├── urls.py
├── urls.pyc
├── wsgi.py
└── wsgi.pyc