Follow up on the previous blog:
1. Adding check-ups to check whether users have shop functions
1. Check the store's cookie when the user logs in
Verify that the user owns the store after the login check is completed
Check cookie s to prove whether a user owns a store or not
If the store exists, set cookie is_store as the store id
If the store does not exist, set the cookie to empty (this is set to empty because both 0 and false are string types when the front-end gets the cookie, and the front-end will judge to be true).
2. Re-check the front-end shop on the base page
<!-- Sidebar --> {% if request.COOKIES.is_store %} <ul class="navbar-nav bg-gradient-primary sidebar sidebar-dark accordion" id="accordionSidebar"> <!-- Sidebar - Brand --> <a class="sidebar-brand d-flex align-items-center justify-content-center" href="index.html"> <div class="sidebar-brand-icon rotate-n-15"> <i class="fas fa-laugh-wink"></i> </div> <div class="sidebar-brand-text mx-3">Background Management System</div> </a> <!-- Divider --> <hr class="sidebar-divider my-0"> <!-- Nav Item - Dashboard --> <li class="nav-item"> {# {% if is_store %}#} <a class="nav-link" href="#"> <i class="fas fa-fw fa-tachometer-alt"></i> <span>Shop management</span></a> {# {% else %}#} {# <a class="nav-link" href="/Store/store_register/">#} {# <i class="fas fa-fw fa-tachometer-alt"></i>#} {# <span>No shop, register one</span></a>#} {# {% endif %}#} </li> <!-- Divider --> <hr class="sidebar-divider"> <!-- Heading --> <div class="sidebar-heading"> Sales Management </div> <!-- Nav Item - Pages Collapse Menu --> <li class="nav-item"> <a class="nav-link collapsed" href="#" data-toggle="collapse" data-target="#collapseTwo" aria-expanded="true" aria-controls="collapseTwo"> <i class="fas fa-fw fa-cog"></i> <span>Commodity management</span> </a> <div id="collapseTwo" class="collapse" aria-labelledby="headingTwo" data-parent="#accordionSidebar"> <div class="bg-white py-2 collapse-inner rounded"> <h6 class="collapse-header">Commodity Information:</h6> <a class="collapse-item" href="/Store/add_good/">Adding goods</a> <a class="collapse-item" href="/Store/goods_list/">List of Goods</a> </div> </div> </li> <!-- Nav Item - Utilities Collapse Menu --> <!-- Divider --> {# <hr class="sidebar-divider">#} <!-- Nav Item - Charts --> {# <li class="nav-item">#} {# <a class="nav-link" href="charts.html">#} {# <i class="fas fa-fw fa-chart-area"></i>#} {# <span>Chart</span></a>#} {# </li>#} <!-- Nav Item - Tables --> {# <li class="nav-item">#} {# <a class="nav-link" href="tables.html">#} {# <i class="fas fa-fw fa-table"></i>#} {# <span>form</span></a>#} {# </li>#} <!-- Divider --> {# <hr class="sidebar-divider d-none d-md-block">#} <!-- Sidebar Toggler (Sidebar) --> <div class="text-center d-none d-md-inline"> <button class="rounded-circle border-0" id="sidebarToggle"></button> </div> </ul> <!-- End of Sidebar --> {% else %} <ul class="navbar-nav bg-gradient-primary sidebar sidebar-dark accordion" id="accordionSidebar"> <!-- Sidebar - Brand --> <a class="sidebar-brand d-flex align-items-center justify-content-center" href="index.html"> <div class="sidebar-brand-icon rotate-n-15"> <i class="fas fa-laugh-wink"></i> </div> <div class="sidebar-brand-text mx-3">Background Management System</div> </a> <!-- Divider --> <hr class="sidebar-divider my-0"> <!-- Nav Item - Dashboard --> <li class="nav-item"> <a class="nav-link" href="/Store/store_register/"> <i class="fas fa-fw fa-tachometer-alt"></i> <span>No shop, register one</span></a> </li> <!-- Divider --> <hr class="sidebar-divider"> <div class="text-center d-none d-md-inline"> <button class="rounded-circle border-0" id="sidebarToggle"></button> </div> </ul> {% endif %}
Approximate process of shop verification:
3. The back-end store registration view also sends cookies is_store
Effect demonstration:
Re-register a background user for login testing:
Registered stores:
When the registered store is successful, it will automatically jump to the home page:
At this time, the store management in the left functional area has been displayed (identifying that the user has a shop, through is_store), and the relevant shop functions can be operated.
2. Realization of Business Logic: Viewing the List of Goods is a store for current users
1. Change the store id (hidden domain) of the added product page to automatically acquire cookies. Each time a product is added, the store id in the cookie of the current page will be automatically associated.
Solving the Dependence of Shops and Commodities
<div class="form-group"> <input type="hidden" class="form-control form-control-user" name="store_id" value="{{ request.COOKIES.is_store }}"> </div>
2. Modify the back-end commodity list view and use multi-to-multi relational reverse query
Based on the current store adding commodity information, the returned commodity should be the current store's commodity.
3. Function of New Commodities Off Shelf
1. The commodity model class adds the commodity status field and synchronizes the database.
Use goods_under as the commodity status field, 1 for the shelf and 2 for the ready-for-sale status.
# v2.4 new commodity status field; 1 for sale 0 off the shelf goods_under = models.IntegerField(verbose_name="Commodity status",default=1)
2. Change the view of commodity list, and the new condition in filter only queries the goods on the shelf.
In this way, the front-end list will only show the goods on the shelves.
3. Update the Off-Shelf label of the front-end page of the commodity list
We trigger the Off-Shelf view function by clicking on the Off-Shelf button of the commodity information on the commodity list page, and send the current commodity id to the back end through the parameter id to associate the specific commodity to be off-shelf.
<a class="btn btn-danger" href="/Store/goods_under/?id={{ goods.id }}">Lower shelf</a>
4. New commodity Off-Shelf view function
Get the id parameter in the front-end get request to query which goods need to be off the shelf, set the status of the goods to 0, get the source address of the request through the HTTP_REFERER parameter in request.META, and finally return to the page of the previous request.
# v2.4 new commodity on-shelf function def under_goods(request): id = request.GET.get("id") # v2.4 returns the source address of the current request referer = request.META.get("HTTP_REFERER") if id: # v2.4 Gets the goods with the specified id (insurance implements filter, get if id does not exist is error prone) goods = Goods.objects.filter(id=id).first() # v2.4 Modification of Commodity Status goods.goods_under = 0 goods.save() return HttpResponseRedirect(referer)
IV. Functions of goods on shelves and destruction
Goods off shelves can not disappear in the air, nor can we delete goods off shelves directly from the database. We need a place where we can inquire about goods off shelves. When there is demand, goods off shelves can be put on shelves or destroyed again.
New Off-Shelf list pages and options in the background
1. Options for Adding Off-shelf Goods on base Page
<div class="bg-white py-2 collapse-inner rounded"> <h6 class="collapse-header">Commodity Information:</h6> <a class="collapse-item" href="/Store/add_good/">Adding goods</a> <a class="collapse-item" href="/Store/goods_list/up/">List of goods on sale</a> <a class="collapse-item" href="/Store/goods_list/down/">List of Off-Shelf goods</a> </div>
2. In order to make the list of goods on and off shelves not redundant in modern codes, the view of the list of goods and the front-end state judgement share a view and front-end page.
Modify the database through the status of url requests to complete the functions of shelving, shelving and destroying
In order to avoid using the data front-end to change it into character type, we use word letters to express judgment directly. When the state parameter is "up", it represents the goods on the shelf and all the goods with the commodity status of 1 are queried; otherwise, it is the goods off the shelf that are queried, and the goods with the commodity status of 0 are queried.
Sub url s pass parameters for regular group naming:
3. The under_goods view should be set_goods, and the submission of the buttons on the upper and lower shelves of the page should be judged and processed accordingly. At the same time, the destruction function is also written out.
Similarly, according to the commodity list, add a status parameter and add a state to delete. Delete the item as long as the state parameter is delete
# v2.4 new commodity on-shelf function def set_goods(request,state): # v2.5 enables the view to have both upper and lower shelves and destruction functions if state == "up": state_num = 1 else: state_num = 0 id = request.GET.get("id") # v2.4 returns the source address of the current request referer = request.META.get("HTTP_REFERER") if id: # v2.4 Gets the goods with the specified id goods = Goods.objects.filter(id=id).first() # v2.5 judges the field parameters in the front-end routing to do the corresponding operation. If delete is used here, delete the commodity. if state == "delete": goods.delete() else: # v2.4 Modification of Commodity Status goods.goods_under = state_num goods.save() return HttpResponseRedirect(referer)
Similarly, the route of the request is defined:
If the state is up to indicate that it is the goods on the shelf, the operation that can be done is the goods off the shelf. Clicking on the goods off the shelf will trigger / Store / set_goods / down/?={{ goods.id }} Routing, then entering the view function processing, the commodity status is set to 0
Otherwise, the operation that you can do is to put the goods on the shelf. Clicking on the goods on the shelf will trigger / Store/set_goods/up/?id.={{ goods.id }} Routing, then entering the view function processing, the commodity status is set to 1
Destruction function is aimed at the function of both goods on and off shelves, so both the list of goods on sale and the list of goods off shelves have the function of destroying goods.
{% ifequal state 'up' %} <a class="btn btn-danger" href="/Store/set_goods/down/?id={{ goods.id }}">Lower shelf</a> {% else %} <a class="btn btn-danger" href="/Store/set_goods/up/?id={{ goods.id }}">Upper shelf</a> {% endifequal %} <a class="btn btn-primary" href="/Store/set_goods/delete/?id={{ goods.id }}">Destruction</a>
GitHub project address: https://github.com/py304/DjangoShop