如果我的网址是“someurl / report / 1 /”,其中“report”是一个应用程序而“1”对应于我的SQL数据库中模型的某个id,我将如何替换具有特定模型数据的模板?
就我而言,我正在写一个网站,显示不同海滩的冲浪报告。我已经设置好了,所以每个SQL模型ID都对应一个不同的海滩。因此,如果我想在模板中使用海滩“3”的数据,我将如何在html模板中显示这些数据?
TRACEBACK
使用surfsite.urls中定义的URLconf,Django按以下顺序尝试了这些URL模式: ^管理员/ ^ report / ^ $ [name ='index'] ^ report /(?P [0-9] +)$ [name ='get_report'] 当前网址report / 1 /与其中任何一个都不匹配。
#URLS.PY
from django.conf.urls import url
from django.conf.urls.static import static
from . import views
urlpatterns = [
# /index/
url(r'^$', views.index, name='index'),
# /report/
url(r'(?P<beach_id>[0-9]+)$', views.get_report, name='get_report'),
]
#MY TEMPLATE
<!DOCTYPE html>
<html>
{% load staticfiles %}
<link rel="stylesheet" type="text/css"
href="{% static 'report/css/style.css' %}"/>
<link href="https://fonts.googleapis.com/css?family=Biryani:300,400,800"
rel="stylesheet"/>
<head>
<title>REALSURF</title>
</head>
<body>
<h1>
<form id="search">
<input id="search" type="text">
</form>
</h1>
<div class="container">
<div class="column">
<div class="text">Wind</div>
<div class="number">{{Break.wind}}</div>
</div>
<div class="column">
<div class="text">Wave Height</div>
<div class="number">{{Break.low}}-{{Break.high}}</div>
</div>
<div class="column">
<div class="text">Tide</div>
<div class="number">{{Break.tide}}</div>
</div>
</div>
<h2>
REALSURF
</h2>
<h3>
A simple site by David Owens
</h3>
</body>
</html>
#MY MODEL
from __future__ import unicode_literals
from django.db import models
class Beach(models.Model):
name = models.CharField(max_length=50)
high = models.SmallIntegerField()
low = models.SmallIntegerField()
wind = models.SmallIntegerField()
tide = models.DecimalField(max_digits=3, decimal_places=2)
def __str__(self):
return self.name
#MY VIEWS
from django.http import Http404
from django.shortcuts import render
from .models import Beach
def index(request):
allBeaches = Beach.objects.all()
context = {
'allBeaches': allBeaches,
}
return render(request, 'report/index.html', context)
def get_report(request, id):
try:
beach = Beach.objects.get(id=id)
except Beach.DoesNotExist:
raise Htpp404("404")
return render(request, 'report/index.html', {'beach': beach})
答案 0 :(得分:1)
如果我理解你,你想要创建视图,分别渲染海滩 你可以做这样的事情,你的观点:
def get_beach(request, id)
beach = Beach.objects.get(id=id)
return render(request, 'path/to/your/template', {'beach':beach})
网址:
url(r'^someurl/report/(?P<id>[0-9]+)$', views.get_beach(), name='get_beach'),
此页面的模板网址:
<a href= "someurl/report/{{beach.id}}">beach</a>
<强>被修改强>
根据我的理解,这是您的视图,这是详细视图,但选择所有对象(Break.objects.all()
)
def detail(request, break_id):
try:
allBreaks = Break.objects.all()
except Break.DoesNotExist:
raise Http404("404")
return render(request, 'report/index.html', {'allBreaks': allBreaks})
所以你必须改变这个:
def detail(request, break_id):
try:
break_detail = Break.objects.get(id=break_id)
return render(request, 'path/to/your/template', {'break_detail':break_detail})
except Break.DoesNotExist:
raise Http404("404")
然后你的网址应如下所示:
url(r'^someurl/report/(?P<break_id>[0-9]+)$', views.detail(), name='detail'),
或者您可以使用get_object_or_404
:
def detail(request,break_id): break_detail = get_object_or_404(Break,id = break_id) return render(request,&#39; path / to / your / template&#39;,{&#39; break_detail&#39;:break_detail})
网址是一样的。
因此,如果你想访问风场,你只需写下这个tempalte标签{{break_detail.wind}}
<强> UPD2 强>
从此处更改地点
^admin/
^report/ ^$ [name='index']
^report/ (?P<beach_id>[0-9]+)$ [name='get_report']
到此:
^admin/
^report/(?P<beach_id>[0-9]+)$ [name='get_report']
^report/^$ [name='index']
并在report/