python - 在我的浏览器中看不到 view.py。 Django

标签 python django

我刚刚安装了 Django,并在views.py 文件中创建了一个简单的 HTML 页面。当我尝试为 HTML 页面创建主页 URL 时,我无法显示它。

这是我在 urls.py 文件中使用的代码:

from django.conf.urls.defaults import *
from bookmarks.views import *
urlpatterns = patterns('',
  (r'^$', main_page),
)

我错过了什么?浏览器调试器说第 2 行有语法错误,但我直接从书中复制了它。

编辑:

这就是我现在的语法错误(views.py):

from django.http import HttpResponse
def main_page(request):
    output = '''
        <html>
            <head><title>%s</title></head>
            <body>
                <h1>%s</h1><p>%s</p>
            </body>
        </html>
''' % (
    'Django Bookmarks',
    'Welcome to Django Bookmarks',
    'Where you can store and share bookmarks!'
)
return HttpResponse(output)

我也从书中复制了这个......所以我很困惑

最佳答案

您的缩进已关闭:

from django.http import HttpResponse

def main_page(request):
    output = '''
        <html>
            <head><title>%s</title></head>
            <body>
                <h1>%s</h1><p>%s</p>
            </body>
        </html>
    ''' % (
        'Django Bookmarks',
        'Welcome to Django Bookmarks',
        'Where you can store and share bookmarks!'
    )

    return HttpResponse(output)

Python 使用缩进来表示 block ,因此如果您的代码缩进不正确,它就不会按照您希望的方式运行。

关于python - 在我的浏览器中看不到 view.py。 Django ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13775007/

相关文章:

python - 读取同一行中多个项目的项目

python - csrf_exempt 的用例

Django ORM、CharField 和空白=真

javascript - 如何让 Mailchimp 弹出窗口在 Chrome 中工作

python - 查找日期和时间距现在不到 24 小时的对象

python - 编写错误函数以在 python 中提供 scipy.optimize.least_squares

python - 在matplotlib中使用数据坐标时如何使y轴文本出现在图中?

python - 什么类型错误,__init__() 缺少 1 个必需的位置参数 : 'get_response' mean in python?

django - Gunicorn 退出,因为来自 Django 的 https 请求在 OpenSSL 中失败

python - Django-tastypie prepend_urls 中的多个 url