简体   繁体   中英

Cant Adding to detail page user's posts İn django

Hey ı am trying to add the user's post to the detail page with DetailView but ı cant add this is the code ı wrote

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

when ı start the server ı get this error profildetailview.get_object() missing 1 required positional argument: 'request' can you help me thank you?

There is no need to do the "get function". Instead you need to define the model in your view. Do it like like this:

from .models import YazilarModel

class ProfilDetailView(DetailView):
        template_name = 'pages/profil.html'
        model = YazilarModel
        context_object_name='profil'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM