larken

勤奋的人生才有价值

导航

Django入门与实践-第18章:访问已登录用户(完结)

#boards/views.py 

@login_required
def new_topic(request, pk):
    board = get_object_or_404(Board, pk=pk)
    if request.method == 'POST':
        form = NewTopicForm(request.POST)
        if form.is_valid():
            topic = form.save(commit=False)
            topic.board = board
            topic.starter = request.user # <- here
            topic.save()
            Post.objects.create(
                message=form.cleaned_data.get('message'),
                topic=topic,
                created_by=request.user # <- and here
            )
            return redirect('board_topics', pk=board.pk) # TODO: redirect to the created topic page
    else:
        form = NewTopicForm()
    return render(request, 'new_topic.html', {'board': board,'form': form})
#myproject/urls.py
url(r'^boards/(?P<pk>\d+)/topics/(?P<topic_pk>\d+)/$', views.topic_posts, name='topic_posts'),

#boards/views.py
def topic_posts(request, pk, topic_pk):
    topic = get_object_or_404(Topic, board__pk=pk, pk=topic_pk)
    return render(request, 'topic_posts.html', {'topic': topic})
    
<!--templates/topic_posts.html-->
{% extends 'base.html' %}
{% block title %}{{ topic.subject }}{% endblock %}
{% block breadcrumb %}
    <li class="breadcrumb-item"><a href="{% url 'home' %}">Boards</a></li>
    <li class="breadcrumb-item"><a href="{% url 'board_topics' topic.board.pk %}">{{ topic.board.name }}</a></li>
    <li class="breadcrumb-item active">{{ topic.subject }}</li>
{% endblock %}
{% block content %}
{% endblock %}
<!--templates/topic_posts.html-->

{% extends 'base.html' %}
{% load static %}
{% block title %}{{ topic.subject }}{% endblock %}
{% block breadcrumb %}
    <li class="breadcrumb-item"><a href="{% url 'home' %}">Boards</a></li>
    <li class="breadcrumb-item"><a href="{% url 'board_topics' topic.board.pk %}">{{ topic.board.name }}</a></li>
    <li class="breadcrumb-item active">{{ topic.subject }}</li>
{% endblock %}
{% block content %}
    <div class="mb-4">
        <a href="#" class="btn btn-primary" role="button">Reply</a>
    </div>
    {% for post in topic.posts.all %}
    <div class="card mb-2">
        <div class="card-body p-3">
            <div class="row">
                <div class="col-2">
                    <img src="{% static 'img/avatar.svg' %}" alt="{{post.created_by.username }}" class="w-100">
                    <small>Posts: {{ post.created_by.posts.count }}</small>
                </div>
                <div class="col-10">
                    <div class="row mb-3">
                        <div class="col-6">
                            <strong class="text-muted">{{ post.created_by.username }}</strong>
                        </div>
                        <div class="col-6 text-right">
                            <small class="text-muted">{{ post.created_at}}</small>
                        </div>
                    </div>
                    {{ post.message }}
                    {% if post.created_by == user %}
                    <div class="mt-3">
                    <a href="#" class="btn btn-primary btn-sm" role="button">Edit</a>
                    </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
    {% endfor %}
{% endblock %}
<!--templates/topics.html-->
 
{% for topic in board.topics.all %}
    <tr>
        <td><a href="{% url 'topic_posts' board.pk topic.pk %}">{{ topic.subject }}</a></td>
        <td>{{ topic.starter.username }}</td>
        <td>0</td>
        <td>0</td>
        <td>{{ topic.last_updated }}</td>
    </tr>
{% endfor %}

 

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