创建项目
django-admin startproject projectname
然后再继续创建一个子项目:
python manage.py startapp blog
urls.py文件添加:
urlpatterns = [
path('admin/', admin.site.urls),
path('show_time/', views.show_time),
url('blog/', cinclude('blog.urls)), # 路由分发(注释的最后一句有提示的)
]
随后views.py里面设置:
# Create your views here.
def show_time(request):
# return HttpResponse('hello')
t = time.ctime()
return render(request, 'index.html', {'time': t})
templates新建个html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
{% load staticfiles %}
<title>Title</title>
</head>
<body>
<h1>hello {{ time }}</h1>
<h1>hello 121212121</h1>
{#<script src="/static/jquery-3.1.1.js"></script>#}
<script src="{% static 'jquery-3.1.1.js' %}"></script>
<script>
$("h1").css('color', 'red')
</script>
</body>
</html>
setting.py里面添加设置:
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
运行django:
python manage.py runserver
整理下需要知道的三条命令
1.创建项目
django-admin startproject 项目名
2.创建一个子项目:
python manage.py startapp 子项目名字
3.运行项目
python manage.py runserver