Cant Adding to detail page user's posts İn django
Здравствуйте, я пытаюсь добавить сообщение пользователя на подробную страницу с помощью 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() missing 1 required positional argument: 'request' можете ли вы помочь мне спасибо?
Нет необходимости выполнять "функцию get". Вместо этого вам нужно определить модель в представлении. Сделайте это следующим образом:
from .models import YazilarModel
class ProfilDetailView(DetailView):
template_name = 'pages/profil.html'
model = YazilarModel
context_object_name='profil'