Django笔记

总结一下上课用到的命令和一些笔记

  • django-admin startproject acapp #创建项目

  • ag “xxx” #快速查找xxx内容

  • python3 manage.py runserver 0.0.0.0:8000 #django启动项目

  • .gitigonre */xxxxx #不需要git的内容

  • python3 manage.py startapp Name #创建app

  • app中结构 |– game | |– init.py | |– admin.py #存储管理员页面相关 | |– apps.py | |– migrations #系统生成 | | – init.py | |– models.py #定义网站中的数据库和表 | |– tests.py | `– views.py #写函数的

  • python3 manage.py migrate #警告信息中是python manage.py migrate 应加上3, 意为同步数据库的修改 (执行之后可以打开管理员页面

  • python3 manage.py createsuperuser #创建管理员用户

  • touch urls.py & mkdir templates #models(存放数据结构),views(存储视图/函数)以及这两个是日后主要的操作对象,templates存放的是网页模板,urls是路由

  • vim game/views.py

    1
    2
    3
    4
    5
    6
    7
    
     from django.http import HttpResponse
    
     def index(resquest):
     def index(resquest):
         line1 = '<h1 style="text-align: center">xxx</h1>'
         line2 = '<img src = "#图片地址">'
         return HttpResponse(line1+line2)                   
    
  • vim game/urls.py

    1
    2
    3
    4
    5
    6
    
     from django.urls import path
     from game.views import index
    
     urlpatterns = [
          path("", index, name="index"),                                                                                                                                     
     ]
    
  • vim/acapp/acapp/urls.py

    1
    2
    
    from django.urls import path, include #修改第十一行
    path('', include('game.urls')), #在第20行之后插入
    
Licensed under CC BY-NC-SA 4.0
comments powered by Disqus