2013-01-16 57 views
0

我不明白你怎麼會塞後場產生的網址,進入停止另一個頁面的Django以前保存塞字段的值,或應該去的views.py值塞URL和頁後

我在models.py

from django.db import models 
from django.db.models import permalink 

class Blog(models.Model): 
    title =  models.CharField(max_length=100, unique=True) 
    slug =  models.SlugField(max_length=100, unique=True) 
    body =  models.TextField() 
    posted = models.DateField(db_index=True, auto_now_add=True) 
    category = models.ForeignKey('blog.Category') 

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

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

views.py

def index(request): 
     return render_to_response('index.html', {'categories':Category.objects.all(), 'posts': Blog.objects.all()[:5]}) 

def view_post(request, slug): 
     return render_to_response('view_post.html', {'post':get_object_or_404(Blog, slug=slug)}) 

def view_details(request, slug): 
      return render_to_response('view_details.html', {'post':get_object_or_404(Blog, slug=slug)}) 

urls.py

from django.conf.urls import patterns, include, url 
from django.contrib import admin 
admin.autodiscover() 

urlpatterns = patterns('', 

    url(r'^$', 'blog.views.index', name='index'), 
    url(r'^blog/view/(?P<slug>[^\.]+)', 'blog.views.view_post', name='view_blog_post'), 
    url(r'^blog/view/(?P<slug>[^\.]+)'/details, 'blog.views.view_details', name='view_blog_details'), 
) 

的index.html

{% extends 'base.html' %} 
{% block titulo %} My blog {% endblock %} 

{% block content %} 
<h2>posts</h2> 
    {% if posts %} 
     <ul> 
     {% for post in posts %} 
     <li><a href="{{ post.get_absolute_url }}">{{post.title}}</a></li> 
     {% endfor %} 
     </ul> 
    {% else %} 
     <p>No hay post's</p> 
    {% endif%} 
{% endblock %} 

view_post.html

{% extends 'base.html' %} 
{% block head_title %} {{post.title}} {% endblock %} 
{% block title %} {{post.title}} {% endblock %} 

{% block content %} 
    {{post.body}} 
{% endblock %} 

我不能讓一個url像

本地主機:8000 /現場塞/細節

不起作用這個網址有人可以幫助我?

回答

0

嘗試改變蛞蝓模式,並添加$到URL的末尾:

urlpatterns = patterns('', 
    url(r'^$', 'blog.views.index', name='index'), 
    url(r'^blog/view/(?P<slug>[-\w]+)/$', 'blog.views.view_post', name='view_blog_post'), 
    url(r'^blog/view/(?P<slug>[-\w]+)/details/$', 'blog.views.view_details', name='view_blog_details'), 
)