【问题标题】:Django page & category names showing as objects显示为对象的 Django 页面和类别名称
【发布时间】:2014-12-25 07:12:58
【问题描述】:

此代码将页面和类别名称显示为对象,而不是它们各自的标题。它应该显示名称及其显示页面对象和类别对象,而不是所有标题

import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')

import django
django.setup()

from rango.models import Category, Page


def populate():
    python_cat = add_cat('Python')

    add_page(cat=python_cat,
        title="Official Python Tutorial",
        url="http://docs.python.org/2/tutorial/")

    add_page(cat=python_cat,
        title="How to Think like a Computer Scientist",
        url="http://www.greenteapress.com/thinkpython/")

    add_page(cat=python_cat,
        title="Learn Python in 10 Minutes",
        url="http://www.korokithakis.net/tutorials/python/")

    django_cat = add_cat("Django")

    add_page(cat=django_cat,
        title="Official Django Tutorial",
        url="https://docs.djangoproject.com/en/1.5/intro/tutorial01/")

    add_page(cat=django_cat,
        title="Django Rocks",
        url="http://www.djangorocks.com/")

    add_page(cat=django_cat,
        title="How to Tango with Django",
        url="http://www.tangowithdjango.com/")

    frame_cat = add_cat("Other Frameworks")

    add_page(cat=frame_cat,
        title="Bottle",
        url="http://bottlepy.org/docs/dev/")

    add_page(cat=frame_cat,
        title="Flask",
        url="http://flask.pocoo.org")

    # Print out what we have added to the user.
    for c in Category.objects.all():
        for p in Page.objects.filter(category=c):
            print ("- {0} - {1}".format(str(c), str(p)))

def add_page(cat, title, url, views=0):
    p = Page.objects.get_or_create(category=cat, title=title, url=url, views=views)[0]
    return p

def add_cat(name):
    c = Category.objects.get_or_create(name=name)[0]
    return c

# Start execution here!
if __name__ == '__main__':
    print ("Starting Rango population script...")
    populate()

代码有什么问题还是另一个文件有问题?使用 python 3.4 和 django 1.7。我错过了一个文件吗?还有其他文件我应该分享吗?

【问题讨论】:

    标签: python-3.4 django-1.7


    【解决方案1】:

    __unicode__(self) 似乎正在为 Python-3 制造问题。将其替换为 __str__(self) 即可。

    from django.db import models
    
    class Category(models.Model):
        name = models.CharField(max_length=128, unique=True)
        def __str__(self):
            return self.name
    
    class Page(models.Model):
        category =  models.ForeignKey(Category)
        title = models.CharField(max_length = 128)
        url = models.URLField()
        views = models.IntegerField(default=0)
        def __str__(self):
            return self.title
    

    【讨论】:

      【解决方案2】:

      我在使用 tango 学习 django 时遇到了同样的问题。我错过了Page类中rango/modules.py中的方法。

      def str(self): # 对于 Python 2,也使用 unicode 返回self.title

      【讨论】:

        猜你喜欢
        • 2018-07-21
        • 1970-01-01
        • 2020-03-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多