我不明白如何在 slug 字段之后生成 url,输入 stop another page as django ago 以保存 slug 字段值或应该转到 views.py 的值
我在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})
视图.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)})
网址.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'),
)
索引.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 %}
我不能做一个像这样的网址
本地主机:8000/field-slug/details
这个网址不起作用有人可以帮助我吗?