代码拉取完成,页面将自动刷新
1. Create a django project:
Terminal: django-admin startproject pyshop .
# 'pyshop' is the name of the project
2. run web server
Terminal: python manage.py runserver
3. create an app named 'products'
Terminal + : python manage.py startapp products
4. view function
Open views.py, define the index(main page of products app) function, simply return the http response via class called ‘HttpResponse’
5. mapping the urls to the view function (so far, django don't know to call the view function when there is a '/product' request from the browser)
create a new python file called 'urls' under products file, mapping the url to the view function via path function
6. add the products app
open urls.py under pyshop file, it's the parent urls module.
7 create a model
Open models.py under products file
8 注册
open setting.py under pyshop file, add info into INSTALLED_APPS, info came from apps.py under products file.
9 makemigrations
Terminal: python manage.py makemigrations
Terminal: python manage.py migrate
10 admin panel
Terminal: python manage.py createsuperuser
open admin.py under products, 'admin.site.register(Product)'
11 显示产品给用户
open views.py 'from .models import Product'
add a new directory called 'templates' inside products file
add a new file called 'index.html' inside templates file
open views.py change the index function
open index.html
12 use base html, copy from 'https://getbootstrap.com/'
add a new file called 'base.html' inside the templates file.
do change under <body>
back to index.html '{% extends 'base.html'%}'
13 card 点替换图片
go https://getbootstrap.com/docs/4.4/components/card/, copy
back to index.html, do changes
14 加引导栏,美化
https://getbootstrap.com/docs/4.4/components/navbar/ copy
back to base.html do change under <body>
为了能让base.html复用
build a templates directory under PyShop file, 把base.html从 之前的文件夹,拖到新的templates里面,
open settings, TEMPLATES 'DIRS': [
os.path.join(BASE_DIR, 'templates')
]
美化:
open base.html
<body>
<nav class="navbar navbar-light bg-light">
<a class="navbar-brand" href="#">PyShop</a>
</nav>
<div class="container">
{% block content %}
{% endblock %}
</div>
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。