我已经创建了Django,但它没有加载我的主页

2024-04-20 10:53:29 发布

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

我正在学习Django应用程序,我被卡住了。我不知道这个错误,因为编译器工作正常,但是URL没有加载。这是我的密码

视图.py

'''
from django.shortcuts import render
   def home(request):
       # prams={'name':'Abhinav', 'place':'Earth'}
       return render(request, "index.html") 
'''

index.html

'''
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Template Home</title>
</head>
<body>
Welcome to template:
<h1>Text Analyzer : Enter your text below : </h1>
<form action='/analyze' method='get'>
  <textarea name='text'  style='margin: 0px; width: 1498px; height:  166px;'></textarea><br>
  <input type='checkbox' name='removepunc'>Remove Punctuations<br>
  <input type='checkbox' name='spaceremove'>Remove Space<br>
  <input type='checkbox' name='capfirst'>Capitalize First<br>
  <input type='checkbox' name='newlineremove'>Remove New Line <br>
  <button type="submit">Analyzer</button>
</form>
</body>
</html>

'''

url.py

'''
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.home, name='home'),
    path('analyze/', views.analyze, name='analyze'),]
'''

没有错误,但URL未连接;它应该显示index.html文件,但它显示了安装应用程序成功,Django应用程序的默认页面。请帮忙。我已经提到了上面的代码


Tags: pathdjangonamefrombrimport应用程序home
2条回答
textUtils
|
+templates
|    |
|    +analyze.html
|    +index.html
+text_Utils
|    |
|    +__pycache__
|    +__init__.py
|    +asgi.py
|    +settings.py
|    +urls.py
|    +views.py
|    +wsgi.py
+db.sqlite3
+manage.py

这里views.home正在访问index.html页面

您是否在TEMPLATES = [. 下的settings.py中添加了以下内容

替换

'DIRS': [],

'DIRS': [os.path.join(BASE_DIR, 'templates')], # in django2
or

'DIRS': [BASE_DIR.joinpath('templates')],# in django3

要访问主项目目录中templates文件夹下的模板文件,需要执行此操作

相关问题 更多 >