Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/276.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 我能';t将view.py和url.py连接到html文件_Python_Django - Fatal编程技术网

Python 我能';t将view.py和url.py连接到html文件

Python 我能';t将view.py和url.py连接到html文件,python,django,Python,Django,我一直在学习Django,现在正在研究所谓的“模板标记”主题。这是使用“url”函数将views.py文件连接到HTML的过程。但是当我试图运行代码时,我从Anaconda提示符中得到消息,“AttributeError:'list'对象没有属性'endswith'”。我怎样才能使这个程序工作?代码如下 ・app_文件/views.py: from django.shortcuts import render from django.http import HttpResponse # Cre

我一直在学习Django,现在正在研究所谓的“模板标记”主题。这是使用“url”函数将views.py文件连接到HTML的过程。但是当我试图运行代码时,我从Anaconda提示符中得到消息,“AttributeError:'list'对象没有属性'endswith'”。我怎样才能使这个程序工作?代码如下

・app_文件/views.py:

from django.shortcuts import render
from django.http import HttpResponse

# Create your views here.

def index(request):
    return render(request,"My_app/index.html")

def second(request):
    return render(request,"My_appp/second.html")

def third(request):
    return render(request,"My_app/third.html")
・项目文件/url.py:

from django.contrib import admin
from django.urls import path,include
from My_app import views
from django.conf.urls import url

urlpatterns = [
    url('admin/', admin.site.urls),
    url("lp",views.index,name="lp"),
    url("My_app/",include("My_app.urls"))
]
・my_app/url.py:

from django.contrib import admin
from django.urls import path
from My_app import views
from django.conf.urls import url

app_name = "My_app"

urlpatterns =[
    url("second/",views.second,name="second"),
    url("third/",views.third,name="third")
]
・模板/app_文件/index.html:

<!DOCTYPE html>
{% load statics %}
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>My Django Practice</title>
    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
  </head>
  <body>
    <div class="container">`enter code here`
      <div class="jambotron">
        <h1>PAGE FOR INDEX</h1>
        <a href="{% url 'My_app:second' %}">LINK TO SECOND.HTML</a>
      </div>
    </div>
  </body>
</html>

在URL.py文件中包含视图文件,然后根据该文件创建路径。
从my_app.views导入索引,第二,第三。在中,URL的位置使用路径。将您的应用程序包括在setings.py文件中

url(“lp/”,views.index,name=“lp”),#“lp”需要后缀a“/”,谢谢您对我的问题发表评论。我修复了它,但仍然收到相同的错误消息。如果你知道发生了什么,你能再帮我一点吗?我创建了一个新项目,运行了你的代码,没有发现任何错误,一切正常。为什么不试试
path()
而不是
url()
<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title></title>
        <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
  </head>
  <body>
    <div class="container">
      <div class="jambotron">
          <a href="{% url 'My_app:third' %}">LINK TO THIRD.HTML</a>
      </div>
    </div>
  </body>
</html>
<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title></title>
        <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
  </head>
  <body>
    <div class="container">
      <div class="jumbotron">
          <a href="{% url 'admin:index' %}">LINK TO ADMIN PAGE</a>
      </div>
    </div>
  </body>
</html>
Watching for file changes with StatReloader
Performing system checks...

Exception in thread django-main-thread:
Traceback (most recent call last):
  File "C:\Users\yuto1\Anaconda3\envs\MyDjango\lib\threading.py", line 932, in _bootstrap_inner
    self.run()
  File "C:\Users\yuto1\Anaconda3\envs\MyDjango\lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\yuto1\Anaconda3\envs\MyDjango\lib\site-packages\django\utils\autoreload.py", line 53, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\yuto1\Anaconda3\envs\MyDjango\lib\site-packages\django\core\management\commands\runserver.py", line 117, in inner_run
    self.check(display_num_errors=True)
  File "C:\Users\yuto1\Anaconda3\envs\MyDjango\lib\site-packages\django\core\management\base.py", line 392, in check
    all_issues = self._run_checks(
  File "C:\Users\yuto1\Anaconda3\envs\MyDjango\lib\site-packages\django\core\management\base.py", line 382, in _run_checks
    return checks.run_checks(**kwargs)
  File "C:\Users\yuto1\Anaconda3\envs\MyDjango\lib\site-packages\django\core\checks\registry.py", line 72, in run_checks
    new_errors = check(app_configs=app_configs)
  File "C:\Users\yuto1\Anaconda3\envs\MyDjango\lib\site-packages\django\core\checks\urls.py", line 101, in check_url_settings
    if value and not value.endswith('/'):
AttributeError: 'list' object has no attribute 'endswith'"