如何解决当我使用样板时,Django 模型不渲染
当我尝试渲染时没有错误。当我不使用 django 仪表板样板时,它工作正常。我测试了在 shell 上设置的查询及其工作。数据可以在admin上看到。它只是没有在 html 模板上呈现。不确定网址设置是否会影响它。
我已尝试更改如何在模板上呈现,但仍然无法正常工作。有人知道如何纠正它吗?
网址
from django.urls import path,include,re_path
from . import views
urlpatterns = [
# The home page
path('',views.index,name='home'),# Matches any html file
re_path(r'^.*\.*',views.pages,name='pages'),]
Views
from django.contrib.auth.decorators import login_required
from django.shortcuts import render,get_object_or_404,redirect
from django.template import loader
from django.http import HttpResponse
from django import template
from stockmgmt import views
from .models import *
from .forms import *
@login_required(login_url="/login/")
def index(request):
context = {}
context['segment'] = 'index'
html_template = loader.get_template('index.html')
return HttpResponse(html_template.render(context,request))
def list_tables(request):
title = 'List of Items'
# form = StockSearchForm(request.POST or None)
queryset = Stock.objects.all()
context = {
"title": title,"queryset": queryset,"form": form,}
return render(request,"list_tables.html",context)
list_tables.html
{% for instance in queryset %}
<tr>
<td><input type="checkBox" class="checkthis" /></td>
<td>{{forloop.counter}}</td>
<td>{{instance.category}}</td>
<td>{{instance.item_name}}</td>
<td>{{instance.use}}</td>
<td>{{instance.size}}</td>
<td>{{instance.color}}</td>
<td>{{instance.packaging}}</td>
<!-- ====================================================================================== -->
<!-- # 28. 4. Make a template tag in the list_items.html condition with a div rapping the quantity field that will apply a style to the field when the reorder level is equal to or less than the quantity -->
<!-- <td><a href="{% url 'stock_detail' instance.id %}">{{instance.quantity}}</a></td> -->
<td>
{% if instance.quantity <= instance.reorder_level %}
<div style="background-color: orange;">
<a href="{% url 'stock_detail' instance.id %}">{{instance.quantity}}</div>
{% else %}
<a href="{% url 'stock_detail' instance.id %}">{{instance.quantity}}</div>
{% endif %}
</td>
<!-- Step 28.5 - Add a column for reorder level and make is clickable to update the reorder level -->
<td><a href="{% url 'reorder_level' instance.id %}">{{instance.reorder_level}}</a></td>
<!-- ====================================================================================== -->
<td>{{instance.receive_by}}</td>
<td>{{instance.issue_by}}</td>
<td>{{instance.issue_to}}</td>
<td>{{instance.create_by}}</td>
<td>{{instance.e_date}}</td>
<td>{{instance.timestamp}}</td>
<td>{{instance.last_updated}}</td>
<td><a href="{% url 'update_items' instance.id %}" class="btn btn-success">Update</a>
<td><a href="{% url 'delete_items' instance.id %}" class="btn btn-danger">Delete</a>
</td>
</tr>
{% endfor %}
解决方法
问题出在您的 urls.py 中,您没有在 url 中定义 list_tabels
path('List_tabels/',view.list_tables,name='list_tabel')
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。