【问题标题】:How do I set my django view's URL to the root domain of my website?如何将我的 django 视图的 URL 设置为我网站的根域?
【发布时间】:2014-04-23 12:15:54
【问题描述】:

我在 Heroku 上托管我的 Django 应用程序,并且我的域名已在 Network Solutions 注册。当我输入我的域名时,即 www.example.com,Heroku 显示:

"Not Found

The requested URL / was not found on this server."

然后,我必须导航到显示我的应用登录页面的模板 URL,www.example.com/shipment/。

如何让我的根域 www.example.com 自动重定向到 www.example.com/shipment/,或者将 /shipment/ 的 URL 更改为我的根域?

这是我的应用程序的 urls.py:

from django.conf.urls import patterns, url
from shipment import views

urlpatterns = patterns('',
    url(r'^$', views.landing, name='landing'),
    url(r'^create-subscription/$', views.createSubscription, name='createSubscription'), #
    url(r'^(?P<subscription_duration>\d+)/create-account/$', views.createAccount,     name='createAccount'),
    url(r'^create-account/pay/$', views.pay, name='pay'),
    url(r'^create-account/confirm/$', views.confirm, name='confirm'),
)

这是我项目的 urls.py:

from django.conf.urls import patterns, include, url

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'subscription.views.home', name='home'),
    # url(r'^subscription/', include('subscription.foo.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    url(r'^shipment/', include('shipment.urls', namespace = "shipment")),    
    url(r'^admin/', include(admin.site.urls)),
)

【问题讨论】:

  • 如果你把url(r'^$', 'shipment.urls.landing', name='landing'), 加到你的项目url 怎么办?

标签: python django heroku


【解决方案1】:

我变了:

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

到

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

然后就解决了!

【讨论】:

    【解决方案2】:

    你的问题是这一行:

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

    意思是“包括所有这些网址,但它们必须以“shipment/”开头。

    您可以在您网站的 urls.py 中添加一行,例如:

    url(r'^$', generic.RedirectView(url='/shipment/', Permanent=False)),

    (然后还在文件顶部放置相应的“from django.views import generic”)。

    这会将您的 URL / 映射到重定向到 /shipment/ 的重定向视图。

    【讨论】:

      猜你喜欢
      • 2012-07-13
      • 1970-01-01
      • 2011-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-13
      • 1970-01-01
      相关资源
      最近更新 更多