竟然还是Django 【关于template_dirs的设置】 搞了我一个小时,脑残了

template的默认目录在安装的目录里。所以要project 下重新设置。

找到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.html
2:About parameter.
For example:
I want to pass string 'jason' to my template
in 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 sctf
if not ,you will receive 403 error
The action is the view you want to handle the request
2: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.py
Next change urls.py views.login to view.login_view

Conclusion: Make sure each view endswith _view. That will be much better
3:
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_required
def my_view():
    youR busssiness....
4::
user create
#1 Using default user
Code:
from django.contrib.auth.models import User
#return a User 
User.objects.create(username="username",email="test@gmail.com")


5:Custom my User model
TIPS::About UserChangeForm

Class MyUserAdmin(UserAdmin):
    #with it can change user profile
      form = UserChangeForm
      add_form = UserCreationForm
6:
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


 
原文地址:https://www.cnblogs.com/-Doraemon/p/4700060.html