2014-11-02 53 views
1

我不能爲了我的生活找出爲什麼我的URL配置不會指向django中的頁面。Django url confs不會匹配

我想在底部訪問錯誤信息中顯示一個URL(無法得到逃避的鏈接,抱歉)

YellowOrangeFoxZebra是一個有效的imgID,在模型中正確定義。

imgApp/urls.py 

from django.conf.urls import patterns, url 
from imgApp import views 
urlpatterns = patterns('', 
    url(r'^$', views.index, name='index'), 
    url(r'^(?P<imgID>\d+)/$', views.detail, name='detail'), 
) 

views.py 

def detail(request, given_image_ID): 
image = get_object_or_404(imgAppImage, imgID=given_image_ID) 
return render(request, 'imgApp/detail.html', image) 

detail.html 

<img src="{{ image.imgFile.url }}" > 

錯誤消息,我回去了Django的是:

Page not found (404) 
Request Method: GET 
Request URL: http://127.0.0.1:8000/imgApp/YellowOrangeFoxZebra/ 
Using the URLconf defined in thatSite.urls, Django tried these URL patterns, in this order: 
^imgApp/ ^$ [name='index'] 
^imgApp/ ^(?P<imgID>\d+)/$ [name='detail'] 
^imgApp/ ^(?P<pk>\d+)/results/$ [name='results'] 
^imgApp/ ^(?P<question_id>\d+)/vote/$ [name='vote'] 
^admin/ 
^media/(?P<path>.*)$ 
The current URL, imgApp/YellowOrangeFoxZebra/, didn't match any of these. 
+0

你爲什麼使用'\ d +'? – 2014-11-02 15:49:12

+0

因爲它在本教程中使用過,所以我不知道它的含義。這是我第一次用正則表達式。 – Jon 2014-11-02 16:04:15

+0

嘗試'^ imgApp/\ w +/$' – 2014-11-02 16:06:11

回答

1

由於阿維納什·拉吉上述糾正錯誤我的rege X。我已經修復所有的代碼彈出的問題固定的是,一對夫婦類型的錯誤後,和對子孫後代已經發布的代碼的正確版本如下問題:

imgApp/urls.py 

from django.conf.urls import patterns, url 
from imgApp import views 
urlpatterns = patterns('', 
    url(r'^$', views.index, name='index'), 
    url(r'^(?P<inputID>\w+)/$', views.detail, name='detail'), 
) 

我換了d +用W +,並修復它!當它需要尋找單詞時,它正在尋找數字。


views.py 

def detail(request, inputID): 
    image = get_object_or_404(imgAppImage, imgID=inputID) 
    context = {'image': image} 
    return render(request, 'imgApp/detail.html', context) 

我改名given_image_ID的清晰度inputID,增加了行定義背景,並在渲染調用


detail.html 

<img src="{{ image.imgFile.url }}" > 

我有這個權與語境置換圖像!但現在它顯示的是全尺寸的圖像,不幸的是它可能比窗口大得多,所以這是下一個要解決的問題!