我对Django和python还是很陌生,在我的应用中,我有两个模型,一个是-moz-element()
,另一个是MyProfile
,用户将拥有个人资料,用户可以创建帖子,它是所有工作,但我想显示用户在其个人资料中创建的帖子。为此,我尝试在通用MyPost
内创建一个get_context_data
。但这给了我这个错误Detailview
。 ahmy是我登录的用户名。
我的模型
Cannot query "ahmy": Must be "MyProfile" instance
我的视图 @method_decorator(需要登录,名称=“ dispatch”)
from django.db import models
from django.contrib.auth.models import User
from django.db.models.deletion import CASCADE
from django.core.validators import MinValueValidator, RegexValidator
# Create your models here.
class MyProfile(models.Model):
name = models.CharField(max_length = 500)
user = models.OneToOneField(to=User, on_delete=CASCADE)
address = models.TextField(null=True, blank=True)
gender = models.CharField(max_length=20, default="Male", choices=(("Male", 'Male'), ("Female", "Female"), ("LGBTQ", "LGBTQ")))
phone_no = models.CharField(validators=[RegexValidator("^0?[5-9]{1}\d{9}$")], max_length=15, null=True, blank=True)
description = models.CharField(max_length = 240, null=True, blank=True)
pic = models.ImageField(upload_to = "image\\", null=True)
def __str__(self):
return "%s" % self.user
class MyPost(models.Model):
main_pic = models.ImageField(upload_to = "image\\", null=True)
amount_spend = models.IntegerField(null=True, blank=True)
total_donars = models.IntegerField(null=True, blank=True)
title = models.CharField(max_length = 200)
body = models.TextField(null=False, blank=False)
cr_date = models.DateTimeField(auto_now_add=True)
uploaded_by = models.ForeignKey(to=MyProfile, on_delete=CASCADE, null=True, blank=True)
def __str__(self):
return "%s" % self.title
我的HTML文件
class MyProfileDetailView(DetailView):
model = MyProfile
def get_context_data(self, **kwargs):
# Call the base implementation first to get a context
context = super().get_context_data(**kwargs)
# Add in a QuerySet of all the user posts
user_posts = MyPost.objects.filter(uploaded_by=self.request.user).order_by('-cr_date')
context['user_posts'] = user_posts
context['user'] = self.request.user
return context
URLS
{% extends 'base.html' %}
{% block content %}
<div class="p-5">
<img src="/media/{{myprofile.pic}}" />
<h1 class="myhead2">{{myprofile.name}}</h1>
<p><strong>Address: {{myprofile.address}}</strong></p>
<p><strong>Phone Number: {{myprofile.phone_no}}</strong></p>
<p><strong>Email: {{myprofile.user.email}}</strong></p>
<p><strong>About:</strong> {{myprofile.purpose}}</p>
<p><strong> Total Donation Recived: {{myprofile.donation_recived}}</strong></p>
<hr>
<table class="table my-3">
<thead class="thead-dark">
<tr>
<th>Title</th>
<th>Date</th>
<th>Action</th>
</tr>
</thead>
{% for MyPost in user_posts %}
<tr>
<td>{{MyPost.title}}</td>
<td>{{MyPost.cr_date | date:"d/m/y"}}</td>
<td>
<a class="btn btn-dark btn-sm" href='/covid/mypost/{{n1.id}}'>Read More</a>
<a class="btn btn-dark btn-sm" href='/covid/mypost/delete/{{n1.id}}'>Delete</a>
</td>
</tr>
{% endfor %}
</table>
</div>
{% endblock %}
答案 0 :(得分:1)
uploaded_by
字段引用的是MyProfile
模型,而不是User
模型。您可以将查询更改为:
user_posts = MyPost.objects.filter(
uploaded_by__user=self.request.user
).order_by('-cr_date')
因此,通过使用双下划线(__
),我们可以“遍历”一个关系,因此,我们寻找MyPost
是uploaded_by
且{ MyProfile
对user
对象的引用。
如果要在路径中显示request.user
,请显示用户的内容:
pk
您可以将其替换为:
path('myprofile/<int:pk>', views.MyProfileDetailView.as_view()),
假设user_posts = MyPost.objects.filter(
uploaded_by_id=self.kwargs['pk']
).order_by('-cr_date')
是 profile id;或:
pk
如果user_posts = MyPost.objects.filter(
uploaded_by__user_id=self.kwargs['pk']
).order_by('-cr_date')
引用了 user id。
或者您可以使用pk
:
self.object