zoukankan      html  css  js  c++  java
  • Django 项目中添加静态文件夹

    在 mysite 文件夹下添加一个 statics 文件夹用来存放 js 文件

    在 index.html 文件中添加

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <form action="/userInfo" method="post">
        <p>名字<input type="text" name="username"></p>
        <p>性别<input type="text" name="sex"></p>
        <p>邮箱<input type="text" name="email"></p>
        <p><input type="submit" value="submit"></p>
    </form>
    <hr>
    <h1>数据展示</h1>
    <table border="1px">
        <tr>
            <td>名字</td>
            <td>性别</td>
            <td>邮箱</td>
        </tr>
    
        {% for i in user_list %}
        <tr>
            <td>{{ i.username }}</td>
            <td>{{ i.sex }}</td>
            <td>{{ i.email }}</td>
        </tr>
        {% endfor %}
    </table>
    </body>
    <!-- 新添加 -->
    {% load static %}
    <script src="{% static "jquery-3.3.1.min.js" %}" ></script>
    <script>
        $("h1").css("color","red")
    </script>
    <!-- 新添加 -->
    </html>
    

    在 urls.py 文件中添加

    from django.contrib import admin
    from django.urls import path
    from blog import views
    from django.conf import settings
    from django.conf.urls.static import static
    
    urlpatterns = [
        path('admin/', admin.site.urls),
        path('userInfo', views.userInfo),
    ] + static(settings.STATIC_URL, document_root=settings.STATICFILES_DIRS)
    

    在 settings.py 中添加

    # 在 STATIC_URL = '/static/' 下面添加,STATIC_URL 相当于一个别名,给前端使用,映射 statics 文件夹(该文件夹名字可更改);前端调用就使用 /static/jquery-3.3.1.min.js。与文件夹名字无关。
    
    STATICFILES_DIRS=[
        os.path.join(BASE_DIR, "statics"),
    ]
    

  • 相关阅读:
    CHAR和HEX互相转换
    Delphi之TComponent类
    Delphi 延迟函数 比sleep 要好的多
    Delphi中三种延时方法及其定时精度分析
    Cport 应用集合
    重命名数据库时提示无法用排他锁锁定数据库
    Bugzilla在XP下安装
    Web service 超过了最大请求长度
    调用webservice时提示对操作的回复消息正文进行反序列化时出错
    c# IL 指令解析
  • 原文地址:https://www.cnblogs.com/klvchen/p/10616399.html
Copyright © 2011-2022 走看看