我得到: 使用blog.urls中定义的URLconf,Django按以下顺序尝试了这些URL模式: ^ ^ $ ^ ^ /(?P [a-zA-Z0-9] +)[name ='view_blog_post'] 目前的网址duzeltme-yazisi /与其中任何一个都不匹配。
这个错误。 这里有一些输出:
urls.py(在项目文件夹中):
from django.conf.urls import patterns, include, url
urlpatterns = patterns('',
url(r'^', include('userside.urls')),
)
urls.py(在app的文件夹中):
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'),
)
views.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))
models.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
这是错的吗? :/谢谢
答案 0 :(得分:1)
项目 - urls.py
url(r'^$', include('userside.urls')),
userside - urls.py
url(r'^(?P<postslug>[-\w]+)/$',
# ../gis-grundlagen/
view = 'singlePost',
name = 'userside-single-post',
),
userside - 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))
答案 1 :(得分: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'),