2012-02-28 58 views
-3
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可能只是缺少的东西 谢谢

+0

那么,哪个代码导致你的问题,你觉得呢? – Marcin 2012-02-28 13:10:32

+0

@ephan - 我建议你发布你的模板代码部分,它显示了正在生成的链接。 – 2012-02-28 13:34:40

+0

@DominicRodger我刚刚把模板代码。 – ephan 2012-02-28 13:46:25

回答

0

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

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

另外我会用@pemarlink修饰器为您的get_absolute_url函数。看看here

+0

非常感谢,试过了,没有帮助,我一直在永久战斗了几天,网址总是显示空白。我知道我的大部分问题都是基本的,仍然在学习。现在我要用django-context这个问题的处理器。 – ephan 2012-02-28 15:18:51