1. 搭建整个框架
django-admin.py startproject lovesong
cd lovesong
python manage.py startapp userManagement
2. setting 文件设置 添加app 及静态文件路径
INSTALLED_APPS = [
‘django.contrib.admin‘,
‘django.contrib.auth‘,
‘django.contrib.contenttypes‘,
‘django.contrib.sessions‘,
‘django.contrib.messages‘,
‘django.contrib.staticfiles‘,
‘userManagement‘,
]
STATIC_URL = ‘/static/‘
SITE_ROOT=os.path.join(os.path.abspath(os.path.dirname(__file__)),‘..‘)
STATIC_ROOT = os.path.join(SITE_ROOT,‘static‘)
STATICFILES_DIRS = (
("css", os.path.join(STATIC_ROOT,‘css‘)),
("js", os.path.join(STATIC_ROOT,‘js‘)),
("images", os.path.join(STATIC_ROOT,‘images‘)),
)
3. 根目录下建立静态文件夹static,内含文件夹js, css, images.
4. 在views.py上写上访问网页的代码
from django.shortcuts import render
# Create your views here.
def index(request):
return render(request, ‘index.html‘,{‘param‘:‘HelloWorld!‘})
5. 配置url
from django.conf.urls import url
from django.contrib import admin
from userManagement import views as userManagement_views
urlpatterns = [
url(r‘^admin/‘, admin.site.urls),
url(r‘^$‘, userManagement_views.index),
]
6. 在app下建一个文件夹templates, 在这个文件夹下新建一个index.html
<!DOCTYPE html>
<html>
<head>
<title>Hello World</title>
{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static ‘css/main.css‘ %}"/>
<script type="text/javascript" src="{% static ‘js/jquery.js‘ %}"></script>
</head>
<body>
<h1>{{param}}</h1>
</body>
</html>
7. 执行以下命令
python manage.py collectstatic
python manage.py migrate
python manage.py runserver