python - 站点包中模板中的 URL

标签 python html django

我正在构建我的第一个 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/

相关文章:

python - Django (1.10) 覆盖 AdminSite

python - django 中的部分反向 url 查找

jquery - 将图像发送到服务器 - 错误 415(不支持的媒体类型): "Unsupported media type\"image/png\"in request."

python - 如何使用 Python 将多个工作表中的 .csv 文件合并为一个 .xls 文件?

python - 在 Django 中获取 ManytoMany 的错误?

javascript - 单击按钮打开漂亮的照片(多张图片)

html - 填充不会在 html 中消失

python3如何设置单元测试中通过的测试

Python 3 导入优先级与 __init__.py

javascript - 有没有一种自动方法来检测 html 元素(标签)何时出现在屏幕上?