Kevin
Kevin

Reputation: 4351

Link a view to a link Django

I am making a simple blog app in Django as a learning exercise. I am able to add posts, see them all on the front page, so it is working so far. Where I am having an issue is creating a view that shows the whole post on a separate page. I want to click the title, and go to a page at the url /post/primary key that has the title and body. When I click on the link, I get a page with just the base.html. Not sure where I am missing anything, here are urls.py, views.py, and post.html:

urls.py from django.conf.urls import patterns, include, url

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('blog.views',
    url(r'^$', 'frontpage'),
    url(r'^post/(\d+)/$', 'post'),
)

urlpatterns += patterns('',
    url(r'^admin/', include(admin.site.urls)),
) 

from django.core.paginator import Paginator, InvalidPage, EmptyPage
from django.core.urlresolvers import reverse
from django.shortcuts import get_object_or_404, render_to_response
from blog.models import *

views.py

def frontpage(request):
    posts = Post.objects.all().order_by("-created")
    paginator = Paginator(posts, 5)

    page = request.GET.get('page', '1')

    try: 
        posts = paginator.page(page)
    except (InvalidPage, EmptyPage):
        posts = paginator.page(paginator.num_pages)

    return render_to_response("list.html", dict(posts=posts, user=request.user))

def post(request, pk):
    """Single Post"""
    post = Post.objects.get(pk = pk)
    d = dict(post=post, user=request.user)
    return render_to_response("post.html", d)

post.html

{% extends "base.html" %}

{% block content %}
    <div class ="main">
        <ul>
            {% for post in posts.object_list %}
                <div class = "title">{{ post.title }}</div>
                <ul>
                    <div class="time"> {{ post.created }}</div>
                    <div class ="body"> {{ post.body|linebreaks }}</div>
                </ul>
            {% endfor %}
        </ul>
    </div>
{% endblock %}

Thanks in advance for your help.

Upvotes: 1

Views: 1518

Answers (2)

fuka1983
fuka1983

Reputation: 31

You have to change urls.py to go to a page at the url /post/primary key.

urlpatterns = patterns('blog.views',
    url(r'^$', 'frontpage'),
    url(r'^post/(?P<pk>\d+)/$', 'post'),
)

Upvotes: 0

dm03514
dm03514

Reputation: 55962

I am assuming the page.html is actually the post.html you have in yoru codes sample???You no longer have a collection of posts but instead just have 1 post

This needs to change from: (which is looping through your posts)

{% extends "base.html" %}

{% block content %}
    <div class ="main">
        <ul>
            {% for post in posts.object_list %}
                <div class = "title">{{ post.title }}</div>
                <ul>
                    <div class="time"> {{ post.created }}</div>
                    <div class ="body"> {{ post.body|linebreaks }}</div>
                </ul>
            {% endfor %}
        </ul>
    </div>
{% endblock %}

To something like (which just displays your single post):

{% extends "base.html" %}

{% block content %}
    <div class ="main">
        {{ post.title }}
        {{ post.created }}
        {{ post.body }}      
    </div>
{% endblock %}

Upvotes: 1

Related Questions