gpt4 book ai didi

python - Django 中的链接

转载 作者:太空宇宙 更新时间:2023-11-04 10:50:07 24 4
gpt4 key购买 nike

我正在尝试让我的链接在 Django 中正常工作。输入时所有 URL 都有效,但我无法弄清楚内部导航。它们都是 app.com/storename/pagename 格式,所以如果我在 app.com/shoestore/products 上单击位置,我应该转到 app.com/shoestore/location。我失去了鞋店部分。

这是一个示例 View :

def homepage(request, store_subdomain):
store_db, store_products = database_selector(store_subdomain)
return render_to_response('base.html',
{'store_name': store_db.name, 'store_subdomain':store_subdomain})

我的 urls.py:

urlpatterns = 模式('',

url(r'^admin/', include(admin.site.urls)),
url(r'^(?P<store_subdomain>\w+)/$', homepage),
url(r'^(?P<store_subdomain>\w+)/products/$', products),
url(r'^(?P<store_subdomain>\w+)/shoppingcart/$', shoppingcart),
url(r'^(?P<store_subdomain>\w+)/checkout/$', checkout),
url(r'^static/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.STATIC_ROOT}),
url(r'^media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT}),

)

和我的导航标签:

<li><a href = "/">Home</a></li>
<li><a href = "/products/"}>Products</a></li>
<li><a href = "/location/">Location</a></li>
<li><a href="mailto:{{store_db.email}}">Email Us</a> </li>

最佳答案

使用命名 url patterns .

url(r'^(?P<store_subdomain>\w+)/$', homepage, name='home')

<li><a href="{% url home store_subdomain %}">Home</a></li>

关于python - Django 中的链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14525487/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com