class Reporter(models.Model): full_name = models.CharField(maxlength=70) def__str__(self): return self.full_name class Admin: pass class Article(models.Model): pub_date = models.DateTimeField() headline = models.CharField(maxlength=200) article = models.TextField() reporter = models.ForeignKey(Reporter) def__str__(self): return self.headline class Admin: pass
修改view.py:
from webblog.blog.models import* from django.shortcuts import render_to_response from django.template import Context, loader from django.http import HttpResponse def index(request): article_list = Article.objects.all() return render_to_response('htmls/index.html', {'article_list': article_list})
返回去修改setting.py,把新创建的app和模板加进去:
TEMPLATE_DIRS = ( # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates". # Always use forward slashes, even on Windows. # Don't forget to use absolute paths, not relative paths. "d:/webblog/", ) INSTALLED_APPS = ( 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'webblog.blog', 'django.contrib.admin', )