点击进去
1.8 跟1.11差别不大
https://yiyibooks.cn/xx/django_182/index.html
内建展示视图
https://yiyibooks.cn/xx/django_182/topics/class-based-views/generic-display.html
https://yiyibooks.cn/xx/django_182/topics/class-based-views/mixins.html
django官方文档
点击进去
https://docs.djangoproject.com/en/2.2/
下面展示数据
C:\Users\huang\PycharmProjects\MxShop\apps\goods\views_base.py
from django.views.generic.base import View
# from django.views.generic import ListView
from goods.models import Goods
class GoodsListView(View):
def get(self, request):
"""
通过django的view实现商品列表页
:param request:
:return:
"""
json_list = []
goods = Goods.objects.all()[:10]
for good in goods:
json_dict = {}
json_dict["name"] = good.name
json_dict["category"] = good.category.name
json_dict["market_price"] = good.market_price
json_list.append(json_dict)
from django.http import HttpResponse
import json
return HttpResponse(json.dumps(json_list), content_type="application/json")
C:\Users\huang\PycharmProjects\MxShop\MxShop\urls.py
"""MxShop URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.2/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
# from django.contrib import admin
# from django.urls import path
from django.conf.urls import url
import xadmin
from MxShop.settings import MEDIA_ROOT
from django.views.static import serve
from goods.views_base import GoodsListView
urlpatterns = [
# path('admin/', admin.site.urls),
url(r'^xadmin/', xadmin.site.urls),
url(r'^media/(?P<path>.*)$', serve, {"document_root": MEDIA_ROOT}),
# 商品列表页
url(r'goods/$', GoodsListView.as_view(), name="goods-list")
]
启动项目,访问
用2345 浏览器 安装 jsonview之后浏览
======================
如果把 时间字段 显示出来会报错