开发者

Issue with URL pattern in Django with webmonkey tutorial

开发者 https://www.devze.com 2023-02-05 00:03 出处:网络
I have been working my way through the Django webmonkey tutorial and am currently stuck in lesson 4 available here http://www.webmonkey.com/2010/02/Use_Templates_in_Django/.

I have been working my way through the Django webmonkey tutorial and am currently stuck in lesson 4 available here http://www.webmonkey.com/2010/02/Use_Templates_in_Django/.

My problem is with the blog detail template, whenever I click the link on my blog/list.html page to view the detail of the entry I get a Page not found (404).

This is exactly the error I see:

Page not found (404)
Request Method:     GET
Request URL:http://127.0.0.1:8000/2010/dec/17/welcome-my-blog/

Using the URLconf defined in djangoblog.urls, Django tried these URL patterns, in this order: 

^admin/(.*) 
^blog/ 
^tags/(?P<slug>[a-zA-Z0-9_.-]+)/$ 
The current URL, 2010/dec/17/welcome-my-blog/, didn't match any of these.

These are my url.py files as well as my models.py, I didn't post my admin, tag views or settings but I can if it would help.

djangoblog\urls.py

# This also imports the include function
from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
(r'^admin/(.*)', admin.site.root),
(r'^blog/', include('djangoblog.blog.urls')),
(r'^tags/(?P<slug>[a-zA-Z0-9_.-]+)/$', 'djangoblog.tag_views.tag_detail'),
)

djangoblog\blog\urls.py

from django.conf.urls.defaults import *
from djangoblog.blog.models import Entry
from tagging.views import tagged_object_list

info_dict = {
 'queryset': Entry.objects.filter(status=1),
 'date_field': 'pub_date',
}

urlpatterns = patterns('django.views.generic.date_based',
(r'(?P<year>d{4})/(?P<month>[a-z]{3})/(?P<day>w{1,2})/(?P<slug>[-w]+)/$',    
 'object_detail', dict(info_dict, slug_field='slug',template_name='blog/detail.html')),
(r'^(?P<year>d{4})/(?P<month>[a-z]{3})/(?P<day>w{1,2})/(?P<slug>[-w]+)/$',  
 'object_detail', dict(info_dict, template_name='blog/list.html')),   
(r'^(?P<year>d{4})/(?P<month>[a-z]{3})/(?P<day>w{1,2})/$',
 'archive_day',dict(info_dict,template_name='blog/list.html')),
(r'^(?P<year>d{4})/(?P<month>[a-z]{3})/$','archive_month', dict(info_dict, template_name='blog/list.html')),
(r'^(?P<year>d{4})/$','archive_year', dict(info_dict, template_name='blog/list.html')),
(r'^$','archive_index', dict(info_dict, template_name='blog/list.html')),
)

djangoblog\blog\models.py.

from django.db import models
from django.contrib.syndication.feeds import Feed
from django.contrib.sitemaps import Sitemap
import markdown
from tagging.fields import TagField
from tagging.models import Tag

class Entry(models.Model):
    title = models.CharField(max_length=200)
    slug = models.SlugField(
        unique_for_date='pub_date',
        help_text='Automatically built from the title.'
    )

    body_html = models.TextField(blank=True)
    body_markdown = models.TextField()
    pub_date = models.DateTimeField('Date published')
    tags = TagField()
    enable_comments = models.BooleanField(default=True)

    PUB_STATUS = (
        (0, 'Draft'),
        (1, 'Published'),
    )

    status = models.IntegerField(choices=PUB_STATUS, default=0)


    class Meta:
        ordering = ('-pub_date',)
        get_latest_by = 'pub_date'
        verbose_name_plural = 'entries'

    def __unicode__(self):
        return u'%s' %(self.title)

    def get_absolute_url(self):
        return "/%s/%s/" %(self.pub_date.strftime("%Y/%b/%d").lower(), self.slug)

    def save(self):
         self.body_html = markdown.markdown(self.body_markdown, safe_mode = False)
         super(Entry, self).save()

    def get_previous_published(self):
        return self.get_previous_by_pub_date(status__exact=1)

    def get_next_published(self):
         return self.get_next_by_pub_date(status__exact=1)

    def get_tags(self):
         return Tag.objects.get_for_object(self)

If there are any other files that would help I can provide them. My file structure is laid out below:

File Structure

C:\Workspaces\python\djangoblog

urls.py
tag_views.py
settings.py
manage.py
djangoblog.db
admin.py
__init__.py

templates

 blo开发者_Go百科g
  detail.html
  list.html

tags
    detail.html (empty)
    list.html (empty)

base.html

tagging

markdown

blog

 __init__.py
 admin.py
 models.py
 tests.py
 urls.py
 views.py

Edit: (from comment on Josh's answer)

404 at: blog/dec/17/welcome-my-blog/:

^admin/(.*) 
^blog/ (?P<year>d{4})/(?P<month>[a-z]{3})/(?P<day>w{1,2})/(? P<slug>[-w]+)/$ 
^blog/ ^(?P<year>d{4})/(?P<month>[a-z]{3})/(?P<day>w{1,2})/(? P<slug>[-w]+)/$ 
^blog/ ^(?P<year>d{4})/(?P<month>[a-z]{3})/(?P<day>w{1,2})/$ 
^blog/ ^(?P<year>d{4})/(?P<month>[a-z]{3})/$ 
^blog/ ^(?P<year>d{4})/$ 
^blog/ 
^$ ^tags/(?P<slug>[a-zA-Z0-9_.-]+)/$ 
The current URL, blog/2010/dec/17/welcome-my-blog/, didn't match any of these.


The problem is that none of your URL patterns match the URL in the browser.

http://127.0.0.1:8000/2010/dec/17/welcome-my-blog/

Is the URL that you're trying to reach. I assume that is meant to be a blog entry, reachable at the URL blog/.

Your root URL patterns are the following:

urlpatterns = patterns('',
    (r'^admin/(.*)', admin.site.root),    
    (r'^blog/', include('djangoblog.blog.urls')),
    (r'^tags/(?P[a-zA-Z0-9_.-]+)/$', 'djangoblog.tag_views.tag_detail'),
)

Which means that none of them match your URL in the browser, since it does not start with either admin or blog or tags.

The following URL may fix your problem:

http://127.0.0.1:8000/blog/2010/dec/17/welcome-my-blog/

Notice the /blog/ after the IP:Port. This will match the blog/ entry in your root URL conf. Then it is up to your djangoblog.blog.urls file to handle the rest of the pattern, which it looks like it does.

(r'(?Pd{4})/(?P[a-z]{3})/(?Pw{1,2})/(?P[-w]+)/$', 'object_detail', dict(info_dict, slug_field='slug',template_name='blog/detail.html'))

The above pattern looks for:
- 4 numbers (2010)
- then a /
- then 3 letters (dec)
- then a /
- then a word with 1 or 2 characters (17)
- then a /
- then a word (welcome-my-blog)

You should be fine by adding the /blog/ after the IP:port.

Your Update:

^blog/ (?P<year>d{4})/(?P<month>[a-z]{3})/(?P<day>w{1,2})/(? P<slug>[-w]+)/$ 

The above URL is incorrect. You need to prepend it with a ^ as the other URLS.


For those that miss it as I did the major change is to change this - P<year>d{4} to this P<year>\d{4}. That is add the backslash. You may also wish to take a look at the /$ at the end of the lines. These will force your urls to end in a "/".


Figured it out, Josh was right about the URL pattern missing the /blog. I updated my list template like so:

<p><a href="/blog{{object.get_absolute_url}}">read more</a></p>

That resolved the /blog error, after that I needed to modify my djangoblog\blog\urls.py like so:

djangoblog\blog\urls.py

urlpatterns = patterns('django.views.generic.date_based',

(r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/(?P<day>\d{1,2})/(?P<slug>[-\d\w]+)/$', 'object_detail', dict(info_dict, slug_field='slug',template_name='blog/detail.html')),

(r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/(?P<day>\d{1,2})/(?P<slug>[-\d\w]+)/$', 'object_detail', dict(info_dict, template_name='blog/list.html')),

(r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/(?P<day>\d{1,2})/$','archive_day',dict(info_dict,template_name='blog/list.html')),

(r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/$','archive_month', dict(info_dict, template_name='blog/list.html')),

(r'^(?P<year>\d{4})/$','archive_year', dict(info_dict, template_name='blog/list.html')),

(r'^$','archive_index', dict(info_dict, template_name='blog/list.html')),

)

Once I made those two changes all of my URL's were fixed, I can't take credit for the urls.py fix as I found it on this site:

http://ubuntuforums.org/showthread.php?p=9650959

Hope this helps others in the future with this tutorial.

0

精彩评论

暂无评论...
验证码 换一张
取 消