Skip to content

Commit 3ff5272

Browse files
authored
Create 1项目搭建步骤
创建1项目搭建步骤
1 parent 0885d51 commit 3ff5272

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed

_posts/tool/1项目搭建步骤

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
---
2+
layout: post
3+
category: 工具
4+
---
5+
1、创建项目
6+
django-admin.py startproject mysite
7+
8+
会创建项目的文件夹
9+
10+
2、创建应用
11+
cd /mysite
12+
python manage.py startapp HelloWorld
13+
14+
3、测试
15+
python manage.py runserver 0.0.0.0:8000
16+
17+
4、编写应用HelloWorld的View函数
18+
19+
from django.shortcuts import render
20+
from django.http import HttpResponse
21+
22+
import json
23+
from models import Tips
24+
25+
def getTips(request):
26+
aTip = Tips.objects.get(id=1)
27+
res = {"data":aTip.conte}
28+
return HttpResponse(json.dumps(res),content_type="application/json")
29+
30+
def myFunc(request):
31+
return render(request,'starter.html')
32+
33+
5、编写models
34+
from __future__ import unicode_literals
35+
36+
from django.db import models
37+
38+
class Tips(models.Model):
39+
conte = models.CharField(max_length=500, blank=True, null=True)
40+
user_name = models.CharField(max_length=50, blank=True, null=True)
41+
42+
编写好之后,运行命令
43+
python manage.py makemigrations
44+
python manage.py migrate
45+
46+
可以生成相应的表。
47+
48+
6、编辑urls.py
49+
可以在总的urls通过include来配置:
50+
在项目层:
51+
from django.conf.urls import url,include
52+
from django.contrib import admin
53+
54+
urlpatterns = [
55+
url(r'^admin/', admin.site.urls),
56+
url(r'^hello/', include('HelloWorld.urls',namespace='helloworld')),
57+
]
58+
在应用层:
59+
from django.conf.urls import url
60+
from django.contrib import admin
61+
62+
from HelloWorld import views as Hview
63+
64+
urlpatterns = [
65+
url(r'^$', Hview.myFunc),
66+
url(r'gettips$', Hview.getTips),
67+
]
68+
69+
7、在setting中添加app
70+
INSTALLED_APPS = [
71+
'django.contrib.admin',
72+
'django.contrib.auth',
73+
'django.contrib.contenttypes',
74+
'django.contrib.sessions',
75+
'django.contrib.messages',
76+
'django.contrib.staticfiles',
77+
78+
'HelloWorld',
79+
]
80+
81+
8、设置static
82+
static是常用的一些静态文件。针对runserver的话:
83+
84+
STATIC_URL = '/static/'
85+
STATIC_ROOT = os.path.join(BASE_DIR,"static/")
86+
STATICFILES_DIRS = [os.path.join(BASE_DIR,"sta/")]
87+
88+
可将常用的css/js/img放到项目根目录的sta下,然后设置static_url和static_root,通过
89+
python manage.py collectstatic
90+
可将所有的静态文件收集至/static下,这样就可以在html中引用/static/这个目录了(如adminlte就可以通过这样方式放到/static目录下)
91+
92+
9、编写html
93+
在应用文件夹内新建templates
94+
然后新建.html
95+
自行编写的html或者采用adminlte框架的页面都可以放在这个地方。
96+
97+
<!DOCTYPE HTML>
98+
<html>
99+
<head>
100+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
101+
<title>HelloWorld</title>
102+
<style type="text/css">
103+
h1{
104+
font-size:20px;
105+
color:#930;
106+
text-align:center;
107+
}
108+
</style>
109+
</head>
110+
<body>
111+
<h1>this is html</h1>
112+
<p>Hello World</p>
113+
</body>
114+
</html>

0 commit comments

Comments
 (0)