在上一个问题this中,我问过有关在另一个Django应用程序中使用数据的问题。我想在页面应用程序中使用博客应用程序中的博客数据。目的是使我的网站主页显示3个最新条目。
当时answer provided是可以接受的。但是,自那时以来,我想现在将设置在主页上的博客项设置为指向博客项本身的链接,但是主页上的博客项具有不同的url路径。有什么办法解决吗?
这是我的代码:
博客
from .views import (
BlogListView,
BlogUpdateView,
BlogDetailView,
BlogDeleteView,
BlogCreateView,
)
urlpatterns = [
path('<int:pk>/edit/',
BlogUpdateView.as_view(), name='Blog_edit'),
path('<int:pk>/',
BlogDetailView.as_view(), name='Blog_detail'),
path('<int:pk>/delete/',
BlogDeleteView.as_view(), name='Blog_delete'),
path('new/', BlogCreateView.as_view(), name='Blog_new'),
path('', BlogListView.as_view(), name='Blog_list'),
]
models.py
class Blog(models.Model):
title = models.CharField(max_length=255)
body = models.TextField()
date = models.DateTimeField(auto_now_add=True)
author = models.ForeignKey(
get_user_model(),
on_delete=models.CASCADE,
)
thumb = models.ImageField(blank=True, null=True)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('Blog_detail', args=[str(self.id)])
views.py
class BlogListView(ListView):
model = Blog
template_name = 'blog_list.html'
class BlogDetailView(DetailView):
model = Blog
template_name = 'blog_detail.html'
login_url = 'login'
class BlogUpdateView(LoginRequiredMixin, UpdateView):
model = Blog
fields = ('title', 'body', 'thumb')
template_name = 'blog_edit.html'
login_url = 'login'
def test_func(self):
obj = self.get_object()
return obj.author == self.request.user
class BlogDeleteView(LoginRequiredMixin, DeleteView):
model = Blog
template_name = 'blog_delete.html'
success_url = reverse_lazy('blog_list')
login_url = 'login'
def test_func(self):
obj = self.get_object()
return obj.author == self.request.user
class BlogCreateView(LoginRequiredMixin, CreateView):
model = Blog
template_name = 'blog_new.html'
fields = ('title', 'body', 'thumb')
login_url = 'login'
def form_valid(self, form):
form.instance.author = self.request.user
return super().form_valid(form)
页面
urls.py
urlpatterns = [
path('', HomePageView, name='home'),
]
views.py
def HomePageView(request):
context = {}
blog = Blog.objects.order_by('-date')[:3]
context['blog']=blog
return render(request,'home.html',context)
home.html
{% extends 'base.html' %}
{% block title %}Home{% endblock title %}
{% block content %}
<div class="jumbotron">
<h1 class="display-4">Lakeland Cycle Club</h1>
<p class="lead">The home of cycling in Fermanagh.</p>
<p class="lead">
<a class="btn btn-primary btn-lg" href="{% url 'blog_list' %}" role="button">View All Club News</a>
</p>
{% for new in blog%}
<div class="card" style="width: 300px; display: inline-block; margin: 5px; vertical-align: top;">
<div class="card-header">
<span class="font-weight-bold">
{{ new.title }}
</span> ·
<span class="text-muted">by {{ new.author }} | {{ new.date }}</span>
</div>
<div class="card-body">
{% if blog.thumb %}
<p align="center"><img src="{{ new.thumb.url }}" /></p>
{% endif %}
<p>{{ new.body | linebreaks | truncatewords:30 }}
</div>
</div>
{% endfor %}
{% endblock content %}
更多信息。
我认为,如果我在标题({{ new.title }}
)中添加URL,它可能会按我的意愿工作。我尝试了<a href="{% url 'blog_detail' %}">{{ new.title }}</a>
,但收到此错误:
django.urls.exceptions.NoReverseMatch: Reverse for 'blog_detail' with no arguments not found. 1 pattern(s) tried: ['blog/(?P<pk>[0-9]+)/$']
原因很可能是因为我主页中的每个项目都无法识别特定博客条目的主键。如果我将URL更改为blog_list
,则不需要主键,因此我被带到blog_lost页面,没有任何问题。如何添加URL链接并确保其链接到正确的blog_detail
帖子?
答案 0 :(得分:0)
我进行了以下更改:
templates / home.html
我改变了
<span class="font-weight-bold">
{{ new.title }}
</span> ·
为
<span class="font-weight-bold">
<a href="{% url 'pages_blog_detail' new.pk %}">{{ new.title }}</a>
</span> ·
pages / urls.py
我添加了
from blog.views import BlogDetailView
并更改
urlpatterns = [
path('', HomePageView, name='home'),
]
到
urlpatterns = [
path('blog/<int:pk>/',
BlogDetailView.as_view(), name='pages_blog_detail'),
path('', HomePageView, name='home'),
]