我知道这是一个非常菜鸟的问题,但是当我尝试呈现我的产品模板时需要帮助,它返回此错误,前两个页面已正确呈现,例如 about 、 home 和联系,但是当我尝试为我的产品 页面添加另一个应用时,它无法加载。
#THE ERROR
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/%7B%25%20url%20'products'%7D
Using the URLconf defined in trydjango.urls, Django tried these URL
patterns, in this order:
1. ^admin/
2. ^$ [name='home']
3. ^about/$ [name='about']
4. ^products/$ [name='products']
5. ^contact/$ [name='contact']
6. ^accounts/
7.^static\/(?P<path>.*)$
8.^static\/(?P<path>.*)$
The current URL, {% url 'products'}, did not match any of these.
#This is the structure
src
>> contact
>> products
>> migrations
>> templates
>> products.html
>> profiles
>> migrations
>> templates
>> about.html
>> base.html
>> home.html
>> navbar.html
#from the products app this is the views.py
def products(request):
context = {}
template = 'products.html'
return render(request, template,context)
#This is the config for urls
from profiles import views as profiles_views
from contact import views as contact_views
from products import views as products_views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', profiles_views.home, name='home'),
url(r'^about/$', profiles_views.about, name='about'),
url(r'^products/$', products_views.products, name='products'),
url(r'^contact/$', contact_views.contact, name='contact'),
url(r'^accounts/', include('allauth.urls')),
]
#and this is where I called it in navbar
<li class="nav-item">
<a class="nav-link js-scroll-trigger" href="{%url'products'}">Products</a>
</li>
请您参考如下方法:
如果有包含,请不要命名 url,要这样做,请转到 url 并添加:
app_name='products'
urlpatterns = [
url(r'^$', your_view.path, name='home'),
]
并将其引用到:
{% url 'products:home' %}
