一、Django makes it easier to build better Web apps more quickly and with less code.

  • O(objects):类和对象。
  • R(Relation):关系,关系数据库中的表格。
  • M(Mapping):映射。

django创建项目详解

Django ORM框架的功能:

a) 建立模型类和表之间的对应关系,允许我们通过面向对象的方式来操作数据库。

b) 根据设计的模型类生成数据库中的表格。

c) 通过方便的配置就可以进行数据库的切换。

二、创建Django项目

1、使用Mysql数据库

2、进入虚拟环境

workon dj

django创建项目详解

3、创建项目demo2

django-admin startproject demo2

4、进入项目目录下之后创建应用

python manage.py startapp news

成功之后目录结构

django创建项目详解

5、注册应用,demo2/settings.py文件将刚刚创建的应用添加进去。

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'news'
)

6、执行迁移,在终端中执行命令

python manage.py makemigrations  生成迁移
python manage.py migrate  执行迁移

django创建项目详解

7、登录mysql数据库(可以为项目创建一个数据库:create database demo2 charset=utf8;)

django创建项目详解

8、为django配置使用mysql数据库

修改demo2/setting中的DATEBASES


DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',  # 设置使用mysql数据库
        'NAME':'demo4',                       # 指定数据库
        'USER':'root',                         # 用户名
        'PASSWORD':'mysql',                # 数据库登陆密码
        'PORT':3306,                           #端口号
        'HOST':'localhost'#数据库所在主机的ip
    }
}

       此外还需要安装pymysql

python3:pip install pymysql
python2:pip install mysql-python

django创建项目详解

三、设计模型类NewsInfo并生成表。

   1、在news/models.py里添加。设置软删除可以防止重要数据误删

from django.db import models

# Create your models here.
class NewsInfo(models.Model):
    title = models.CharField(max_length=50,verbose_name='新闻标题')     #标题
    context = models.TextField(verbose_name='新闻内容')			   #内容
    b_date = models.DateField(verbose_name='发布日期')			   	   #日期
    read =models.IntegerField(verbose_name='阅读量')				   #阅读量
    comment = models.IntegerField(verbose_name='评论量')			   # 评论数
    is_delete = models.BooleanField(default=0,verbose_name='是否删除')  #逻辑删除

   2、注册模型类:打开应用目录下的admin.py文件

from django.contrib import admin
from .models import *
# 自定义显示项


class NewsInfoAdmin(admin.ModelAdmin):
    list_display = ['id', 'title', 'b_date']

#把模型类注册到admin站点中
admin.site.register(NewsInfo, NewsInfoAdmin)

3、登录站点管理

生成迁移:python manage.py makemigrations

执行迁移 :python manage.py migrate

创建管理员账号:python manage.py createsuperuser

启动项目:python manage.py runserver 主机ip:端口号

django创建项目详解

在浏览器中输入   127.0.0.1:8000/admin

django创建项目详解

三、视图函数并配置url

    1、打开views.py创建一个Index视图函数

from django.shortcuts import render
# Create your views here.
from .models import NewsInfo

def index(request):
    news_list = NewsInfo.objects.all()
    context={'news_list':news_list}
    return render(request,'templates/index',context)

   2、配置urls.py,在应用目录下床架一个urls.py文件,可以防止以后应用过多条理清晰。

demo2/urls.py

demo2/urls.py​

from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^',include('news.urls'))   #将应用目录下的路径添加进去
]

​

news/urls.py

news/urls.py
#!/usr/bin/env/python
# -*-coding:utf-8 -*-

from django.conf.urls import include, url
from django.contrib import admin
from .views import *

urlpatterns = [
    url(r'^$',index),
    url(r'^index',index)
]

在demo2/setting.py中,添加路径

django创建项目详解

django创建项目详解


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
  <h1>欢迎</h1>
</body>
</html>

四、验收成果,在浏览器中输入127.0.0.1:8000/index.(运行项目python manage.py runserver)

django创建项目详解

 

 

相关文章:

  • 2021-11-23
  • 2021-11-23
  • 2021-09-29
  • 2021-07-08
  • 2021-08-27
猜你喜欢
  • 2021-12-03
  • 2022-01-23
  • 2021-09-01
  • 2021-10-30
  • 2021-06-14
相关资源
相似解决方案