我正在构建我的第一个 Django 应用程序,所以我知道我错过了很多东西,但我为我的网站安装了一个画廊,并且位于网站包内,我在 urls.py 中设置了 url,它看起来像这样:
from django.contrib import admin
from django.urls import path,include
from django.conf import settings
from django.conf.urls.static import static
from contacto.views import Home,Contacto
urlpatterns = [
path('admin/', admin.site.urls),
path('gallery/', include('gallery.urls'),name='gallery'),
path('home/',Home,name='home'),
path('contacto/',Contacto,name='contacto')
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
如果我在浏览器中写入网址,我可以完美访问图库及其功能,但我无法像使用 {% url 'gallery' %} 的其他网址一样在模板中引用图库网址,我不断收到此错误:
Reverse for 'gallery' not found. 'gallery' is not a valid view function or pattern name.
这也是画廊网址:
from django.urls import path
from gallery.views import ImageView, ImageList, AlbumView, AlbumList, ImageCreate
app_name = 'gallery'
urlpatterns = [
path('', AlbumList.as_view(), name='album_list'),
path('images/', ImageList.as_view(), name='image_list'),
path('image/<int:pk>/<slug>', ImageView.as_view(), name='image_detail'),
path('upload/', ImageCreate.as_view(), name='image_upload'),
path('album/<int:pk>/<slug>/', AlbumView.as_view(), name='album_detail'),
path('album/<int:apk>/<int:pk>/<slug>', ImageView.as_view(), name='album_image_detail')
]
谢谢!
最佳答案
app_name = 'gallery'
urlpatterns = [
path('admin/', admin.site.urls),
path('gallery/', include('gallery.urls')),
path('home/',Home,name='home'),
path('contacto/',Contacto,name='contacto')
]
如上所示进行更改,并调用 gallery:album_list
获取专辑列表,其他部分遵循相同的模式
关于python - 站点包中模板中的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59597831/