2012-02-28 47 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