展示HTML网页

1.导入正则模块:re_path

2.settings配置静态资源

STATICFILES_DIRS = [os.path.join(BASE_DIR,'static')]

3.settings 连接数据库

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', #选择MySQL数据库
'NAME': 'test1', #数据库名
'USER': 'root',
'PASSWORD': 'ryy123456',
'HOST': '127.0.0.1',
'PORT': '3306',
}
}

4.views重定向
  • redirect重定向关键字
    from django.shortcuts import render,redirect from django.http import HttpResponse,HttpResponseRedirect
def hello(request):
   #响应内容
   #1.跟绝对路径 2.跟相对路径 3.HTTP
   #return redirect('http://127.0.0.1/index/')
   return  HttpResponseRedirect('/index/')

5.网页添加标题

def index(request):
   ctx = {'title':'首页'}
    #重定向
   return render(request=request,template_name='index.html',context=ctx)
   type_list = models.Product.objects.values('type').distinct()   #distinct()去重
   name_list = models.Product.objects.values('name','type')

   title = '首页'   
   #导入HTML文件 render  from django.shortcuts import render
   return render(request=request,template_name='index.html',context=locals())

6. init.py导入pymysql模块

import pymysql
pymysql.install_as_MySQLdb()

7.迁移

1.生成迁移文件 2.执行迁移

python manage.py makemigrations
python manage.py migrate

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 切换到创建项目的目录 cd C:\Users\admin\Desktop\DjangoProject创建名为pr...
    在努力中阅读 8,585评论 2 3
  • 模块间联系越多,其耦合性越强,同时表明其独立性越差( 降低耦合性,可以提高其独立性)。软件设计中通常用耦合度和内聚...
    riverstation阅读 6,263评论 0 8
  • 已经同步到gitbook,想阅读的请转到gitbook: Django 1.10 中文文档 This tutori...
    leyu阅读 7,775评论 3 13
  • 22年12月更新:个人网站关停,如果仍旧对旧教程有兴趣参考 Github 的markdown内容[https://...
    tangyefei阅读 35,299评论 22 257
  • 昨晚你打电话过来,话筒里传来一片嘈杂,你告诉我你失恋了,我没有听清,然后你换了表达方式,你说你和她分手了。这一次我...
    譬如北晨阅读 3,899评论 0 3