template 两大功能
我们在生产中会发现写的平台顶部导航栏,和左侧菜单栏都是相同的。这样我们可以把它写成模板,写的时候回只写主体部分,继承菜单栏和导航栏,来省略大量代码
举例
编辑login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>demo01</title>
</head>
<body>
<h1>{{obj.username.0}}</h1>
{% for item in obj_li %}
<div>
{% if item.username == 'zzxx' %}
<span>{{ item.username }}</span>
<span>{{ item.password }}</span>
{%endif %}
</div>
{% endfor %}
</body>
</html>
urls
from django.contrib import admin
from django.conf.urls import url,include
from app1 import views
urlpatterns = [
url('admin/', admin.site.urls),
url('login/', views.login),
views
def login(request):
if request.method == 'GET':
obj_li = models.U1serInfo.objects.all()
for obj in obj_li :
print('username :',obj.username)
print('password :',obj.password)
return render(request,'login.html',locals())
导入静态文件
template中
{% load staticfiles%} ##django中导入静态文件
<script src = "{% static 'test.js' %}"></script> ##写在title下面
在setting中增加
STATIC_URL = '/static/'
STATIC_DIRS = (
os.path.join(BASE_DIR,"static/")
)