Django之环境搭建

时间:2023-03-09 13:11:46
Django之环境搭建

安装django

pip install django

安装完django之后就有了可用的管理工具django-admin.py,我们可以用它来创建我们的项目。

django-admin的语法:

django-admin

Type 'django-admin help <subcommand>' for help on a specific subcommand.

Available subcommands:

[django]
check
compilemessages
createcachetable
dbshell
diffsettings
dumpdata
flush
inspectdb
loaddata
makemessages
makemigrations
migrate
runserver
sendtestemail
shell
showmigrations
sqlflush
sqlmigrate
sqlsequencereset
squashmigrations
startapp
startproject
test
testserver
Note that only Django core commands are listed as settings are not properly configured (error: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the env
ironment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.).

创建我们的项目

创建工程

django-admin startproject djangoStudy

目录结构

│  manage.py

└─djangoStudy
settings.py
urls.py
wsgi.py
__init__.py

初始化系统表

>python manage.py makemigrations
No changes detected >python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying sessions.0001_initial... OK

启动项目

>python manage.py runserver 127.0.0.1:8001
Performing system checks... System check identified no issues (0 silenced).
March 20, 2017 - 11:00:23
Django version 1.10.4, using settings 'djangoStudy.settings'
Starting development server at http://127.0.0.1:8001/
Quit the server with CTRL-BREAK.

页面访问项目

输入http://127.0.0.1:8001

Django之环境搭建

项目已经运行成功了。。。。

但是提示你还没有创建一个app并且设置urls,那么我们按照它的提示来操作吧。

创建我们的app

>python manage.py startapp sqlOper

设置settings

DEBUG = False
ALLOWED_HOSTS = ['127.0.0.1']

设置views

from django.http import HttpResponse

def hello(request):
return HttpResponse("hello Word ...")

设置urls

urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^index', views.hello),
]

再次刷新一次

Django之环境搭建