一、主页输出简单数据
核心代码;
index.html
<div class="container">
{% block page-container %}
`articles`
{{ request.path }}
{% endblock %}
</div> <!-- /container -->
views.py
def index(request):
articles = models.Article.objects.all()
return render(request,'index.html',{'articles':articles})
二、跳转页面输出数据
不同板块之间跳转,根据category的id来设计url,实现不同url之间的跳转
index.html
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', views.index, name="index" ),
url(r'^category/(\d+)/$', views.category, name="category"),
]
对category 表构建出id
admin.py
class CategoryAdmin(admin.ModelAdmin):
list_display = ('id','name')
class ArticleAdmin(admin.ModelAdmin):
list_display = ('id','title','auther','hidden','publish_date')
admin.site.register(models.Article,ArticleAdmin)
admin.site.register(models.Category,CategoryAdmin)
admin.site.register(models.Comment)
admin.site.register(models.ThumbUp)
admin.site.register(models.UserProfile)
admin.site.register(models.UserGroup)
index.html 设计跳转链接
<ul class="nav navbar-nav">
<li><a href="{% url 'index' %}">综合讨论区</a></li>
<li><a href="{% url 'category' 1 %}">DOTA1</a></li>
<li><a href="{% url 'category' 2 %}">DOTA2</a></li>
<li><a href="{% url 'category' 3 %}">LOL</a></li>
<li><a href="{% url 'category' 4 %}">星际争霸</a></li>
</ul>