2014-01-09 101 views
0

當我點擊'Home'時,我預計我將被重定向到index.htmlDjango進入存檔頁面後無法進入索引頁面

但是在我訪問我的檔案頁面(journal.html)(類似「/雜誌/ 2014/01」這是從數據庫中查詢)

我不能去索引頁或其他地方,只在頁面如/journal/Year/Month/journal/Year

因爲當我試圖去index.html它重定向我/journal/Year/Month/index.html

爲什麼?

我的urls.py:

urlpatterns = patterns(
    '', 
    url(r'^$', index), 
    url(r'^index.html$', index), 
    url(r'^journal.html$', journal), 
    url(r'^contact.html$', contact), 
    url(r'^about.html$', about), 

    (r'^journal/(\d{4})$', journal), 
    (r'^journal/(\d{4})/(\d{2})$', journal), 

    url(r'^auth.html$', auth), 
    url(r'^logout.html$', logx), 
    url(r'^admin/', include(admin.site.urls)), 

    # develop 
    url(r'^Observations/(?P<path>.*)$', 'django.views.static.serve', 
     {'document_root': MEDIA_ROOT}), 
) 

我的應用程序/ views.py:

def index(request): 
    if request.user.is_authenticated(): 
     return render_to_response('index.html', {'user': request.user}) 
    else: 
     return render_to_response('index.html') 


def journal(request, *args): 
    # print args 
    if request.user.is_authenticated(): 
     obss = Obs.objects.all() 
     if len(args)>0: 
      year = args[0] 
      # print 'year', year 
      obss = obss.filter(date__year=year) 
      if len(args) > 1: 
       month = args[1] 
       obss = obss.filter(date__month=int(month)) 
       # print 'month', month 

     ar_obs = Obs.objects.all() 
     archive = create_archive_data(ar_obs) 
     return render_to_response('journal.html', {'obss': obss, 'user': request.user, 
                'archive_counts': archive}) 
    else: 
     state = "Please log in below..." 
     # return HttpResponseRedirect('auth.html', {'state': state}) 
     return render_to_response('auth.html', {'state': state}) 

回答

1

有沒有在你的urls.pyviews.py,可能會導致。

我假設如果您在瀏覽器中輸入{mysite}/index.html,它會按預期轉到主頁?

...只有當你在一個雜誌頁面上,然後點擊'首頁'鏈接,它會去錯誤的網址?

這聽起來像你有一個問題,在您的HTML鏈接href ...可能你的鏈接是這樣的:<a href="index.html">Home</a>

它缺少前面的斜線這樣的URL被視爲相對到當前的URL,即如果你是/journal/2014/01然後單擊該鏈接將帶您到/journal/2014/01/index.html

避免這些種類的最佳方式問題(並避免,如果你改變任何網址在urls.py改變所有的模板)是命名您的網址,並使用Django's url tag

注意,Django的URL模式regular expressions所以你應該逃避.字符作爲\.

此外,利用正則表達式,您只需要一個url模式即可匹配index視圖所需的所有url。與journal視圖相同。

所以你urls.py會是什麼樣子:

urlpatterns = patterns(
    '', 
    url(r'^(?:index\.html)?$', index, name='index'), 
    url(r'^contact\.html$', contact, name='contact'), 
    url(r'^about\.html$', about, name='about'), 

    url(r'^journal(?:\.html$|/(\d{4})(?:/(\d{2}))?$)', journal, name='journal'), 

    url(r'^auth\.html$', auth, name='auth'), 
    url(r'^logout\.html$', logx, name='logout'), 
    url(r'^admin/', include(admin.site.urls)), 

    # develop 
    url(r'^Observations/(?P<path>.*)$', 'django.views.static.serve', 
     {'document_root': MEDIA_ROOT}), 
) 

而在你的模板:

<a href="{% url 'index' %}">Home</a> 
<a href="{% url 'journal' %}>Journal</a> 
<a href="{% url 'journal' '2014' %}>Journal: 2014</a> 
<a href="{% url 'journal' '2014' '06' %}>Journal: June 2014</a> 

我可能會做出進一步的改變,這是使用named groupsjournal網址:

url(r'^journal(?:\.html$|/(?P<year>\d{4})(?:/(?P<month>\d{2}))?$)', journal, name='journal'), 

並更新視圖代碼協議ingly:

def journal(request, year=None, month=None): 
    # print args 
    if request.user.is_authenticated(): 
     obss = Obs.objects.all() 
     if year is not None: 
      obss = obss.filter(date__year=year) 
      if month is not None: 
       obss = obss.filter(date__month=int(month)) 

     ar_obs = Obs.objects.all() 
     archive = create_archive_data(ar_obs) 
     return render_to_response('journal.html', {'obss': obss, 'user': request.user, 
                'archive_counts': archive}) 
    else: 
     state = "Please log in below..." 
     # return HttpResponseRedirect('auth.html', {'state': state}) 
     return render_to_response('auth.html', {'state': state}) 

此外,而不是做is_authenticated檢查,並隨後登錄手動重定向在每一個需要它的觀點,你應該看看使用Django的login_required裝飾:
https://docs.djangoproject.com/en/dev/topics/auth/default/#django.contrib.auth.decorators.login_required

+0

一個很好的答案,但爲什麼你想在你的URL的末尾添加'.html'嗎? Django的長度使它很容易支持漂亮的URL,看起來像這樣毀了它似乎是一種恥辱。我是否錯過了某些好處,或者您是否在OP中選擇忽略它? – ptr

+0

@PeteTinkler是的,我同意這是醜陋的,沒有必要在網址中有'.html',但我假設OP有這樣做的理由(可能將現有的網站轉換爲Django) – Anentropic

+0

謝謝。這是Home。我更改爲Home。感謝您的建議! – Viktor