class Entry(models.Model):
....
slug = models.SlugField(help_text = "You do not need to change this unless you want to change the url")
class Meta:
verbose_name_plural = "Entries"
def __unicode__(self):
return self.title
def get_absolute_url(self):
cat = slugify(self.category)
return "%s/%s/" % (cat,self.slug)
意见
def index(request):
all_entries = Entry.objects.filter(status=1)
treatments = all_entries.filter(category='treatments')
female = all_entries.filter(category='female')
male = all_entries.filter(category='male')
work = all_entries.filter(category='work')
return render_to_response('index.html',locals())
def entry_page(request,slug_add):
all_entries = Entry.objects.filter(status=1)
page = all_entries.get(slug=slug_add)
treatments = all_entries.filter(category='treatments')
female = all_entries.filter(category='female')
male = all_entries.filter(category='male')
work = all_entries.filter(category='work')
return render_to_response('index.html',locals())
网址
url(r'^$','hypno_pages.views.index'),
url(r'^admin/', include(admin.site.urls)),
url(r'^$','hypno_pages.views.index'),
url(r'^(treatments|male|female|work)/(?P<slug_add>[a-zA-Z0-9-]+)/$','hypno_pages.views.entry_page'),
模板
<div class="subnav ui-corner-all">
<h3>xxxxx can help to treat any of the following conditions </h3>
<ul class = 'float' >
{% for line in treatments|slice:":5" %}
<li ><a href='{{line.get_absolute_url}}'>{{ line.title }}</a></li>
{% empty %}
{% endfor %}
</ul>
<ul class = 'float'>
{% for line in treatments|slice:"5:10" %}
<li ><a href="{{line.get_absolute_url }}" >{{ line.title }}</a></li>
{% empty %}
{% endfor %}
</ul>
.......
*编辑*这是模板代码,只是截断它,其他部分只是重复。Django的网址不断重复(动态导航)
我的问题。我有一个导航栏的主索引页,有一个下拉框有很多链接(一旦客户端添加了一些内容,这些链接就会从数据库动态添加,现在我的问题是在导航链接上说我点击链接'http://127.0.0.1:8000/treatments/what-to-do/'我去了一个链接页面,但现在导航栏中的所有链接都改为'http://127.0 .0.1:8000 /治疗/做什么/治疗/做什么/'根据特定的链接。 我是一个星期的Django和一个月的Python可能只是缺少的东西 谢谢
那么,哪个代码导致你的问题,你觉得呢? – Marcin 2012-02-28 13:10:32
@ephan - 我建议你发布你的模板代码部分,它显示了正在生成的链接。 – 2012-02-28 13:34:40
@DominicRodger我刚刚把模板代码。 – ephan 2012-02-28 13:46:25