在 Django 2.0 上使用 Django 1.9 的 Tango - "list indices must be integers or slices, not str"
Tango with Django 1.9 on Django 2.0 - "list indices must be integers or slices, not str"
我可能很愚蠢,试图在新发布的 Django 2.0 上使用 Django 1.9 通过 Tango。我快要 chapter 6 了,但收到以下错误。
错误与我的 views.py 文件(下方)有关,其中包含
context_dict['pages'] = pages
context_dict['category'] = category
貌似是罪魁祸首。 Django 2.0 中变量分配给字典的方式是否发生了变化,或者这是一个 Python 问题?可以提供来自所有其他应用程序文件的代码。
Views.py
from django.shortcuts import render
from rango.models import Category, Page
def index(request):
category_list = Category.objects.order_by('-likes')[:5]
context_dict = {'categories':category_list}
return render(request, 'rango/index.html', context=context_dict)
def about(request):
return render(request, 'rango/about.html')
def show_category(request, category_name_slug):
context_dict = []
try:
category = Category.objects.get(slug=category_name_slug)
pages = Page.objects.filter(category=category)
context_dict['pages'] = pages
context_dict['category'] = category
except Category.DoesNotExist:
context_dict['category'] = None
context_dict['pages'] = None
return render(request, 'rango/category.html', context_dict)
Models.py
from django.db import models
from django.contrib import admin
from django.template.defaultfilters import slugify
# Create your models here.
class Category(models.Model):
name = models.CharField(max_length=128, unique=True)
views = models.IntegerField(default=0)
likes = models.IntegerField(default=0)
slug = models.SlugField(unique=True)
def save(self, *args, **kwargs):
self.slug = slugify(self.name)
super(Category,self).save(*args, **kwargs)
class Meta:
verbose_name_plural = 'Categories'
def __str__(self):
return self.name
class Page(models.Model):
category = models.ForeignKey(Category, on_delete=models.PROTECT)
title = models.CharField(max_length=128)
url = models.URLField()
views = models.IntegerField(default=0)
def __str__(self): # For Python 2, use __unicode__ too
return self.title
class PageAdmin(admin.ModelAdmin):
list_display = ('title','category','url')
页面(来自管理页面)
您的问题在这里:
def show_category(request, category_name_slug):
context_dict = []
其中 context_dict
应该是字典,而不是列表:
def show_category(request, category_name_slug):
context_dict = {}
#also valid: context_dict = dict()
我可能很愚蠢,试图在新发布的 Django 2.0 上使用 Django 1.9 通过 Tango。我快要 chapter 6 了,但收到以下错误。
错误与我的 views.py 文件(下方)有关,其中包含
context_dict['pages'] = pages
context_dict['category'] = category
貌似是罪魁祸首。 Django 2.0 中变量分配给字典的方式是否发生了变化,或者这是一个 Python 问题?可以提供来自所有其他应用程序文件的代码。
Views.py
from django.shortcuts import render
from rango.models import Category, Page
def index(request):
category_list = Category.objects.order_by('-likes')[:5]
context_dict = {'categories':category_list}
return render(request, 'rango/index.html', context=context_dict)
def about(request):
return render(request, 'rango/about.html')
def show_category(request, category_name_slug):
context_dict = []
try:
category = Category.objects.get(slug=category_name_slug)
pages = Page.objects.filter(category=category)
context_dict['pages'] = pages
context_dict['category'] = category
except Category.DoesNotExist:
context_dict['category'] = None
context_dict['pages'] = None
return render(request, 'rango/category.html', context_dict)
Models.py
from django.db import models
from django.contrib import admin
from django.template.defaultfilters import slugify
# Create your models here.
class Category(models.Model):
name = models.CharField(max_length=128, unique=True)
views = models.IntegerField(default=0)
likes = models.IntegerField(default=0)
slug = models.SlugField(unique=True)
def save(self, *args, **kwargs):
self.slug = slugify(self.name)
super(Category,self).save(*args, **kwargs)
class Meta:
verbose_name_plural = 'Categories'
def __str__(self):
return self.name
class Page(models.Model):
category = models.ForeignKey(Category, on_delete=models.PROTECT)
title = models.CharField(max_length=128)
url = models.URLField()
views = models.IntegerField(default=0)
def __str__(self): # For Python 2, use __unicode__ too
return self.title
class PageAdmin(admin.ModelAdmin):
list_display = ('title','category','url')
页面(来自管理页面)
您的问题在这里:
def show_category(request, category_name_slug):
context_dict = []
其中 context_dict
应该是字典,而不是列表:
def show_category(request, category_name_slug):
context_dict = {}
#also valid: context_dict = dict()