我正在开发一个博客项目。现在当我加载127.0.0.1;我看到我的主页面列出了所有创建的帖子。当我点击其中一个帖子时,它会重定向一个页面。该页面的网址如下:127.0.0.1/first-post。
我想做的就是,
127.0.0.1/post/first-post
我该怎么做?
这是我的urls.py文件:
from django.conf.urls import patterns, include, url
from userside.models import Post
urlpatterns = patterns('userside.views',
url(r'^$','index'),
url(r'^(?P<postslug>[-\w]+)',view ='singlePost', name='view_blog_post'),
)
这是我的models.py:
from django.db import models
from django.db.models import permalink
from autoslug import AutoSlugField
class Post(models.Model):
title = models.CharField(max_length = 100)
text = models.TextField()
slug = AutoSlugField(populate_from='title',unique=True)
posted = models.DateField(auto_now_add=True)
def __unicode__(self):
return self.title
@permalink
def get_absolute_url(self):
return ('view_blog_post',None, {'postslug':self.slug})
这是我的主要urls.py:
from django.conf.urls import patterns, include, url
urlpatterns = patterns('',
url(r'^', include('userside.urls')),
)
这是我的views.py:
from userside.models import Post
from django.shortcuts import render_to_response,get_object_or_404
from django.template import RequestContext
def index(request):
post_list = Post.objects.all().order_by("-posted")
return render_to_response('userside/index.html',
{'post_list':post_list},
context_instance = RequestContext(request))
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))
答案 0 :(得分:2)
url(r'^post/(?P<postslug>[-\w]+)',view ='singlePost', name='view_blog_post'),