【问题标题】:Trouble mapping Django URLs映射 Django URL 时遇到问题
【发布时间】:2021-09-06 11:07:17
【问题描述】:

我已经完成了 Django 教程的所有部分,现在已经开始我自己的项目来练习。我回到了it talks about views/mapping urls 的开始教程。我也在关注this tutorial for trying to display a table

无论出于何种原因,我无法弄清楚为什么当我尝试点击http://127.0.0.1:8000/show/ 时,它会返回 404。我在过去的一个小时里一直在盯着这个,并且一直在教程和我的代码之间来回切换。我必须做的事情与第二个提到的教程有些不同,主要是他们没有谈论创建应用程序级 urls.py 文件。到目前为止,一切都运行良好。 models.py 文件在 MySQL 数据库中创建了表,正如我在工作台中看到的那样。

我的项目结构是这样的:

  • 我的网站(项目)
  • 显示数据(应用)

这是位于 mywebsite 文件夹中的项目级 urls.py 文件:

from django.contrib import admin
from django.urls import include,path

urlpatterns = [
    path('admin/', admin.site.urls),
    path('displaydata/', include('displaydata.urls'))
]

这是位于 displaydata 文件夹中的应用级 urls.py 文件:

from django.urls import path
from . import views
app_name = 'displaydata'

urlpatterns = [
    path('', views.show, name='show')
]

这是我的 displaydata views.py 文件:

from django.shortcuts import render,redirect
from django.http import HttpResponse
from .models import Shipment

# Create your views here.

def show(request):
    shipments = Shipment.objects.all()
    return HttpResponse(render(request,"show.html",{'shipment':shipments}))

这里是 show.html 文件:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Django CRUD Operations</title>
  <meta charset="utf-8">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script> 
</head>
<body>
<div class="container">
<table class="table table-striped">
    <thead>
      <tr>
        <th>Shipment ID</th>
        <th>Driver</th>
        <th>Destination City</th>
        <th>Destination State</th>
      </tr>
    </thead>
    <tbody>
    {% for ship in shipment %}  
      <tr>
        <td>{{ship.id}}</td>
        <td>{{ship.driver}}</td>
        <td>{{ship.destination_city}}</td>
        <td>{{ship.destination_state}}</td>
      </tr>
      {% endfor %} 
    </tbody>
</table>    
</div>
</body>
</html>

【问题讨论】:

    标签: python django django-urls


    【解决方案1】:

    它将点击 URL 127.0.0.1:8000<b>/displaydata/</b>show 视图。

    出现这种情况是因为您包含所有带有 displaydata/ 前缀的 displaydata 网址。在 displaydata 应用程序的 url 模式中,有一个模式:空字符串,因此它将与路径 /dispaydata 匹配。

    如果你想用/show访问视图,你可以在项目url中使用一个空字符串作为前缀:

    from django.contrib import admin
    from django.urls import include,path
    
    urlpatterns = [
        path('admin/', admin.site.urls),
        path('', include('displaydata.urls'))
    ]

    然后对于displaydata url 使用:

    from django.urls import path
    from . import views
    app_name = 'displaydata'
    
    urlpatterns = [
        path('/show/', views.show, name='show')
    ]

    如果模板位于<i>app_name</i>/templates/<i>app_name</i>/show.html,那么你渲染模板:

    def show(request):
        shipments = Shipment.objects.all()
        return render(request,'app_name/show.html',{'shipment': shipments})

    【讨论】:

    • 谢谢!我尝试了这个并遇到了这个问题:“/..show.html(源不存在)”但是,经过仔细检查,它看起来像是在寻找 html 模板的上一个目录。我最初有它在 mywebsite/displaydata/templates/displaydata/show.html 但它想要 mywebsite/displaydata/templates/show.html 你知道为什么官方 django 教程要求嵌套它(并且它在那个教程中工作)但是在这个实例是在查找一个目录吗?有什么地方需要我指定吗?
    • @krisbkreeme:你不能访问模板,所以/..show.html 没有多大意义。您触发以 HTTP 响应响应的视图,对于该响应,视图可以呈现零个、一个或多个模板,但模板因此更多地是生成 HTTP 响应的工具。
    • @krisbkreeme:你能用模板的内容编辑问题吗?
    • 如果它位于displaydata/templates/displaydata/show.html,您应该使用render(request, 'displaydata/show.html', {'shipment':shipments})进行渲染。
    • @krisbkreeme:确实,所以你可以通过'displaydata/show.html'访问它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-02
    • 2014-05-07
    • 1970-01-01
    • 1970-01-01
    • 2019-11-26
    • 1970-01-01
    • 2021-01-04
    相关资源
    最近更新 更多