2014-09-23 151 views
0

我在Django是新和使用本教程學習https://docs.djangoproject.com/en/1.6/intro/tutorial03/ 有一點問題,在路由Django的URL無法找到

查看代碼:

def index(request): 
    return HttpResponse(r'<h3 style="font-style: bold;">Index</h3>') 

URL配置代碼:

1.博客/網址

urlpatterns = patterns('', 
    url(r'^$', views.index, name='index') 
) 

2,項目/網址

urlpatterns = patterns('', 
     url(r'^blog/', include('blog.urls')), 
     url(r'^admin/', include(admin.site.urls)), 
    ) 

當我來爲127.0.0.1:8080/index該

未找到頁面(404)

使用URL配置在les1定義.urls,Django按以下順序嘗試了這些URL模式:

  1. ^blog/
  2. ^管理員/

當前的URL,索引,沒有找到相關的這些。

項目的結構

blog/ 
    templates 
    __init__.py 
    admin.py 
    models.py 
    tests.py 
    urls.py 
    views.py 
les1/ 
    __init__.py 
    settings.py 
    urls.py 
    wsgi.py 
db.sqlite3 
manage.py 

找不到錯誤:(

從視圖代碼
+2

請更新您就是您要訪問的URL,你得到完整的錯誤消息,你的問題。 – Alasdair 2014-09-23 19:36:13

+0

聽起來像一個http服務器,因爲Apache正在尋找一個index.html。你如何爲你的django項目運行http服務器? – elmonkeylp 2014-09-23 19:37:16

+0

從視圖響應中刪除r。 – 2014-09-23 20:01:52

回答

0

什麼是你在進入實際的URL你的瀏覽器是127.0.0.1:8000還是127.0.0.1:8000/index?從你的文章中,我認爲它是secon d:

The current URL, index, didn't match any of these. 

如果輸入127.0.0.1:8000/blog會怎麼樣?

index是你的路線,只在服務器端使用的名字。它是用於快速倒車和URL的顯示的快捷方式:

from django.core.urlresolvers import reverse 

print(reverse('index')) 
# will output "/blog", which is the actual URL 
+0

那麼,它不只是一個前綴? 當我設置'^ blog/$'前綴時,它也不是工作。 – Igzar 2014-09-23 21:57:24

+0

它不是一個前綴,它是您在服務器端(django)上反向URL的路由標識符。你不能從客戶端(網絡瀏覽器)使用它。 – 2014-09-23 22:19:23

-1

刪除河

def index(request): return HttpResponse('<h3 style="font-style: bold;">Index</h3>')

+1

感謝您的時間,但它還沒有解決我的問題 – Igzar 2014-09-23 20:51:45

+0

你可以粘貼輸出錯誤? – 2014-09-23 20:58:24

+2

-1:這不會導致該錯誤。 – 2014-09-23 21:06:49