在博客页面中,我为每个用户制作了个人资料页面链接。还有一个用户列表视图,用户链接到他们自己的个人资料页面。
这是我的博客/models.py:
class Post(models.Model):
title = models.CharField(max_length=255)
body=RichTextField(blank=True,null=True)
image=models.FileField(upload_to="mediaphoto",validators=[validate_file_size])
topImage=ImageSpecField(source='image',processors=[ResizeToFill(750,300)],format='PNG',options={'quality':60})
date = models.DateTimeField(auto_now_add=True)
author = models.ForeignKey(User,on_delete=models.CASCADE)
category=models.ForeignKey(Category,on_delete=models.CASCADE)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('post_detail', args=[str(self.id)])
博客/views.py:
class CustomContentMixin:
def get_context_data(self, **kwargs):
context = super(CustomContentMixin, self).get_context_data(**kwargs)
context['user_list']= Profile.objects.all()
context['category_list']=Category.objects.all()
return context
class BlogappListView(CustomContentMixin,ListView):
model = Category,Profile
template_name = 'home.html'
context_object_name='post_list'
queryset=Post.objects.all()
paginate_by=7
class BlogappUpdateView(CustomContentMixin,UpdateView,LoginRequiredMixin,UserPassesTestMixin):
model=Post
template_name='post_edit.html'
fields=('title','body','image','category')
login_url='login'
def test_func(self):
obj = self.get_object()
return obj.author == self.request.user
class BlogappDetailView(DetailView,LoginRequiredMixin,FormMixin):
model=Post
template_name='post_detail.html'
login_url='login'
form_class=CommentForm
def get_success_url(self):
return reverse_lazy('post_detail', kwargs={'pk': self.object.pk})
def get_context_data(self, **kwargs):
context = super(BlogappDetailView, self).get_context_data(**kwargs)
context['comments']=Comment.objects.filter(post=self.object)
context['comment_form']=CommentForm()
context['user_list']= Profile.objects.all()
context['category_list']=Category.objects.all()
return context
def post(self, request, *args, **kwargs):
self.object = self.get_object()
comment_form = self.get_form()
if comment_form.is_valid():
return self.form_valid(comment_form)
else:
return self.form_invalid(comment_form)
def form_valid(self, comment_form):
comment_form.instance.post = self.object
comment_form.instance.author=self.request.user
comment_form.save()
return super().form_valid(comment_form)
class BlogappDeleteView(CustomContentMixin,DeleteView,LoginRequiredMixin,UserPassesTestMixin):
model=Post
template_name='post_delete.html'
success_url=reverse_lazy('home')
login_url='login'
def test_func(self):
obj = self.get_object()
return obj.author == self.request.user
class BlogappCreateView(CustomContentMixin,CreateView,LoginRequiredMixin):
model=Post
template_name='post_new.html'
login_url='login'
fields=('title','body','image','category')
def form_valid(self, form):
form.instance.author = self.request.user
return super().form_valid(form)
帐户/模型.py:
class Profile(models.Model):
user = models.OneToOneField(User,on_delete=models.CASCADE,primary_key=True)
name=models.CharField(max_length=30, blank=True)
bio = models.TextField(max_length=500, blank=True)
location = models.CharField(max_length=30, blank=True)
birthdate = models.DateField(null=True, blank=True)
def __str__(self):
return self.user.username
@receiver(post_save, sender=User)
def user_is_created(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
else:
instance.profile.save()
帐户/views.py:
class ProfileDetailView(DetailView):
template_name='profile.html'
model=Profile
def get_user_profile(self,pk):
return get_object_or_404(Profile,pk=pk)
class ProfileEditView(UpdateView):
model=Profile
template_name='profile_edit.html'
form_class=ProfileForm
success_url = reverse_lazy('profile')
def get_user_profile_edit(self,pk):
return get_object_or_404(Profile,pk=pk)
帐户/urls.py:
urlpatterns = [
.............
path('profile/<int:pk>/', ProfileDetailView.as_view(), name='profile'),
path('profile/<int:pk>/edit', ProfileEditView.as_view(), name='profile_edit'),
]
现在对于 base.html,这是我的用户列表部分,它只显示链接到他们的个人资料页面链接的用户列表:
{% for user in user_list %}
<ul>
<a class="user" href="{% url 'profile' user.pk %}" >{{ user }}</a>
</ul>
{% endfor %}
现在对于 home.html ,其中显示了帖子列表及其作者链接:
{% block content %}
{% for post in post_list %}
<!-- Blog Post -->
<div class="card mb-4">
<img class="card-img-top" src="{{post.topImage.url}}" alt="Card image cap">
<div class="card-body">
<h2 class="card-title">{{post.title}}</h2>
<p class="card-text">{{post.body|truncatechars:400|safe}}</p>
<a href="{% url 'post_detail' post.pk %}" class="btn btn-primary">Read More →</a>
</div>
<div class="card-footer text-muted">
Posted on {{post.date|safe}} by
<a href="{% url 'profile' post.pk %}" >{{post.author}}</a>
in <a href="{% url 'category_list' post.category.pk %}">{{post.category}}</a>
</div>
</div>
{% endfor %}
{% endblock content %}
这是profile.html:
{% block content %}
<p>Name: {{ object.name }} .</p>
<p>Bio: {{ object.bio }} .</p>
<p>Location: {{ object.location }} .</p>
<p>Date of birth: {{ object.birthdate }} .</p>
<p>Gender: {{ object.gender }} .</p>
<p><a class="btn btn-warning" href="{% url 'home' %}">homepage</a></p>
<p><a class="btn btn-warning" href="{% url 'profile_edit' object.pk %}">Edit Profile</a></p>
{% endblock %}
主要问题是当点击帖子列表中的每个作者时,它会将我重定向到 http://localhost:8000/profile/3/、http://localhost:8000/profile/4/、http://localhost: 8000/profile/5/ 等等,这意味着当我添加新帖子时,它只会增加配置文件的 pk 数量。但配置文件 pk 编号应根据他们的配置文件 url 固定。并且个人资料页面将我带到以下错误,因为与 pk 相关的个人资料页面不会退出:
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/profile/4/
Raised by: accounts.views.ProfileDetailView
No profile found matching the query
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
我的错误是什么?我该如何解决这个问题,以便我可以显示与帖子作者相关的个人资料页面?
如果您需要更多信息,请告诉我。