• 博客基础_django_python从入门到实践_创建简易版学习笔记网页


    效果

      建立学习笔记网页。其中包括主页和话题,话题中又有具体的条目。有条目则显示,没有则提示。

      urls.py  views.py   html

      base.html  index.html  topics.html  topic.html

    主页

    分类

     某分类下具体条目

    无条目

    urls.py  learning_log/

    1 from django.conf.urls import urls, include
    2 from django.contrib import admin #
    3 
    4 urlpatterns=[
    5     url(r'^admin/',  admin.site.urls)
    6     url(r'', include('learning_logs.urls', namespace='learning_logs')),                     
    7 ]   PPhsoa
    View Code

    urls.py  learning_logs/

    1 from django.conf.urls import url
    2 from . import views
    3 
    4 urlpatterns=[
    5     url(r'^$', views.index, name='index'),
    6     url(r'^topics/$', views.topics, name='topics'),
    7     url(r'^topics/(?P<topic.id>d+)/$', views.topic, name='topic'),       
    8 ]
    View Code

    views.py  learning_logs/

     1 from django.shortcuts import render #
     2 from .models import Topic
     3 
     4 def index(request):
     5     return render(request, 'learning_logs/index.html')
     6 
     7 def topics(request):
     8     topics = Topic.objects.order_by('date_added')
     9     context = {'topics':topics}
    10     return render(request, 'learning_logs/topics.html', context)
    11 
    12 def topic(request, topic_id):
    13     topic = Topic.objects.get(id=topic_id)
    14     entries = topic.entry_set.order_by('-date_added')
    15     context = {'topic':topic, 'entries':entries}
    16     return render(request, 'learning_logs.topic.html', context)
    View Code

    base.html   / learning_log/ learning_logs/templates/learning_logs/

    1 <p>
    2     <a href="{% url 'learning_logs:index' %}">Learning Log</a>-
    3     <a hrer="{% url 'learning_logs:topics' %}">Topics</a>
    4 </p>
    5 
    6 {% block content %}
    7 {% endblock content %}
    View Code

    index.html  / learning_log/ learning_logs/templates/learning_logs/

    1 {% extends 'learning_logs/base.html' %}
    2 
    3 {% block content %}
    4    <p>Learning log helps you learn better..</p>
    5 {% endblock content %}
    View Code

    topics.html  / learning_log/ learning_logs/templates/learning_logs/

     1 {% extends 'learning_logs/bse.html' %}
     2 
     3 {% block content %}
     4     <p>Topics</p>
     5     <ul>
     6         {% for topic in topics %}
     7             <li>
     8                 <a href="{% url 'learning_logs:topic' topic.id %}">{{ topic }}</a>
     9             </li>
    10         {% empty %}
    11              No topic has been added here.
    12         {% endfor %}
    13     </ul>
    14 {% endblock content %}    
    View Code

    topic.html  / learning_log/ learning_logs/templates/learning_logs/

     1 {% extends 'learning_logs/base.html' %}
     2 
     3 {% block content %}
     4     <p>Topic:{{ topic }}</p>
     5     <p>Entries:</p>
     6     <ul>
     7         {% for entry in entries %}
     8              <li>
     9                   <p>{{ entry.date_added|date:'M d, Y H:i' }}</p>
    10                   <p>{{ entry.text|linebreaks }}</p>
    11              </li>
    12          {% empty %}
    13               There are no entries for this topic yet.
    14          {% endfor %}
    15     </ul>
    16 {% endblock content %}        
    View Code

  • 相关阅读:
    2012暑期川西旅游之第六天(四姑娘山达维夹金山邛崃青城山)
    实现元素拖拽
    asp.net学习资源
    080402 晴れ
    送给Rain的文章
    两天后见
    若水三千
    关于下一代的教育
    20080408 大雨
    Missing You
  • 原文地址:https://www.cnblogs.com/dignity/p/9813775.html
Copyright © 2020-2023  润新知