[英]Trouble mapping Django URLs
I've completed all of the sections of the Django tutorial and have started my own project now to practice.我已经完成了 Django 教程的所有部分,现在已经开始了我自己的项目来练习。 I am back at the beginning tutorial where it talks about views/mapping urls .
我回到开始教程,它讨论了 views/mapping urls 。 I also am following this tutorial for trying to display a table
我也在关注本教程以尝试显示表格
For whatever reason, I cannot figure out why when I try to hit http://127.0.0.1:8000/show/ , it returns 404. I've been staring at this for the last hour and have been going back and forth between the tutorial and my code.无论出于何种原因,我无法弄清楚为什么当我尝试点击http://127.0.0.1:8000/show/ 时,它返回 404。我一直盯着这个看最后一个小时,并且一直在这之间来回走动教程和我的代码。 I had to do things a little bit differently than the 2nd mentioned tutorial, mainly that they didn't talk about creating an app-level urls.py file.
我不得不做的事情与第二个提到的教程略有不同,主要是他们没有谈论创建应用程序级别的 urls.py 文件。 Everything up to this point has worked fine.
到目前为止,一切都运行良好。 The models.py file created the table within the MySQL database, as I can see it in the workbench.
models.py 文件在 MySQL 数据库中创建了表,正如我在工作台中看到的那样。
My project structure is like this:我的项目结构是这样的:
Here is my project level urls.py file located in the mywebsite folder:这是位于 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'))
]
Here is my app-level urls.py file located in the displaydata folder:这是位于 displaydata 文件夹中的我的应用程序级 urls.py 文件:
from django.urls import path
from . import views
app_name = 'displaydata'
urlpatterns = [
path('', views.show, name='show')
]
Here is my displaydata views.py file:这是我的 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}))
Here is the show.html file:这是 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>
It will hit the show
view for the URL 127.0.0.1:8000
./displaydata/
它将点击 URL
127.0.0.1:8000 /displaydata/
的show
视图。
This the case because you include all the displaydata
urls with the displaydata/
prefix.这是因为您包含所有带有
displaydata/
前缀的displaydata
url。 In the url patterns of your displaydata
app, there is one pattern: the empty string, so it will match this for the path /dispaydata
.在您的
displaydata
应用程序的 url 模式中,有一个模式:空字符串,因此它将与路径/dispaydata
。
If you want to access the view with /show
, you can use an empty string as prefix in the project urls:如果要使用
/show
访问视图,可以在项目 url 中使用空字符串作为前缀:
from django.contrib import admin
from django.urls import include,path
urlpatterns = [
path('admin/', admin.site.urls),
path(
'', include('displaydata.urls'))
]
and then for the displaydata
urls work with:然后对于
displaydata
url 使用:
from django.urls import path
from . import views
app_name = 'displaydata'
urlpatterns = [
path(
'/show/', views.show, name='show')
]
If the template is located at app_name /templates/ app_name /show.html
, then you render the template with:如果模板位于
app_name /templates/ app_name /show.html
,则使用以下命令渲染模板:
def show(request):
shipments = Shipment.objects.all()
return render(request,
'app_name/show.html',{'shipment': shipments})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.