嘿,DetailViewı试图将用户的帖子添加到细节页面,但是ı不能添加,这是ı编写的代码
profile.py
from django.shortcuts import get_object_or_404
from django.views.generic import DetailView
from account.models import CustomUserModel
from django.shortcuts import get_object_or_404
class ProfilDetailView(DetailView):
template_name = 'pages/profil.html'
context_object_name='profil'
def get(self,request):
profil = get_object_or_404(
CustomUserModel,username=self.kwargs.get('username')
)
yazi = get_object_or_404(YazilarModel,yazar=profil.username)
return render(request,'pages/profil.html',context={
'profil':profil,
'yazilar':yazi.all()
})urls.py
path('kullanıcı/<str:username>',ProfilDetailView.as_view(),name='profil'),profil.html
{% extends 'base.html'%}
{% load static %}
{% block title %} {{profil.username}} {% endblock %}
{% block content %}
<div class="card mb-3">
<div class="row g-0">
<div class="col-md-4">
{% if profil.avatar %}
<img src="{{profil.avatar.url}}" class="rounded" class="pt-4" width="200px" height="200px "
style="border: radius 15%; margin-right:20px; " alt="">
{% else %}
<img src="{% static 'img/no-avatar.jpg'%}" class="rounded" class="pt-4" width="200px" height="200px "
style="border: radius 15%; margin-right:20px; " alt="">
{% endif %}
</div>
<div class="col-md-8">
<div class="card-body">
{% if profil.get_full_name == '' %}
<h5 class="card-title"> Adı : Girilmemiş</h5>
<h5 class="card-title"> Soyadı : Girilmemiş</h5>
{% else %}
<h5 class="card-title"> Adı : {{profil.first_name}}</h5>
<h5 class="card-title mb-5"> Soyadı : {{profil.last_name}}</h5>
{% endif %}
<h5>Hakkında :</h5>
<hr>
<p>Bla Bla Bla</p>
<p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p>
</div>
</div>
</div>
</div>
<hr>
{%endblock%}yazilar.py in models
from distutils.command.upload import upload
from enum import unique
from autoslug import AutoSlugField
from django.db import models
from forum.models import KategoriModel
from django.contrib.auth.models import User
from ckeditor.fields import RichTextField
from forum.abstract_models import DateAbstractModel
from django_resized import ResizedImageField
class YazilarModel(DateAbstractModel):
resim = ResizedImageField(verbose_name='image',size=[320,214.34],upload_to='yazi_resimler')
baslik = models.CharField(max_length=50)
icerik = RichTextField()
slug = AutoSlugField(populate_from='baslik',unique=True)
kategoriler = models.ManyToManyField(KategoriModel,related_name='yazi')
yazar = models.ForeignKey('account.CustomUserModel',related_name='yazilar',on_delete=models.CASCADE)
class Meta:
verbose_name = 'Yazi'
verbose_name_plural = 'Yazilar'
db_table = 'Yazi'
def __str__(self) -> str:
return self.baslik当profildetailview.get_object()启动服务器时,ı()丢失了一个必需的位置参数:‘ı’,你能帮我谢谢吗?
发布于 2022-06-05 16:30:51
没有必要做"get函数“。相反,您需要在视图中定义模型。像这样做:
from .models import YazilarModel
class ProfilDetailView(DetailView):
template_name = 'pages/profil.html'
model = YazilarModel
context_object_name='profil'https://stackoverflow.com/questions/72508973
复制相似问题