我知道有一个类似的帖子。我已经审查过它,它与我目前在 Pythonanywhere.com 上所经历的完全不同

我正在尝试将我通过 tangowithdjango.com 完成的 rango 教程项目部署到 Pythonanywhere.com。当我尝试打开网站(网址是 ragzputin.pythonanywhere.com)时,我得到了这个页面:



这是我的项目的 urls.py 文件:

from django.conf.urls.defaults import *
from django.conf import settings

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',

    url(r'^rango/', include('rango.urls')),
    url(r'^admin/', include(admin.site.urls)),
)

if settings.DEBUG:
urlpatterns += patterns(
    'django.views.static',
    (r'media/(?P<path>.*)',
    'serve',
     {'document_root': settings.MEDIA_ROOT}), )

如果你们需要一些额外的信息,这是我的应用程序 urls.py(应用程序名称是 rango):
from django.conf.urls import patterns, url
from rango import views

urlpatterns = patterns('',
    url(r'^$', views.index, name="index"),
    url(r'^about/$', views.about, name="about"),
    url(r'^add_category/$', views.add_category, name="add_category"),
    url(r'^category/(?P<category_name_url>\w+)/add_page/$', views.add_page,   name="add_page"),
    url(r'^category/(?P<category_name_url>\w+)/$', views.category, name="category"),
    url(r'^register/$', views.register, name="register"),
    url(r'^login/$', views.user_login, name="login"),
    url(r'^restricted/$', views.restricted, name="restricted"),
    url(r'^logout/$', views.user_logout, name="logout"),
    url(r'^profile/$', views.profile, name="profile"),
    url(r'^goto/$', views.track_url, name="track_url"),
    url(r'^like_category/$', views.like_category, name="like_category"),
    url(r'^suggest_category/$', views.suggest_category, name="suggest_category"),
    url(r'^auto_add_page/$', views.auto_add_page, name="auto_add_page"),
)

从上面的 404 页面可以看出,服务器无法访问以“^ra​​ngo/”开头的主页。有什么建议么?

最佳答案

您应该将主页的 url 添加到项目的 urls.py 中:

urlpatterns = patterns('',
    url(r'^$', 'rango.views.index', name="index"),
    url(r'^rango/', include('rango.urls')),
    url(r'^admin/', include(admin.site.urls)),
)

并且不要忘记从 rango/urls.py 中删除相同的(第一个)url。

另一种选择是添加从 //rango/ 的重定向。如果你选择这个选项,那么项目的 urls.py 应该是:
from django.views.generic.base import RedirectView

urlpatterns = patterns('',
    url(r'^$', RedirectView.as_view(url='/rango/')),
    url(r'^rango/', include('rango.urls')),
    url(r'^admin/', include(admin.site.urls)),
)

当然,在这种情况下 rango/urls.py 应该保持不变。

关于找不到主页的 PythonAnywhere Django 404 页面,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28007450/

10-13 02:55