簡體   English   中英

django你好世界未顯示

[英]django hello world not showing

我正在遵循django教程 ,將我的項目托管在免費托管中進行測試,

托管可以很好地與python和django shell配合使用,

但我在index.html中看不到正確的數據或無法訪問/ admin

所以我認為路徑有問題嗎?,

所以請就這個菜鳥問題提出建議,

這是我有文件/home/mako34/www/blog的文件夾

這是我的代碼:

我想在創建sqlite數據庫和必要文件夾時為數據庫正確配置了設置

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/

謝謝!

您的網址應如下所示:

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工作機會。 您可能應該將其更改為url(r'^$') ,它將匹配“無路徑” URL,而不是“每個URL”。 請注意,$符號的添加,標志着模式的結束。

編輯:

好的,現在我更好地了解了您的問題。 您要做的是在需要URL路徑中帶有前綴的特定服務器路徑上部署django應用。

通常是標准網址:

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應用應處理該請求。

有兩種方法可以解決您的問題。 首先,也是我認為正確的方法,是使用此處所述的sites框架。

另一種方法是將前綴添加到urlpatterns中的所有URL,如下所示:

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

但是,您還需要記住將“博客”前綴添加到需要URL的多個設置中,例如LOGIN_REDIRECT等。

但是,您真正應該做的是使Django在URL上運行:mako34.alwaysdata.net並完全忘記/ blog /,但是修改apache將所有請求重定向到mod_wsgi。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM