【问题标题】:Using anchor tag to call a view django使用锚标签调用视图 django
【发布时间】:2015-12-18 16:00:20
【问题描述】:

project.urls

from django.contrib import admin
from django.conf.urls import include, url, patterns
from django.conf.urls.static import static
from django.conf import settings
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^$', include('app.urls', namespace = 'app')),
)+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

urlpatterns += staticfiles_urlpatterns()

app.urls

urlpatterns = patterns('',
# ex: /polls/,
 url(r'^', index.as_view(), name = 'index'),
 url(r'^contact/', ContactMail.as_view(), name = 'contact'),
# url(r'^register/', register.as_view(), name = 'register'),
# url(r'^login/', login_user.as_view(), name = 'login'),

)+ 静态(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

urlpatterns += staticfiles_urlpatterns()

html

<a class="page-scroll" href="{% url 'app:contact' %}">Contact</a>

我得到错误:

Reverse for 'contact' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'$contact/']

知道为什么会这样吗?

提前致谢!

【问题讨论】:

  • 试过url(r'^contact/$', ContactMail.as_view(), name = 'contact'), 吗? url(r'^$', index.as_view(), name = 'index'), 也一样

标签: python html django views


【解决方案1】:

您已通过以 $ 结尾的正则表达式包含应用程序网址,因此包含的任何内容都不会匹配。不要那样做。

url(r'^', include('app.urls', namespace = 'app')),

【讨论】:

  • 但我希望我的主页是 www.domainname.com 而不是 www.domainname.com/index/ 。我应该怎么做?谢谢!
【解决方案2】:

将您的应用网址包含为

url(r'^$', include('app.urls', namespace = 'app')),

意味着您将获得匹配的唯一网址是www.domainname.com/,因为您告诉django该网址必须以''开头,不包含任何字符并以''结尾,因此在您的应用程序的网址中只有与 index 视图匹配的 url。

如果您希望应用程序的 URL 从 / 开始提供,您的 URL 应该导入到

url(r'^', include('app.urls', namespace = 'app')),

例如,如果您希望您的应用网址以/app/ 开头,例如/app/contact/,您可以这样做

url(r'^app/', include('app.urls', namespace = 'app')),

查看django docs on this了解更多详情。

【讨论】:

    猜你喜欢
    • 2014-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-22
    • 2019-09-26
    • 2018-12-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多