【问题标题】:I am getting Template Does Not Exist at / when I'm running my server in Django当我在 Django 中运行我的服务器时,我得到模板不存在
【发布时间】:2019-10-27 05:45:44
【问题描述】:

当我运行服务器时,我的 index.html 页面出现 Template Does Not Exist at /。其他模板也会出现同样的问题。 我已经尝试过以前提出的类似问题的解决方案,例如检查模板目录和在 settings.py 文件中安装的应用程序,但似乎没有任何效果。 此外,当我在其他位置创建一个新项目文件夹并将所有代码复制到那里时,它通常可以工作。 这是我的文件夹树:

我目前正在学习 Django 和堆栈溢出方面的新知识。任何帮助,将不胜感激?

这是我的项目文件夹的 urls.py 代码:

from django.contrib import admin
from django.urls import path
from django.conf.urls import url,include
from basic_app import views
urlpatterns = [
    url(r'^$',views.index,name='index'),
    path('admin/', admin.site.urls),
    url(r'basic_app/',include('basic_app.urls')),
]

这是我在 basic_app 下的 urls.py 文件:

from django.conf.urls import url
from basic_app import views

app_name= 'basic_app'
urlpatterns=[
    url(r'^register/$',views.register,name='register')
]

这是我的views.py文件代码:

from django.shortcuts import render
from basic_app.forms  import UserForm, UserProfileInfoForm

def index(request):
    return render(request,'basic_app/index.html')

def register(request):
    registered= False

    if request.method=='POST':
        user_form= UserForm(data= request.POST)
        profile_form= UserProfileInfoForm(data= request.POST)

        if user_form.is_valid() and profile_form.is_valid():
             user= user_form.save()
            user.set_password(user.password)
            user.save()

            profile= profile_form.save(commit=False)
            profile.user=user

            if 'profile_pic' in request.FILES:
                profile.profile_pic= request.FILES['profile_pics']
            profile.save()

            registered= True
        else:
            print(user_form.errors ,profile_form.errors)

    else:
        user_form= UserForm()
        profile_form= UserProfileInfoForm()


    return render(request,'basic_app/registration.html',{'user_form':user_form,'profile_form':profile_form,
                                                    'registered':registered})

这是我的 settings.py 模板文件代码:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIR=os.path.join(BASE_DIR,'templates')
STATIC_DIR=os.path.join(BASE_DIR,'static')
MEDIA_DIR=os.path.join(BASE_DIR,'media')

这是我的已安装应用列表:

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'basic_app',
]

还有这个:

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [TEMPLATE_DIR,],
    'APP_DIRS': True,
    'OPTIONS': {
        'context_processors': [
            'django.template.context_processors.debug',
            'django.template.context_processors.request',
            'django.contrib.auth.context_processors.auth',
            'django.contrib.messages.context_processors.messages',
        ],
    },
},
]

【问题讨论】:

  • 向我们展示您的urls.pyviews.py
  • 以及模板相关的设置。
  • 我已在编辑后的问题中添加了我的文件代码。

标签: python html django django-templates atom-editor


【解决方案1】:

我认为在管理员中只是这样做

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': ['templates'],
    'APP_DIRS': True,
    'OPTIONS': {
        'context_processors': [
            'django.template.context_processors.debug',
            'django.template.context_processors.request',
            'django.contrib.auth.context_processors.auth',
            'django.contrib.messages.context_processors.messages',
        ],
    },
},
]

你的视图应该是这样的

return render(request,'basic_app/registration.html',{'user_form':user_form,'profile_form':profile_form,
                                                    'registered':registered})

希望对你有帮助

【讨论】:

    【解决方案2】:

    在您的 settings.py 中检查 TEMPLATE_DIR 指向的目录,以便您可以配置它的确切位置。

    print(TEMPLATE_DIR) 
    

    然后在 views.py 中:

    def index(request):
        return render(request,'folder/my_html_file.html')
    

    所以 django 从那里渲染模板。

    TEMPLATE_DIR + "文件夹/my_html_file.html"

    【讨论】:

    • 是的.. 谢谢。我弄错了。实际上,我在与主项目文件夹同名的文件夹下创建了模板文件夹。我通过打印模板目录发现了错误。现在它工作正常。
    【解决方案3】:

    如果您使用默认配置设置,则模板应位于 learning_users/basic_app/templates/basic_app/ 中,并按如下方式访问:

    return render(request, `basic_app/index.html`, {})
    

    index.html 是您的模板名称。 这里的目录结构一开始可能看起来有点多余,但您也可以使用settings.py 配置文件对其进行更改。 在您的目录中,模板文件夹直接放在项目文件夹中(即learning_users)。除非您修改了配置文件以更改模板的路径,否则这将导致错误。

    【讨论】:

    • 但是我的 TEMPLATE_DIR 说 **"os.path.join(BASE_DIR,'templates')"** 我认为这应该导致与我的树相同的路径。
    【解决方案4】:

    但也许您可以这样做,确保它在您的 settings.py 文件中有效,将此行添加到已安装的应用程序中

    'basic_app.apps.Basic_appConfig'

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-03
      • 1970-01-01
      • 2021-04-02
      • 2021-07-11
      相关资源
      最近更新 更多