zoukankan      html  css  js  c++  java
  • Django框架创建

    python搭建虚拟环境

    Virtualenv方式

    安装virtualenv

    pip install virtualenv

    进入自己想要创建虚拟环境的目录(以E盘MyDjangoProject为例)

    cd E:/MyDjangoProject

    创建虚拟环境(以创建Envs为例)

    virtualenv Envs # 默认
    virtualenv -p /user/administrator/python35 Envs # 指定python解释器

    启动/退出 虚拟环境

    cd Envs/Scripts
    activate.bat # 启动
    deactivate.bat # 退出

    删除虚拟环境(实际上是删除目录)

    rd /s Envs

    也可以用mkvirtualenv(可能会默认创建在C盘某个地方)

    mkvirtualenv Envs # 创建
    rmvirtualenv Envs # 删除
    workon Envs # 启动虚拟环境
    deactivate # 退出虚拟环境
    pip list # 显示虚拟环境下所有的包

    安装django

    pip install django # pip安装(要联网)

    创建项目

    django-admin startproject test2 
    cd test2

    Django项目结构图

    创建应用(以booktest为例)

    python manage.py startapp booktest

    应用结构图

    创建模型

    # booktest/models.py
    from django.db import models #数据库模块
    
    # Create your models here.
    class BookInfo(models.Model): #继承Model类
        title = models.CharField(max_length=20) #字符串类型
        pub_date = models.DateField()    #日期类型
        def __str__(self):
            return self.title
    class HeroInfo(models.Model):
        name = models.CharField(max_length=10)
        content = models.CharField(max_length=100)
        gender = models.BooleanField(default=True)
        book = models.ForeignKey(BookInfo, on_delete=models.CASCADE) #外键
        def __str__(self):
            return self.name

    settings.py添加app

    生成迁移文件

    python manage.py makemigrations

    迁移在数据库中创建表

    python manage.py migrate

    附: 用shell操作(相当于用cmd运行代码)

    python manage.py shell

    数据操作命令

    # 创建
    book = BookInfo()
    book.*** = ***
    book.save()
    # 修改
    book = BookInfo.objects.get(id=***)
    book.*** = ***
    book.save()
    # 删除
    book.delete()
    # 查询
    BookInfo.objects.all()
    BOokInfo.objects.get(id=***)

    创建超级管理员

    python manage.py createsuperuser

    运行

    python manage.py runserver

     在浏览器地址栏输入 127.0.0.1:8000/admin

    改变语言和时区

    #LANGUAGE_CODE = 'en-us'
    LANGUAGE_CODE = 'zh-Hans'
    
    #TIME_ZONE = 'UTC'
    TIME_ZONE = 'Asia/Shanghai'

    admin注册模型类

    # booktest/admin.py
    class
    BookInfoAdmin(admin.ModelAdmin): list_display = ('id', 'title', 'pub_date') class HeroInfoAdmin(admin.ModelAdmin): list_display = ('id', 'name', 'content', 'gender', 'book') admin.site.register(BookInfo, BookInfoAdmin) admin.site.register(HeroInfo, HeroInfoAdmin)

    views创建视图类

    from django.shortcuts import render
    from django.http import HttpResponse
    from booktest.models import *
    # Create your views here.
    def index(request):
        # return HttpResponse('<h1>hello world</h1>')
        list = BookInfo.objects.all()
        context = {'booklist':list}
        return render(request, 'booktest/index.html', context=context)
    
    def detail(request, id):
        list = BookInfo.objects.get(id=id).heroinfo_set.all()
        context = {'herolist':list}
        return render(request, 'booktest/detail.html', context=context)

    test2/urls.py配置urlpatterns

    from django.contrib import admin
    from django.urls import path, include
    # from django.conf.urls import url, include
    urlpatterns = (
        # url(r'^admin/', include(admin.site.urls)) 旧版本
        path('admin/', admin.site.urls),   # 新版本
        # url(r'^', include('booktest.urls'))
        path('', include('booktest.urls')),
    )

    booktest/urls.py配置urlpatterns

    from booktest.views import *
    from django.urls import path, re_path
    urlpatterns = [
        # url('^index/$', index)
        path('index/', index),
        # url('^(d+)$', detail)
        re_path('(?P<id>d+)/', detail),
    ]

    配置模版目录

    templates目录下创建html文件

    index.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    {#<h3>{{ content }}</h3>#}
    <ul>
    {% for book in booklist %}
        <li><a href="/{{ book.id }}/">{{  book.title}}</a></li>
    {% endfor %}
    </ul>
    </body>
    </html>

    detail.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>英雄列表</title>
    </head>
    <body>
        <ul>
        {% for hero in herolist %}
            <li>{{ hero.name }}</li>
        {% endfor %}
        </ul>
    </body>
    </html>
  • 相关阅读:
    loj 3285 「USACO 2020 US Open Platinum」Circus
    UOJ Round #19 简要题解
    AtCoder Grand Contest 043 简要题解
    JOISC 2020 部分题目简要题解
    HNOI 2019 简要题解
    十二省联考 2019 简要题解
    九省联考 2018 简要题解
    loj 6391 「THUPC2018」淘米神的树 / Tommy
    loj 6703 小 Q 的序列
    Goodbye Jihai 部分题目简要题解
  • 原文地址:https://www.cnblogs.com/wust-ouyangli/p/8456577.html
Copyright © 2011-2022 走看看