[英]My catalog app shows a blank page, what should I do?
I'm creating a website in django, and currently I'm building a online catalog of items.我正在 django 中创建一个网站,目前我正在构建一个项目的在线目录。 But when I run it, it just shows a blank page.但是当我运行它时,它只显示一个空白页面。
my code is given below:我的代码如下:
catalog\\models.py目录\\models.py
from django.db import models
from django.urls import reverse
# Create your models here.
class Category(models.Model):
name = models.CharField(max_length=40, db_index=True)
slug = models.SlugField(max_length=200, db_index=True, unique=True, null=True)
class Meta:
ordering = ('name',)
verbose_name = 'category'
verbose_name_plural = 'categories'
def __str__(self):
return self.name
class itemType(models.Model):
name=models.CharField(max_length=40)
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('catalog:item_list_by_category',
args=[self.slug])
class itemStage(models.Model):
name=models.CharField(max_length=10)
def __str__(self):
return self.name
class item(models.Model):
name=models.CharField(max_length=20, help_text='Name of the item', db_index=True)
slug = models.SlugField(max_length=200, db_index=True, null=True)
description = models.CharField(max_length=200, help_text='Description', blank=True, db_index=True)
category = models.ForeignKey(Category, on_delete=models.SET_NULL, null=True)
version=models.CharField(max_length=20, help_text='Version of the item')
pub_date=models.DateTimeField('date', help_text='The date of publishing', auto_now=True)
item_type = models.ForeignKey(itemType, on_delete=models.SET_NULL, null=True)
stage = models.ForeignKey(itemStage, on_delete=models.SET_NULL, null=True)
image = models.ImageField(upload_to = 'static/static_dirs/images', blank=True)
def __str__(self):
return self.name
class Meta:
ordering = ('name',)
index_together = (('id', 'slug'))
def get_absolute_url(self):
return reverse('catalog:item_detail',
args=[self.id, self.slug])
catalog\\views.py目录\\views.py
from django.shortcuts import render, get_object_or_404
from .models import Category, item
def itemListView(request, category_slug=None):
category = None
categories = Category.objects.all()
items = item.objects.all()
if category_slug:
category = get_object_or_404(Category, slug=category_slug)
items = items.filter(category=category)
return render(request, 'catalog/item/list.html', {'category': category,
'categories': categories,
'items': items})
def itemDetailView(request, id, slug):
item = get_object_or_404(item, id=id, slug=slug, available=True)
context = {
'item' : item
}
return render(request, 'catalog\catalog.html', context=context)
list.html列表.html
<!--{% extends "catalog/catalog.html" %}-->
{% load static %}
{% block title %}
{% if category %}{{ category.name }}{% else %}items{% endif %}
{% endblock %}
{% block content %}
<div id="sidebar">
<h3>Categories</h3>
<ul>
<li {% if not category %}class="selected"{% endif %}>
<a href="{% url "catalog:item_list" %}">All</a>
</li>
{% for c in categories %}
<li {% if category.slug == c.slug %}class="selected"{% endif %}>
<a href="{{ c.get_absolute_url }}">{{ c.name }}</a>
</li>
{% endfor %}
</ul>
</div>
<div id="main" class="item-list">
<h1>{% if category %}{{ category.name }}{% else %}items{% endif %}</h1>
{% for item in items %}
<div class="item">
<a href="{{ item.get_absolute_url }}">
<img src="{% if item.image %}{{ item.image.url }}{% else %}{% static "img/no_image.png" %}{% endif %}">
</a>
<a href="{{ item.get_absolute_url }}">{{ item.name }}</a><br>
${{ item.price }}
</div>
{% endfor %}
</div>
{% endblock %}
I thought that it was because of catalog.html, which includes the navbar and title, so i commented it out.我以为是catalog.html的原因,里面有导航栏和标题,所以我注释掉了。 But now it shows nothing但现在它什么也没显示
you are not sending 'items' in context of itemListView function.您不是在 itemListView 函数的上下文中发送“项目”。 you should use code something like this:你应该使用这样的代码:
def itemListView(request, category_slug=None):
items = item.objects.all()
num_items = item.objects.all().count
num_types = itemType.objects.all().count
if category_slug:
category = get_object_or_404(Category, slug=category_slug)
items = item.objects.filter(category=category)
context = {
'num_items' : num_items,
'num_types' : num_types,
'items': items
}
return render(request, 'catalog\item\list.html', context=context)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.