无法映射 Django URL
Trouble mapping Django URLs
我已经完成了 Django 教程的所有部分,现在开始我自己的项目来练习。我回到了开始教程 it talks about views/mapping urls. I also am following this tutorial for trying to display a table
无论出于何种原因,我无法弄清楚为什么当我尝试点击 http://127.0.0.1:8000/show/ 时,它 returns 404。过去一个小时我一直盯着这个看,一直在回头看在教程和我的代码之间。我不得不做一些与第二个提到的教程不同的事情,主要是他们没有谈论创建应用程序级 urls.py 文件。到目前为止一切都很好。 models.py 文件在 MySQL 数据库中创建了 table,正如我在 workbench.
中看到的那样
我的项目结构是这样的:
- 我的网站(项目)
- 显示数据(应用程序)
这是我的项目级别 urls.py 文件,位于 mywebsite 文件夹中:
from django.contrib import admin
from django.urls import include,path
urlpatterns = [
path('admin/', admin.site.urls),
path('displaydata/', include('displaydata.urls'))
]
这是我的应用级 urls.py 文件,位于 displaydata 文件夹中:
from django.urls import path
from . import views
app_name = 'displaydata'
urlpatterns = [
path('', views.show, name='show')
]
这是我的显示数据 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>
它将点击 URL 127.0.0.1:8000<b>/displaydata/</b>
的 show
视图.
之所以如此,是因为您包含了所有带有 displaydata/
前缀的 displaydata
url。在 displaydata
应用程序的 url 模式中,有一种模式:空字符串,因此它将与路径 /dispaydata
.
相匹配
如果想用/show
访问视图,可以在项目中使用空字符串作为前缀 urls:
from django.contrib import admin
from django.urls import include,path
urlpatterns = [
path('admin/', admin.site.urls),
path(<b>''</b>, include('displaydata.urls'))
]
然后 displaydata
url 与:
一起工作
from django.urls import path
from . import views
app_name = 'displaydata'
urlpatterns = [
path(<b>'/show/'</b>, 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,<b>'<i>app_name</i>/show.html'</b>,{'shipment': shipments})
我已经完成了 Django 教程的所有部分,现在开始我自己的项目来练习。我回到了开始教程 it talks about views/mapping urls. I also am following this tutorial for trying to display a table
无论出于何种原因,我无法弄清楚为什么当我尝试点击 http://127.0.0.1:8000/show/ 时,它 returns 404。过去一个小时我一直盯着这个看,一直在回头看在教程和我的代码之间。我不得不做一些与第二个提到的教程不同的事情,主要是他们没有谈论创建应用程序级 urls.py 文件。到目前为止一切都很好。 models.py 文件在 MySQL 数据库中创建了 table,正如我在 workbench.
中看到的那样我的项目结构是这样的:
- 我的网站(项目)
- 显示数据(应用程序)
这是我的项目级别 urls.py 文件,位于 mywebsite 文件夹中:
from django.contrib import admin
from django.urls import include,path
urlpatterns = [
path('admin/', admin.site.urls),
path('displaydata/', include('displaydata.urls'))
]
这是我的应用级 urls.py 文件,位于 displaydata 文件夹中:
from django.urls import path
from . import views
app_name = 'displaydata'
urlpatterns = [
path('', views.show, name='show')
]
这是我的显示数据 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>
它将点击 URL 127.0.0.1:8000<b>/displaydata/</b>
的 show
视图.
之所以如此,是因为您包含了所有带有 displaydata/
前缀的 displaydata
url。在 displaydata
应用程序的 url 模式中,有一种模式:空字符串,因此它将与路径 /dispaydata
.
如果想用/show
访问视图,可以在项目中使用空字符串作为前缀 urls:
from django.contrib import admin
from django.urls import include,path
urlpatterns = [
path('admin/', admin.site.urls),
path(<b>''</b>, include('displaydata.urls'))
]
然后 displaydata
url 与:
from django.urls import path
from . import views
app_name = 'displaydata'
urlpatterns = [
path(<b>'/show/'</b>, 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,<b>'<i>app_name</i>/show.html'</b>,{'shipment': shipments})