larken

勤奋的人生才有价值

导航

Django入门与实践-第11章:URL 分发(完结)

# myproject/urls.py
from django.conf.urls import url
from django.contrib import admin
from boards import views

urlpatterns = [
    url(r'^$', views.home, name='home'),
    url(r'^boards/(?P<pk>\d+)/$', views.board_topics, name='board_topics'),
    url(r'^admin/', admin.site.urls),
]
# boards/views.py
from django.shortcuts import render
from .models import Board

def home(request):
    boards = Board.objects.all()
    return render(request, 'home.html', {'boards': boards})

def board_topics(request, pk):
    board = Board.objects.get(pk=pk)
    return render(request, 'topics.html', {'board': board})
    
        
# boards/views.py
from django.shortcuts import render
from django.http import Http404
from .models import Board

def home(request):
    boards = Board.objects.all()
    return render(request, 'home.html', {'boards': boards})

def board_topics(request, pk):
    try:
        board = Board.objects.get(pk=pk)
    except Board.DoesNotExist:
        raise Http404
    return render(request, 'topics.html', {'board': board})
    

# boards/views.py
from django.shortcuts import render, get_object_or_404
from .models import Board

def home(request):
    boards = Board.objects.all()
    return render(request, 'home.html', {'boards': boards})

def board_topics(request, pk):
    board = get_object_or_404(Board, pk=pk)
    return render(request, 'topics.html', {'board': board})
<!--templates/topics.html-->
{% load static %}<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>{{ board.name }}</title>
        <link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}">
    </head>
    <body>
        <div class="container">
            <ol class="breadcrumb my-4">
                <li class="breadcrumb-item">Boards</li>
                <li class="breadcrumb-item active">{{ board.name }}</li>
            </ol>
        </div>
    </body>
</html>


<!--templates/topics.html-->    
{% load static %}<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>{{ board.name }}</title>
        <link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}">
    </head>
    <body>
        <div class="container">
            <ol class="breadcrumb my-4">
                <li class="breadcrumb-item"><a href="{% url 'home' %}">Boards</a></li>
                <li class="breadcrumb-item active">{{ board.name }}</li>
            </ol>
        </div>
    </body>
</html>
<!--templates/home.html {% raw %}-->   
{% load static %}<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Boards</title>
        <link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}">
    </head>
    <body>
        <div class="container">
            <ol class="breadcrumb my-4">
                <li class="breadcrumb-item active">Boards</li>
            </ol>
            <table class="table">
                <thead class="thead-inverse">
                    <tr>
                        <th>Board</th>
                        <th>Posts</th>
                        <th>Topics</th>
                        <th>Last Post</th>
                    </tr>
                </thead>
                <tbody>
                    {% for board in boards %}
                      <tr>
                          <td>
                              <a href="{% url 'board_topics' board.pk %}">{{ board.name }}</a>
                  <small class="text-muted d-block">{{ board.description }}</small>
                          </td>
                          <td class="align-middle">0</td>
                          <td class="align-middle">0</td>
                          <td></td>
                      </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </body>
</html>

 

posted on 2018-09-05 09:50  larken  阅读(176)  评论(0编辑  收藏  举报