當我點擊'Home'時,我預計我將被重定向到index.html
。Django進入存檔頁面後無法進入索引頁面
但是在我訪問我的檔案頁面(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})
一個很好的答案,但爲什麼你想在你的URL的末尾添加'.html'嗎? Django的長度使它很容易支持漂亮的URL,看起來像這樣毀了它似乎是一種恥辱。我是否錯過了某些好處,或者您是否在OP中選擇忽略它? – ptr
@PeteTinkler是的,我同意這是醜陋的,沒有必要在網址中有'.html',但我假設OP有這樣做的理由(可能將現有的網站轉換爲Django) – Anentropic
謝謝。這是Home。我更改爲Home。感謝您的建議! – Viktor