简体   繁体   中英

urls.py in Django app causes 404 error on a test blog

I'm learning Python and Django and am in the process of building a test blog. Rather than following the official tutorial given in the Django book (which I found quite difficult to understand and learn), I am following the tutorial given here . This has helped me a lot to understand about the Admin Interface as well as what goes into the template section (including some sample HTML, which I don't think is included in the official Django book).

Here's a sample of my urls.py (this is inside myproject/myproject/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('',
    # Examples:
    # url(r'^$', 'myblog.views.home', name='home'),
    # url(r'^myblog/', include('myblog.foo.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    url(r'^admin/', include(admin.site.urls)),
    url(r'^', include('blog.urls')),
)

Here's a sample of my urls.py (this is inside myproject/blog/urls.py):

from django.conf.urls.defaults import *

urlpatterns = patterns('',
    (r'^$', 'blog.views.liist'),
    (r'^archive/(\d{1,2})/$', 'blog.views.liist'),
    (r'^\d{4}/d{1,2}/(.*)/$', 'blog.views.detail'),
    (r'^(\d{4})/(\d{1,2})/$', 'blog.views.month'),
    (r'^(\d{4})/$', 'blog.views.year'),
    (r'^category/$', 'blog.views.category'),
    (r'^category/(.*)/$', 'blog.views.one_category'),
    (r'^tag/$', 'blog.views.tag'),
    (r'^tag/(.*)/$', 'blog.views.one_tag'),

)
  • 127.0.0.1:8000 - works
  • 127.0.0.1:8000/admin - works
  • 127.0.0.1:8000/2013/2/fourth-post/ - 404

Is there anything that I am missing out on? Can someone point out the steps to troubleshoot, or what to check, or which part throws the error? Because I have no idea where to start troubleshooting from.

Is there any other information that I need to post here which will help my case?

  • Django version - 1.4.3
  • Python version - 2.7
  • OS - Windows XP

Thanks in advance.

Edit:

This is what the "detail" function looks like in my views.py file:

def detail(request,sl):
    try:
         post = Post.objects.filter(slug=sl)[0]
         try:
              previous_post = post.get_previous_by_published()
         except:
              previous_post = ""
         try:
              next_post = post.get_next_by_published()
         except:
              next_post = ""
    except:
         next_post = ""
         previous_post = ""
         post = ""
    return render_to_response('blog/detail.html', {'post':post,
                                            'next_post':next_post,
                                            'previous_post':previous_post,
                                           },)

Try:

(r'^(\d{4})/(\d{1,2})/([-\w]+)/$', 'blog.views.detail')

Also, definitely check out the official docs on URLs: https://docs.djangoproject.com/en/1.4/topics/http/urls/#overview

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM