Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在Django中显示视图?_Python_Django - Fatal编程技术网

Python 如何在Django中显示视图?

Python 如何在Django中显示视图?,python,django,Python,Django,我对Django完全是新手,我正试图了解它是如何工作的(我更习惯PHP和Spring框架)。 我有一个名为testrun的项目,其中有一个名为graphs的应用程序,因此我的views.py看起来像: #!/usr/bin/python from django.http import HttpResponse def index(request): return HttpResponse("Hello, World. You're at the graphs index.") 然后,在

我对
Django
完全是新手,我正试图了解它是如何工作的(我更习惯
PHP
Spring
框架)。 我有一个名为
testrun
的项目,其中有一个名为
graphs
的应用程序,因此我的
views.py
看起来像:

#!/usr/bin/python
from django.http import HttpResponse

def index(request):
   return HttpResponse("Hello, World. You're at the graphs index.")
然后,在
graphs/url.py
中:

from django.conf.urls import patterns, url, include
from graphs import views

urlpatterns = patterns(
   url(r'^$', views.index, name='index'),
)
from django.conf.urls import patterns, include, url
from django.contrib import admin

admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'testrun.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),
    url(r'^graphs/', include('graphs.urls')),
    url(r'^admin/', include(admin.site.urls)),
)
最后,在
testrun/url.py

from django.conf.urls import patterns, url, include
from graphs import views

urlpatterns = patterns(
   url(r'^$', views.index, name='index'),
)
from django.conf.urls import patterns, include, url
from django.contrib import admin

admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'testrun.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),
    url(r'^graphs/', include('graphs.urls')),
    url(r'^admin/', include(admin.site.urls)),
)
但是,当我尝试访问
http://127.0.0.1:8000/graphs/
我得到:

Page not found (404)
Request Method: GET
Request URL:    http://127.0.0.1:8000/graphs/
Using the URLconf defined in testrun.urls, Django tried these URL patterns, in this order:
^admin/
The current URL, graphs/, didn't match any of these.
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.

我做错了什么,不能在浏览器中显示简单的消息?

要展开我的评论,函数
patterns()
的第一个参数是

应用于每个视图函数的前缀

您可以在此处找到更多信息:

因此,在
graphs/url.py
中,您需要像这样修复模式调用:

urlpatterns = patterns('', # <-- note the `'',`
   url(r'^$', views.index, name='index'),
)

urlpatterns=patterns(“”,#您是否已在已安装的应用程序中添加了
图形
?您能告诉我们应用程序目录的文件树是什么样子吗?似乎您导入了错误的
URL.py
模式的第一个参数应该是
。如果您喜欢PhP,您可能想看看mako,这将是一个更简单的转换。另外你可能也想玩一个微框架,以我的经验,django有一点学习曲线。一次只考虑一件事通常是好的:)