Django 查询:如何通过帐户所有者创建的博客 post 查询帐户页面?又名 link 从一个到另一个
Django Querying: How do i query an account page through a blog post that the account owner has created? aka link from one to another
Django 查询:如何通过帐户所有者创建的博客 post 查询帐户页面?又名 link 从一个到另一个。所以我有一个帐户模型和一个博客 post 模型,作者是外键。我不确定如何查询帐户。谁能建议这通常是如何完成的?因为我尝试执行 user_id=request.user.id
但这会将我带到我自己的帐户视图。谢谢!
html
<a class="dropdown-item" href="{% url 'account:view' %}">{{blog_post.author}}</a>
views.py
def detail_blog_view(request, slug):
context = {}
blog_post = get_object_or_404(BlogPost, slug=slug)
context['blog_post'] = blog_post
return render(request, 'HomeFeed/detail_blog.html', context)
urls.py 用于博客
path('<slug>/detail/', detail_blog_view, name= "detail"),
urls.py 帐户:
path('<user_id>/', account_view, name="view"),
models.py
class Account(AbstractBaseUser):
email = models.EmailField(verbose_name="email", max_length=60, unique=True)
username = models.CharField(max_length=30, unique=True)
class BlogPost(models.Model):
title = models.CharField(max_length=50, null=False, blank=False)
body = models.TextField(max_length=5000, null=False, blank=False)
slug = models.SlugField(blank=True, unique=True)
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
你真的不需要查询author
,你所做的只是从BlogPost
中获取foreign key
值像这样的对象:
views.py
def post(request, pk):
post = BlogPost.objects.get(pk=pk)
author = post.author
return render(request, 'post.html', {'post': post,
'author': author})
然后在模板中显示:
post.html
<a href="{% url 'view' author.pk %}">{{ author }}</a>
(or you can use {{ post.author }} here aswell) says:
{{ post.title }} | {{ post.body }}
编辑
在您拥有用户配置文件路径的网址中,您实际上需要指定参数类型,例如:
path('<int:user_id>/', account_view, name="view"), # btw dont call your views 'view', name should describe what the view actually does or shows
Django 查询:如何通过帐户所有者创建的博客 post 查询帐户页面?又名 link 从一个到另一个。所以我有一个帐户模型和一个博客 post 模型,作者是外键。我不确定如何查询帐户。谁能建议这通常是如何完成的?因为我尝试执行 user_id=request.user.id
但这会将我带到我自己的帐户视图。谢谢!
html
<a class="dropdown-item" href="{% url 'account:view' %}">{{blog_post.author}}</a>
views.py
def detail_blog_view(request, slug):
context = {}
blog_post = get_object_or_404(BlogPost, slug=slug)
context['blog_post'] = blog_post
return render(request, 'HomeFeed/detail_blog.html', context)
urls.py 用于博客
path('<slug>/detail/', detail_blog_view, name= "detail"),
urls.py 帐户:
path('<user_id>/', account_view, name="view"),
models.py
class Account(AbstractBaseUser):
email = models.EmailField(verbose_name="email", max_length=60, unique=True)
username = models.CharField(max_length=30, unique=True)
class BlogPost(models.Model):
title = models.CharField(max_length=50, null=False, blank=False)
body = models.TextField(max_length=5000, null=False, blank=False)
slug = models.SlugField(blank=True, unique=True)
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
你真的不需要查询author
,你所做的只是从BlogPost
中获取foreign key
值像这样的对象:
views.py
def post(request, pk):
post = BlogPost.objects.get(pk=pk)
author = post.author
return render(request, 'post.html', {'post': post,
'author': author})
然后在模板中显示:
post.html
<a href="{% url 'view' author.pk %}">{{ author }}</a>
(or you can use {{ post.author }} here aswell) says:
{{ post.title }} | {{ post.body }}
编辑
在您拥有用户配置文件路径的网址中,您实际上需要指定参数类型,例如:
path('<int:user_id>/', account_view, name="view"), # btw dont call your views 'view', name should describe what the view actually does or shows