【问题标题】:django hello world not showingdjango hello world 没有显示
【发布时间】:2012-04-08 15:58:59
【问题描述】:

我正在关注tutorial for django,我将我的项目托管在免费托管中进行测试,

主机在 python 和 django shell 上运行良好,

但我在 index.html 中看不到正确的数据或无法访问 /admin

所以我认为是路径错误的问题?,

所以请就这个菜鸟问题提出建议,

这是我保存文件的文件夹/home/mako34/www/blog

这是我的代码:

我想在创建 sqlite db 和必要的文件夹时为 db 正确配置了设置

settings.py

import os
*
* configure connection do db, etc
*

ROOT_URLCONF = 'blog.urls'

TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    os.path.join(os.path.dirname(__file__),'templates'), # creates a absolute path
)

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'blog',
    # Uncomment the next line to enable the admin:
    'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    # 'django.contrib.admindocs',
)

urls.py

from django.conf.urls.defaults 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'^$', 'blog.views.home', name='home'),
    # url(r'^blog/', include('blog.foo.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
    (r'^, 'blog.views.index'), #<------------------- index/root entry
    # Uncomment the next line to enable the admin:
     url(r'^admin/', include(admin.site.urls)),


)

index.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>My Blog</title>
    </head>

<body>
    <H1>Test Django</H1>
    <div id="content">
    {% block content %}
    {% endblock %}
    </div>
</body>

</html> 

那我错过了什么?

所以我可以看到我的索引 html 和数据 [因为现在它显示标签 {% block content %} {% endblock %} 并且其中没有数据

并且无法访问http://mako34.alwaysdata.net/blog/admin/

谢谢!

【问题讨论】:

  • 您是否配置了兼容 WSGI 的网络服务器以在您的项目中使用 wsgi.py 模块?
  • @kosii ,嘿,不,我没有,会搜索如何做到这一点?,谢谢!
  • 这可能只是一个错字:您缺少正则表达式的结束引号:(r'^, 'blog.views.index'), 应该是 (r'^', 'blog.views.index'),
  • @bernie tnx 我修正了这个错字,但仍然没有显示索引,干杯

标签: python django path


【解决方案1】:

您的网址应如下所示:

urlpatterns = patterns('',
     url(r'^admin/', include(admin.site.urls)),
     url(r'^', 'blog.views.index'),
)

请注意,您要使用的索引 URL 现在包含在 url() 调用中。此外,索引 URL 现在跟随管理 URL,因此引用 /admin 的 url 由管理 URL 处理,而不是您定义的全部 index URL。

URL 处理程序在第一次匹配时起作用。您的url(r'^') 匹配所有内容,因此不会让管理员网址有机会工作。您可能应该将其更改为 url(r'^$'),它将匹配“无路径”URL,而不是“每个 url”。注意添加的 $ 符号,标记模式的结束。

编辑:

好的,现在我更了解您的问题了。您要做的是在需要 URL 路径中的前缀的特定服务器路径上部署 django 应用程序。

这通常是标准 URL:

http://www.example.com/
http://www.example.com/admin/
http://www.example.com/index/

相反,这就是你想要做的:

http://www.example.com/myapp/
http://www.example.com/myapp/admin/
http://www.example.com/myapp/index/

Django 通常希望您的应用程序部署在根 URL 上,没有路径。该路径用于查找应由哪个内部 django 应用处理请求。

有两种方法可以解决您的问题。第一个,也是我认为正确的方法,是使用here 中描述的站点框架。

另一种方法是在 urlpatterns 中为所有 URL 添加前缀,如下所示:

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

但您还需要记住将您的“博客”前缀添加到需要诸如 LOGIN_REDIRECT 等 URL 的多个设置中。

您真正应该做的是让 django 在 URL 上工作:mako34.alwaysdata.net 并完全忘记 /blog/,而是修改 apache 以将所有请求重定向到 mod_wsgi。

【讨论】:

  • 嗨,谢谢,做了建议的更改,但仍然没有正确显示 index.html (mako34.alwaysdata.net/blog),在 /admin (mako34.alwaysdata.net/blog/admin) 上,显示 Not found,, The requested URL /在此服务器上找不到 blog/admin/。
  • @MaKo 您应该为管理员点击的网址是 mako34.alwaysdata.net/admin。您博客的 URL 应该是 mako34.alwaysdata.net。给他们一个裂缝
  • 感谢您的回复,那么 urlpatterns 应该是这样的吗? url(r'^admin/', 包括(mako34.alwaysdata.net/admin)), url(r'^', 'mako34.alwaysdata.net'), ;;;当我尝试了这个但仍然无法正常工作时,还是应该有所不同?谢谢!
  • 好的,谢谢!,将尝试该解决方案,但只是为了保持简单,如果我从根目录 /home/mako34 执行此操作,我可以看到以下文件夹:admin cgi-bin www ..那么我应该在这个根文件夹中创建项目“django-admin.py startproject blog”吗?我把这个项目放在 www 谢谢!
  • 将它放在您之前拥有它的 /www/ 中。但请确保您的网络服务器将所有流量直接传递给 django。您页面上的当前索引页面不应该是可见的,因为 django 应该为所有流量提供服务(静态内容除外,您可以稍后弄清楚)。
猜你喜欢
  • 2016-10-02
  • 2011-05-24
  • 2016-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-03
  • 1970-01-01
相关资源
最近更新 更多