1

我得到:使用 blog.urls 中定义的 URLconf,Django 按以下顺序尝试了这些 URL 模式:^ ^$ ^ ^/(?P[a-zA-Z0-9]+) [name='view_blog_post']当前 URL duzeltme-yazisi/ 与其中任何一个都不匹配。

这个错误。这里有一些输出:

urls.py(在项目文件夹中):

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

urlpatterns = patterns('',
    url(r'^', include('userside.urls')),

)

urls.py(在应用程序的文件夹中):

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

urlpatterns = patterns('userside.views',
    url(r'^$','index'),
    url(r'^/(?P<postslug>[^\.]+)','userside.views.singlePost',name='view_blog_post'),
)

视图.py:

from userside.models import Post
from django.shortcuts import render_to_response
from django.template import RequestContext


def index(request):
    post_list = Post.objects.all()
    return render_to_response('userside/index.html',
                              {'post_list':post_list},
                  context_instance = RequestContext(request))

def singlePost(request,postslug):
    post = Post.objects.get(slug=postslug)
    context = {'post':post}
    return render_to_response('userside/detail.html',context,context_instance = RequestContext(request))

模型.py:

from django.db import models
#from django.utils import timezone
from django.db.models import permalink

class Post(models.Model):
    title = models.CharField(max_length = 100)
#    date  = models.DateTimeField(auto_now_add=True)
    text  = models.TextField()
    slug = models.SlugField(unique=True)

    def __unicode__(self):
    return self.title

    @permalink
    def get_absolute_url(self):
    return ('view_blog_post',None, {'postslug':self.slug})

这是我的 index.html 模板文件:

<html>
<head>
        <title>Welcome</title>
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}css/index.css" />
</head>
<body>
<h1>Hello Mars</h1>
<br>
{% if post_list %}
    {% for x in post_list %}
        <p><a href="/{{ x.slug }}/">{{ x.title }}</a></p>
        <p>{{ x.text }}</p>
        <hr>
    {% endfor %}
{% else %}
    <div class="center">No records! ( but looks like code works correctly!)</div>
{% endif %}
</body>
</html>

Django 版本:1.4

这里有什么问题?:/

谢谢你

4

2 回答 2

1

项目 - urls.py

url(r'^$', include('userside.urls')),

用户端 - urls.py

url(r'^(?P<postslug>[-\w]+)/$',
    # ../gis-grundlagen/
    view = 'singlePost',
    name = 'userside-single-post',
),

用户端-views.py

def singlePost(request, postslug):
    post = get_object_or_404(Post, slug=postslug)
    context = {'post':post}
    return render_to_response('userside/detail.html',context,context_instance = RequestContext(request))
于 2012-08-05T12:12:02.830 回答
0

$使用include时不应该有,尝试:

url(r'^', include('userside.urls')),

通常你会显示一个子文件夹,例如

url(r'^userside/', include('userside.urls')),

最初的斜线 ( /) 可能也是一个错误:

url(r'^/(?P<postslug>[^\.]+).html','userside.views.singlePost',name='view_blog_post'),

应该是

url(r'^(?P<postslug>[^\.]+).html','userside.views.singlePost',name='view_blog_post'),
于 2012-08-04T21:38:57.370 回答