设法在另一个 Django 应用程序中使用来自一个 Django 应用程序的数据,但 URL 不同
Managing to use data from one Django application in another but the urls are different
在 上一个问题中,我询问了有关在另一个 Django 应用程序中使用数据的问题。我想在我的页面应用程序中使用我的博客应用程序中的博客数据。目标是我的站点主页将显示 3 个最新条目。
当时是可以接受的。但是,我现在想将出现在我主页上的博客项目设置为充当博客项目本身的 link,但我主页上的博客项目具有不同的 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 link 并确保它 link 到正确的 blog_detail
post?
我做了以下修改:
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'),
]
在
当时
这是我的代码:
博客
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 link 并确保它 link 到正确的 blog_detail
post?
我做了以下修改:
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'),
]