Django项目入门

Django项目入门

搭建好python+django环境之后,就可以创建一个django项目啦~~~

创建Django项目步骤

  1. 新建项目

django-admin startproject firstproject

项目建好之后我们可以用pycharm打开看一下项目目录结构:
在这里插入图片描述
2. 修改项目内容,新增功能❤
(这一步是重点,涉及到修改视图、路由、配置等,根据实际功能来,这里我们先不操作)
3. 项目目录下调试运行

python manage.py runserver
October 16, 2019 - 11:27:42
Django version 2.2.6, using settings ‘firstproject.settings’
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
[16/Oct/2019 11:27:58] “GET / HTTP/1.1” 200 16348

运行成功后,在浏览器输入 127.0.0.1:8000,正常启动界面如下图(因为我们步骤2什么功能都没有做,所以看到的是这样的画面)
在这里插入图片描述
此外说明一下步骤中出现的djang-admin、manage.py命令

django-admin简介

django-admin是django框架的全局管理工具,
django-admin用法:

django-admin  <command> [options]
django-admin help
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 environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.).

如步骤1中用到的startproject-创建一个django项目工程,我们可以看一下它的用法

django-admin help startproject
usage: django-admin startproject [-h] [--template TEMPLATE]
                                 [--extension EXTENSIONS] [--name FILES]
                                 [--version] [-v {0,1,2,3}]
                                 [--settings SETTINGS]
                                 [--pythonpath PYTHONPATH] [--traceback]
                                 [--no-color] [--force-color]
                                 name [directory]

Creates a Django project directory structure for the given project name in the
current directory or optionally in the given directory.

positional arguments:
  name                  Name of the application or project.
  directory             Optional destination directory

optional arguments:
  -h, --help            show this help message and exit
  --template TEMPLATE   The path or URL to load the template from.
  --extension EXTENSIONS, -e EXTENSIONS
                        The file extension(s) to render (default: "py").
                        Separate multiple extensions with commas, or use -e
                        multiple times.
  --name FILES, -n FILES
                        The file name(s) to render. Separate multiple file
                        names with commas, or use -n multiple times.
  --version             show program's version number and exit
  -v {0,1,2,3}, --verbosity {0,1,2,3}
                        Verbosity level; 0=minimal output, 1=normal output,
                        2=verbose output, 3=very verbose output
  --settings SETTINGS   The Python path to a settings module, e.g.
                        "myproject.settings.main". If this isn't provided, the
                        DJANGO_SETTINGS_MODULE environment variable will be
                        used.
  --pythonpath PYTHONPATH
                        A directory to add to the Python path, e.g.
                        "/home/djangoprojects/myproject".
  --traceback           Raise on CommandError exceptions
  --no-color            Don't colorize the command output.
  --force-color         Force colorization of the command output.

manage.py简介

manage.py 是一个实用的命令行工具,可让你以各种方式与 Django 项目进行交互
manage.py用法:

python manage.py <command> [options]
python manage.py help

Type 'manage.py help <subcommand>' 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

在项目中创建一个具体应用:python manage.py startapp appName

  • 项目与应用的区别:项目对应于一个网站,是配置和应用的集合;而应用对应于特定功能,是具体功能的载体

实现最小的项目——helloworld

1.创建app
在项目目录下执行:

python manage.py startapp helloworld
创建成功后,看看项目目录发生了什么变化?
在这里插入图片描述
从上图可以看到,新增了一个叫helloworld的目录,此目录为app目录,与项目同级。其中views.py包含对某个HTTP请求(URL)的相应。

2.修改views.py视图文件

from django.http import HttpResponse
def hello(request):
    return HttpResponse("Hello World!!!")

3.修改urls.py路由配置
urls.py用于指定URL与响应函数之间的路径关系,即绑定视图与URL

from django.contrib import admin
from django.urls import path
from helloworld import views    

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

4.重新运行一下server
在浏览器中输入http://127.0.0.1:8000/hello/,结果如下图
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值