Django url保持重复(动态导航)

时间:2012-02-28 13:04:39

标签: python django permalinks

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

 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>
  .......

* edit *这是模板代码,只是截断它,其他部分只是重复。

我的问题。我有一个带有导航栏的主索引页面,其中有一个包含大量链接的下拉框(一旦客户端添加内容,将从数据库中动态添加,现在我的问题是在导航链接上说我点击链接'http://127.0.0.1:8000/treatments/what-to-do/'我转到链接页面,但现在导航栏中的所有链接都变为'http://127.0。 0.1:8000 /治疗/做什么/治疗/做什么/根据特定的链接。    我和Django一起工作一个星期,而python一个月也许只是缺少一些东西。 感谢

1 个答案:

答案 0 :(得分:0)

对我来说,看起来你应该在“a hrefs”中使用绝对路径,因为就像你使用它一样,链接是相对的,并且会附加到你已经在的(url-)路径上,

尝试<a href="/{{line.get_absolute_url }}" >

此外,我还会为您的@pemarlink功能使用get_absolute_url装饰器。看看here