【问题标题】:django template just reloads when clicking a link pointing to another template单击指向另一个模板的链接时,Django 模板只会重新加载
【发布时间】:2020-05-05 16:19:16
【问题描述】:

我的 django 中的模板主页包含一个指向另一个页面的 url,但是当单击此链接时,主页模板只是重新加载,这里是 urls.py、views.py 和 home.html 的代码

这是 urls.py

    from django.conf.urls import include, url
from django.contrib import admin
from pizza import views

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'',views.home,name='home'),
    url(r'order/',views.order,name='order'),
]

这是views.py

    from django.shortcuts import render

# Create your views here.

def home(request):
    return render(request,'pizza/home.html')

def order(request):
    return render(request,'pizza/order.html')

这是 home.html

  <h3>
    Nandias Garder
</h3>
<a href="{% url 'order' %}"> order a pizza</a>

【问题讨论】:

    标签: python django django-templates django-urls


    【解决方案1】:

    您的网址格式错误。你应该试试这个:

    urlpatterns = [
        url(r'^admin/', include(admin.site.urls)),
        url(r'^$',views.home,name='home'),
        url(r'^order/',views.order,name='order'),
    ]
    

    如果你注意到这里我使用了^$ 来确保主页 url 只捕获空 url。

    在您的网址中,第二个网址(主网址)将与每个字符串匹配。你可以去 admin,因为它是在 home url 之前定义的。

    【讨论】:

      猜你喜欢
      • 2023-04-10
      • 1970-01-01
      • 1970-01-01
      • 2011-05-28
      • 2013-10-05
      • 1970-01-01
      • 2019-07-16
      • 1970-01-01
      • 2014-07-29
      相关资源
      最近更新 更多