supersheep1
supersheep1

Reputation: 721

Django picture error and {% url admin:index %}

I'm making a simple blog using this tutorial http://lightbird.net/dbe/blog.html I have a question and a problem I would like to address.:]

The problem is my blog doesn't look like the one on light bird example and the second problem is this syntax {% url admin:index %} at my bbase.html I didn't register the namespace as admin but as namespace = 'blog' in my main URL ,I don't understand how does this syntax work {% url admin:index %}

enter image description here enter image description here

My main URLconf are :

 from django.conf.urls import patterns, include, url
 from django.contrib import admin
 from django.conf import settings
 admin.autodiscover()
 urlpatterns = patterns('',                  
     url(r'^admin/', include(admin.site.urls)),
     url(r'^cool/', include('blog.urls', namespace='blog')),

 )

My Blog URLconf are :

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

urlpatterns = patterns('blog.views',
        url(r'^$','main', name='main'),

)

My views.py

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

def main(request):
    """Main listing."""
    posts = Post.objects.all().order_by("-created")
    paginator = Paginator(posts, 2)

    try: page = int(request.GET.get("page", '1'))
    except ValueError: 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))

My bbase.html which lives in C:\djcode\mysite\blog\templates

 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 <head> <title>{% block title %}MyBlog{% endblock %}</title> </head>

 <body>
     <div id="sidebar"> {% block sidebar %} {% endblock %} </div>
     <div id="container">
         <div id="menu">
             {% block nav-global %}

                 <!-- MENU -->
                 <h3>MyBlog</h3>
                 {% if user.is_staff %}
                 <a href="{% url admin:index %}">Admin</a>
                 <a href="{% url admin:blog_post_add %}">Add post</a>
                 {% endif %}

             {% endblock %}
         </div>

         <div id="content">
             {% block content %}{% endblock %}
         </div>
     </div>

 </body>
 </html>

My list.html

{% extends "bbase.html" %}

{% block content %}
    <div class="main">

         <!-- Posts  -->
         <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>

         <!-- Next/Prev page links  -->
         {% if posts.object_list and posts.paginator.num_pages > 1 %}
    <div class="pagination" style="margin-top: 20px; margin-left: -20px; ">
        <span class="step-links">
            {% if posts.has_previous %}
                <a href= "?page={{ posts.previous_page_number }}">newer entries &lt;&lt; </a>
            {% endif %}

            <span class="current">
                &nbsp;Page {{ posts.number }} of {{ posts.paginator.num_pages }}
            </span>

            {% if posts.has_next %}
                <a href="?page={{ posts.next_page_number }}"> &gt;&gt; older entries</a>
            {% endif %}
              </span>
          </div>
          {% endif %}

      </div>

{% endblock %}

Upvotes: 0

Views: 812

Answers (1)

Alasdair
Alasdair

Reputation: 308769

The url tag {% url admin:index %} links to the index view of the admin app. The admin namespace is correct because you included your admin urls with the pattern:

url(r'^admin/', include(admin.site.urls)),

Note you have not set a namespace for the admin app, so it defaults to admin.

If you wanted to link to an index view in your blog app, the you would use {% url blog:index %}.

If you are new to Django, I would personally recommend ignoring the namespace functionality when including url patterns. It can be useful if you want to distinguish between two views with the same name in different apps, or two different instances of the same app, but here, it just seems to be causing you unnecessary confusion. On the other hand, I would always recommend naming your url patterns, which solves many many url pattern problems.

Upvotes: 3

Related Questions