day84-2 django basis

A powerful web framework

django installation and startup

django installation:

  1. Enter cmd pip3 install django==1.11.22(note that adding a version number, this version is relatively stable, the current latest version is 2.x, but the bug more) -----> Recommended
  2. pycharm installation

django creation:

  1. Enter cmd django-admin startproject 文件名(cmd create in your directory)
  2. pycharm create -----> Recommended
  • Note: Do not mess created after the change

django directory structure

  • File name, for example day54
  • Do not change the name after the chaos created
|-day54
    |- day54
        |- settings.py:配置文件
        |- urls.py:路由映射关系
        |- wsgi.py:socket服务端文件
    |- manage.py:管理文件
    |- templates:模版文件,比如html就是模版文件
    |- status:存放一些静态文件(静态资源)
        |- css
        |- js
        |- img
  • Note: django start, something that is to start folder name at the top of pycharm

django route description

# 在urls.py文件中

# 参数一定要写request
def index(request): 
    return HttpResponse('index')

# url中填写的是一个正则表达式
urlpatterns = [
    # url(r'^admin/', admin.site.urls),
    url(r'^index/', index),
    ]

django template description

  • Taken from the database data, transmitted on this page, called rendering, rendering the template, where the template is the html page.
  • Rendering: The process server good things, rendered to the client, the web page
# 模版渲染函数
# python
def f1(request):
    ### 变量的渲染
    name = 'zekai'

    ### 列表
    li = ['zekai', 'lxxx', 'leijun']

    ### 字典
    dict = {"name":'zekai', 'age':18, 'hobby':'bj'}

    ### 列表中套字典
    myli = [
        {'id': 1, 'name': 'zekai', 'age': 12},
        {'id': 2, 'name': 'yuechun', 'age': 32},
        {'id': 3, 'name': 'lifu', 'age': 23}
    ]

    # 在settings中已经进行了路径配置,可以直接写templates中的html文件
    return render(request, 'f1.html', 
                  {"xxx":name, "li":li, 'dict':dict, 'myli':myli})
<!-- html -->
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Title</title>
</head>
<body>
{# 变量渲染 #}
    <h2>{{ xxx }}</h2>
{#列表的渲染#}
<ul>
    <li>{{ li.0 }}</li>
    <li>{{ li.1 }}</li>
    <li>{{ li.2 }}</li>
</ul>

<hr>
{#列表的循环#}
<ul>
    {% for items in li %}
        <li>{{ items }}</li>
    {% endfor %}
</ul>

{#字典的渲染#}
<h3>{{ dict.age }}</h3>

{#字典的循环#}
<ul>
    {% for items in dict.values %}
        <li>{{ items }}</li>
    {% endfor %}
</ul>

<ul>
    {% for items in dict.keys %}
        <li>{{ items }}</li>
    {% endfor %}
</ul>

<ul>
    {% for key, val in dict.items %}
        <li>{{ key }} --- {{ val }}</li>
    {% endfor %}
</ul>

{#列表中套字典格式#}
<table border="1px">
    <thead>
        <tr>
            <th>id</th>
            <th>name</th>
            <th>age</th>
        </tr>
    </thead>
    <tbody>
        {% for items in myli %}
            <tr>
                <td>{{ items.id }}</td>
                <td>{{ items.name }}</td>
                <td>{{ items.age }}</td>
                <td><a href="/f2/">删除</a></td>
            </tr>
        {% endfor %}

    </tbody>
</table>


</body>
</html>

Create a django project, you need to do several operations

# 到settings.py中, 配置:
# 1. 最后
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
    )
# 逗号不能少,只能是STATICFILES_DIRS,不能修改
# static目录需要创建
                
# 2. MIDDLEWARE中
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
#'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
    ]
                
#3. TEMPLATES中
'DIRS': [os.path.join(BASE_DIR, 'templates')]
        

To be learned

As well as learn about the FTP protocol SMTP.

What is Apache

Re going to review the url

hexo + gitee build a blog

What route, what is the interface

django the function is not serial, url parameters, the default will pass a request, parentheses may be used to control parameters. Brackets content is content parameters, a few brackets, several parameters.

Guess you like

Origin www.cnblogs.com/lucky75/p/11318150.html