項目名稱:JalaBapa_websiteDjango的URL正則表達式匹配,但找不到網頁的錯誤
JalaBapa_website \網址:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^about_us/', include('About_Us.urls')),
url(r'^activities/', include('Activities.urls')),
url(r'^collections/', include('Collections.urls')),
url(r'^contact_us/', include('Contact_Us.urls')),
url(r'^donate/', include('Donate.urls')),
url(r'^events/', include('Events.urls')),
url(r'^home/', include('Home.urls')),
url(r'^publications/', include('Publications.urls')),
]
出版物/ urls.py:
from django.conf.urls import url
from . import views
app_name = "Publications"
urlpatterns = [
# /publications/
url(r'^$', views.publications, name='publications'),
# /publications/Xyz_9999.pdf/
url(r'(?P<file_name>^[A-Z][a-z]+\_[0-9]{4}\.pdf$)', views.pdfs, name='pdfs')
]
出版物/ views.py
from django.http import HttpResponse, FileResponse
from django.shortcuts import get_object_or_404, render
from .models import *
# Create your views here.
def publications(request):
all_files = Publications.objects.all()
uploaded_file_names = []
for obj in all_files:
uploaded_file_names.append(str(obj.file_name()))
context = {
'uploaded_file_names': uploaded_file_names
}
return render(request, 'publications/publications.html', context)
def pdfs(request, file_name):
with open('media/pdfs/'+file_name, 'r') as pdf:
response = FileResponse(pdf, content_type='application/pdf')
return response
一些代碼:出版物/模板/出版/ publications.html
<div class="container-fluid">
{% for file_name in uploaded_file_names %}
<a href="{{ file_name }}">
<!-- <a href="{% url 'Publications:pdfs' file_name %}">
將繼續...
摘錄上面一行是因爲COMMENTED之前,這是示值誤差:
NoReverseMatch at/publications/
找不到'pdfs'('July_2017','')。 1個圖案(多個)嘗試:[ '出版物/(ΔP^ [AZ] [AZ] + \ _ [0-9] {4} \ PDF $。)']
Here is Error Screen Shot image
我想知道這是爲什麼不工作 - 問題1
總之,<a href="{{ file_name }}">
被硬編碼,但運行,所以我暫時
移動關於繼續留一半以上文件...
<div style="float: left;" class="boxes black-container text-grow">
{{ file_name }}
</div>
</a>
{% endfor %}
</div>
的問題 - 2:在剛剛上面的文件publications.html,當我點擊
{{ file_name }}
,它讓我錯誤404頁面沒有找到,here is screen shot of that page
的鏈接:127.0.0.1:8000/publications/July_2017
的網址匹配是:url(r'^publications/', include('Publications.urls'))
和url(r'(?P<file_name>^[A-Z][a-z]+\_[0-9]{4}\.pdf$)', views.pdfs, name='pdfs')
雖然,當我去https://www.regextester.com/88429
網站它顯示我,我的文件名:July_2017.pdf
與正則表達式匹配:^\[A-Z\]\[a-z\]+\_\[0-9\]{4}\.pdf$
Ooooow ya正確的正確 –
感謝Atcrank,我也會試圖找出第一個問題。 –