当我在Djang运行服务器时,我正在获取的模板不存在

2024-04-27 05:09:35 发布

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

当我运行服务器时,我得到的模板在/中不存在索引.html第页。其他模板也会出现同样的问题。 我曾经尝试过类似问题的解决方案,比如检查模板目录和安装的应用程序设置.py但似乎什么都没用。 另外,当我在其他位置创建一个新的项目文件夹并将所有代码复制到那里时,它通常是有效的。 这是我的文件夹树:

enter image description here

我目前正在学习Django和stack overflow的新功能。有什么需要帮忙的吗?你知道吗

这是我的网址.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')),
]

这是我的网址.py基本应用程序下的文件:

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

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

这是我的视图.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})

这是我的设置.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',
        ],
    },
},
]

Tags: pathdjangofrompyimportformappbasic
3条回答

我想在行政部门就这么做吧

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})

希望这有帮助

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

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

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

在你的设置.py检查TEMPLATE\u DIR指向的目录,以便您可以配置它的确切位置。你知道吗

print(TEMPLATE_DIR) 

然后在视图.py

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

因此django从中呈现模板。你知道吗

TEMPLATE_DIR + "folder/my_html_file.html"

相关问题 更多 >