django url不工作,管理员除外

django url不工作,管理员除外,django,django-urls,Django,Django Urls,嗨,伙计们,我的django应用程序URL不工作 下面是我的项目url.py urlpatterns = patterns('', url(r'^admin/', include(admin.site.urls)), url(r'^', include('recipe.urls', namespace="recipe")), ) 这是我的应用程序url.py from django.conf.urls import patte

嗨,伙计们,我的django应用程序URL不工作 下面是我的项目url.py

     urlpatterns = patterns('',

           url(r'^admin/', include(admin.site.urls)),
            url(r'^', include('recipe.urls', namespace="recipe")),

    )
这是我的应用程序url.py

from django.conf.urls import patterns,url


urlpatterns = patterns('recipe.views',

    url(r'^$', 'index', name='index'),
    url(r'^create/recipe/$', 'create_recipe', name='create_recipe'),
    url(r'^create/ingredients/(?P<recipe_id>\d+)/$', 'create_ingredients', 
    name="create_ingredients"),
    url(r'^create/steps/(?P<recipe_id>\d+)/$', 'create_steps', 
    name="create_steps"),
     url(r'^view/recipe/(?P<recipe_id>\d+)/$', 'view_recipe', 
    name="view_recipe"),
)
来自django.conf.url导入模式,url
urlpatterns=patterns('recipe.views',
url(r'^$'、'index',name='index'),
url(r“^create/recipe/$”、“create\u recipe”、name='create\u recipe'),
url(r'^create/Components/(?P\d+)/$,“create_Components”,
name=“创建配料”),
url(r“^create/steps/(?P\d+)/$”,“create_steps”,
name=“创建步骤”),
url(r'^view/recipe/(?P\d+/$),“view\u recipe”,
name=“查看配方”),
)

我不能得到索引页和其他网址,除了管理员工作正常。请帮助我在您的
设置.py中查找并编辑此-

TEMPLATE_DIRS = (
    os.path.join(os.path.abspath(os.path.dirname(__file__)), "templates"),
)

从问题注释来看,您似乎对Django的模板而不是urlconfig有问题。以下是Django模板的工作原理。在
settings.py
中定义一个变量
TEMPLATES\u DIRS
,其中指定Django将在其中查找模板的所有目录的元组

假设您有以下
模板\u DIRS

TEMPLATES_DIRS = (
    '/absolute/path/to/foo',
    '/absolute/path/to/bar',
)
然后,如果您查找模板
base.html
,Django将在以下位置查找该模板,如果找到,将使用第一个位置:

/absolute/path/to/foo/base.html
/absolute/path/to/bar/base.html
在您的案例中,您提到您将模板存储在Django的项目文件夹和应用程序文件夹中。在这种情况下,您必须确保在
TEMPLATES\u DIRS
中定义了这两个文件夹,如:

TEMPLATES_DIRS = (
    '/absolute/path/to/project/templates',
    '/absolute/path/to/app/templates',
)
那么在您的情况下,Django将能够找到
base.html
index.html
。现在,为了简化操作,您可以在
设置.py
中定义
项目路径
,它将存储项目路径的绝对路径,以便您可以轻松地将项目移动到其他位置。我想这就是你的问题所在。在Django>=1.4中,您有以下项目结构:

/project         <= this should be your PROJECT_PATH
  /project       <= instead of this
    templates/
      base.html
    settings.py
  /recipe
    templates/
      index.html
    models.py
在上面的项目中,_PATH计算项目的绝对路径。假设您的
settings.py
位于
/some/path/project/project/settings.py
。然后计算项目路径,如下所示:

>>> # settings.py

>>> print __file__
/some/path/project/project/settings.py
>>> print os.path.join(__file__, '..', '..')
/some/path/project/project/settings.py/../../
>>> # now abspath normalizes the path two levels up
>>> print os.path.abspath(os.path.join(__file__, '..', '..'))
/some/path/project

>>> # now you figure out the project name so that you can get the project templates folder
>>> print os.path.basename(os.path.abspath(os.path.join(__file__, '..', '..')))
project

>>> print os.path.join(PROJECT_PATH, PROJECT_NAME, 'templates')
/some/path/project/project/templates
>>> print os.path.join(PROJECT_PATH, 'recipe', 'templates')
/some/path/project/recipe/templates

它显示了什么错误?它是否说视图不存在?如果我说am get template不存在base.htmlIt与url.py无关。此base.html包含在任何模板中?否则,base.html是否存在于“templates”文件夹中,以及在settings.py文件中是否正确定义了“templates”文件夹?在我的项目中,我有templates文件夹。在这个base.html文件中。在我的应用程序文件夹中,我还有模板文件夹。我有index.htmlhey miki,非常感谢它的工作:)顺便说一句,你能解释一下这两个语句的作用吗PROJECT\u PATH=os.PATH.abspath(os.PATH.join(文件“…”,“…”))PROJECT\u NAME=os.PATH.basename(PROJECT\u PATH)
>>> # settings.py

>>> print __file__
/some/path/project/project/settings.py
>>> print os.path.join(__file__, '..', '..')
/some/path/project/project/settings.py/../../
>>> # now abspath normalizes the path two levels up
>>> print os.path.abspath(os.path.join(__file__, '..', '..'))
/some/path/project

>>> # now you figure out the project name so that you can get the project templates folder
>>> print os.path.basename(os.path.abspath(os.path.join(__file__, '..', '..')))
project

>>> print os.path.join(PROJECT_PATH, PROJECT_NAME, 'templates')
/some/path/project/project/templates
>>> print os.path.join(PROJECT_PATH, 'recipe', 'templates')
/some/path/project/recipe/templates