Kidex94
Kidex94

Reputation: 5

Have a Page for each Specific Entry - Django

I am using Django 1.5.1 and right now I have a webpage showing multiple entries from my database but now I want to have a page for each entry by it self. So basically when a user clicks on the title, it brings them to a separate page only showing the entry they clicked.

I am getting the following error:

NameError at /
name 'Post' is not defined

Below is my code for each page that I think is relevant to solving this problem. I also have a post.html page created so that can't be my error, views.py and urls.py are stored in the default directory of Django and my post.html and index.html are stored in /mysite/mysite/templates/blog/

index.html

 {% extends 'base.html' %}
{% block title %} Roadcase WebApp {% endblock %}
{% block content %}
    <h1>Roadcase WebApp</h1>
    <p>Today's Date: {% now "F j, Y" %}</p>
    <div class="post">
        <table class="main">
            <tr class="title_head">
                <td><a href="">ID</a></td>
                <td><a href="">Roadcase</a></td>
                <td><a href="">User</a></td>
                <td><a href="">Location</a></td>
                <td><a href="">Date Taken</a></td>
                <td><a href="">Return Date</a></td>
                <td><a href="">Returned</a></td>
            </tr>
    {% for post in posts %}
            <tr>
                <td><a class="n_link" href="/blog/{{post.id}} ">{{post.id}}</a></td>
                <td>{{post.Roadcase_Number}}</td>
                <td>{{post.First_Name}} {{post.Last_Name}}</td>
                <td>{{post.Where_Did_It_Go}}</td>
                <td>{{post.Date_Taken|date}}</td>
                <td>{{post.Date_Returned|date}}</td>
                <td>{% if post.Returned == True %}
                        Yes
                    {% else %}
                        No
                    {% endif %}</td>
            </tr>
    {% endfor %}
    </table>
    </div>
{% endblock %}

views.py

    from django.shortcuts import render, get_object_or_404
from blog.models import Post

#def index(request):
    # get the blog posts that are published
#   posts = Post.objects.filter(published=True)

#   # Order the content by descending id, (3 2 1)
#   posts = Post.objects.order_by('-id')
    # now return the rendered template
#   return render(request,'blog/index.html', {'posts': posts})

#def post(request, slug):
    # get the Post object
#   post = get_object_or_404(Post, slug=slug)
    # now return the rendered template
#   return render(request, 'blog/post.html', {'post': post})

class Index(ListView):
    model = Post
    template_name = 'blog/index.html'
    def get_queryset(self):
        return super(Index, self).get_queryset().filter(published=True).order_by('-id')

def PostDetailView(DetailView):
    model = Post
    template_name = 'blog/post.html'

urls.py

    from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.views.generic import DetailView

admin.autodiscover()

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    url(r'^$','blog.views.index'),
    url(r'^(?P<slug>[\w\-]+)/$','blog.views.post'),

    url(r'^(?P<pk>\d+)$', DetailView.as_view(
                            model=Post,
                            template_name="post.html")),
)

post.html

{% extends 'base.html' %}


{% block title %} Roadcase WebApp {% endblock %}
{% block content %}
    <h1>Roadcase WebApp</h1>
    <p>Today's Date: {% now "F j, Y" %}</p>
    <div class="post">
        <table class="main">
            <tr class="title_head">
                <td><a href="">ID</a></td>
                <td><a href="">Roadcase</a></td>
                <td><a href="">User</a></td>
                <td><a href="">Location</a></td>
                <td><a href="">Date Taken</a></td>
                <td><a href="">Return Date</a></td>
                <td><a href="">Returned</a></td>
            </tr>
            <tr>
                <td><a class="n_link" href="/blog/{{post.id}} ">{{post.id}}</a></td>
                <td>{{post.Roadcase_Number}}</td>
                <td>{{post.First_Name}} {{post.Last_Name}}</td>
                <td>{{post.Where_Did_It_Go}}</td>
                <td>{{post.Date_Taken|date}}</td>
                <td>{{post.Date_Returned|date}}</td>
                <td>{% if post.Returned == True %}
                        Yes
                    {% else %}
                        No
                    {% endif %}</td>
            </tr>
    </table>
    </div>
{% endblock %}

Upvotes: 0

Views: 146

Answers (2)

Leandro
Leandro

Reputation: 2247

I would do that:

in your views.py:

from django.shortcuts import render, get_object_or_404
from django.views.generic import DetailView, ListView
from blog.models import Post

class Index(ListView):
    model = Post
    template_name = 'blog/index.html'
    def get_queryset(self)
        return super(Index, self).get_queryset().filter(published=True).order_by('-id')

def PostDetailView(DetailView):
    model = Post
    template_name = 'blog/post.html'

in your PROJECT urls.py :

from django.conf.urls import patterns, include, url
from django.contrib import admin

admin.autodiscover()

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

)

in your blog app urls.py:

from blog.views import PostDetailView, Index

urlpatterns = patterns('',
    url(r'^$',Index.as_view()),
    url(r'^post/(?P<slug>[\w-]+)/$',PostDetailView.as_view()),

    url(r'^post/(?P<pk>\d+)$', PostDetailView.as_view()),
)

EDIT Added imports

Upvotes: 0

nnmware
nnmware

Reputation: 950

You have error in urls.py Need add import Post, cause DetailView using this as model

from blog.models import Post

Upvotes: 0

Related Questions