【发布时间】:2019-04-05 19:31:03
【问题描述】:
在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 %}
更多信息。
我想如果我在标题中添加一个 URL ({{ new.title }}),它可能会按我的意愿工作。我试过<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 帖子?
【问题讨论】:
标签: python django python-3.x django-views django-apps