Django show字符串不起作用

2024-04-19 05:10:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我在传递Django的手册

我设置了我的视图.py地址:

from django.http import HttpResponse
from main.views import hello

def hello(request):
    return HttpResponse("Hello world")

文件位于名为:main的子文件夹中。你知道吗

我设置了网址.py地址:

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

urlpatterns = patterns('',
    url(r'^hello/$', hello),
)

当我要:http://localhost:8000//main/hellohttp://localhost:8000/

我明白了:

NameError at /main/hello name 'hello' is not defined

但如果我改变主意视图.py收件人:

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

urlpatterns = patterns('',
)

然后在http://http://localhost:8000/我看到:

It worked!
Congratulations on your first Django-powered page.

我不明白我做错了什么。你知道吗

以下是我的项目文件:

enter image description here

我该怎么解决这个问题?你知道吗


Tags: 文件djangofrompyimport视图localhosthttp
2条回答

您必须在URL页面内导入视图。你知道吗

from django.conf.urls.defaults import patterns, include, url
from main.views import hello

urlpatterns = patterns('',
    url(r'^hello/$', hello),
)

现在您可以通过以下方式访问:

127.0.0.1:8000/hello

您需要从views.py导入hello。你知道吗

urls.py看起来像这样。你知道吗

from django.conf.urls.defaults import patterns, include, url
from main.views import hello

urlpatterns = patterns('',
    url(r'^hello/$', hello),
)

相关问题 更多 >