django学习1 - 入门

通过pip安装django
(base) C:\Windows\system32>pip install django
Collecting django
Downloading https://files.pythonhosted.org/packages/c7/87/fbd666c4f87591ae25b7bb374298e8629816e87193c4099d3608ef11fab9/Django-2.1.7-py3-none-any.whl (7.3MB)
100% |████████████████████████████████| 7.3MB 2.9MB/s
Requirement already satisfied: pytz in c:\users\xxx\anaconda3\lib\site-packages (from django) (2018.7)
Installing collected packages: django
Successfully installed django-2.1.7

显示django安装信息
(base) C:\Windows\system32>pip show django
Name: Django
Version: 2.1.7
Summary: A high-level Python Web framework that encourages rapid development and clean, pragmatic design.
Home-page: https://www.djangoproject.com/
Author: Django Software Foundation
Author-email: foundation@djangoproject.com
License: BSD
Location: c:\users\xxx\anaconda3\lib\site-packages
Requires: pytz
Required-by:

django管理软件
(base) C:\Windows\system32>django-admin

Type 'django-admin help ’ 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 environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.).

创建django项目,startproject 命令
(base) C:\python\django>django-admin startproject demo

C:.
└─demo
| └─demo
| └─__init__.py 一个空文件,用它表示一个目录为Python的标准包
| └─settings.py Django项目的配置文件,包括Django模块应用配置、数据库配置、模板配置等。
| └─urls.py Django项目的URL声明。
| └─wsgi.py 与WSGI兼容的Web服务器为你的项目提供服务的入口点。
└────manage.py 一个命令行工具,让你在使用Django项目时以不同的方式进行交互。

查看manage.py提供的命令
(base) C:\python\django>cd demo
(base) C:\python\django\demo>python manage.py

Type 'manage.py help ’ for help on a specific subcommand.

Available subcommands:

[auth]
changepassword
createsuperuser

[contenttypes]
remove_stale_contenttypes

[django]
check
compilemessages
createcachetable
dbshell
diffsettings
dumpdata
flush
inspectdb
loaddata
makemessages
makemigrations
migrate
sendtestemail
shell
showmigrations
sqlflush
sqlmigrate
sqlsequencereset
squashmigrations
startapp
startproject
test
testserver

[sessions]
clearsessions

[staticfiles]
collectstatic
findstatic
runserver

创建hello应用
(base) C:\python\django\demo>python manage.py startapp hello
在这里插入图片描述
执行完毕后,自动创建hello应用目录和文件
admin.py 映射models中的数据到Django自带的admin后台
apps.py 用于应用程序的配置
models.py 模型文件,创业应用程序数据表模型,对数据库的相关操作
tests.py 创建测试用例
views.py 视图文件,控制箱前端页面显示的内容

运行应用
(base) C:\python\django\demo>python manage.py runserver
Performing system checks…

System check identified no issues (0 silenced).

You have 15 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run ‘python manage.py migrate’ to apply them.
March 14, 2019 - 11:07:10
Django version 2.1.7, using settings ‘demo.settings’
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.

web服务起在本机的8000端口,可以通过浏览器访问该URL,也可以通过Ctrl+c键退出服务。

Hello Django
在demo\settings.py配置文件中,将hello应用加入到项目。

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'hello',
]

在demo\hello\ulrs.py,配置index路径

from hello import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('index/',views.index),
]

在demo\hello\views.py,定制index函数,通过HttpResponse类向浏览器返回字符串"Hello Django!"。

from django.shortcuts import render
from django.shortcuts import HttpResponse

# Create your views here.

def index(request):
    return HttpResponse("Hello Django!")

浏览器访问http://127.0.0.1:8000/index/,显示"Hello Django!"。
在这里插入图片描述

使用模板

在demo/hello目录下创建templates目录(Django默认使用templates的模板文件),和index.html文件。

<html>
    <head>
        <title>Django Page</title>
    </head>
    <body>
        <h1>Hello Django!!!</h1>
    </body>
</html>

修改demo\hello\views.py文件的index函数,使用render函数,将index.html返回给浏览器。

from django.shortcuts import render
from django.shortcuts import HttpResponse

# Create your views here.
def index(request):
    #return HttpResponse("Hello Django!")
    return render(request,"index.html")

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值