首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法添加到详细页面用户的帖子İn django

无法添加到详细页面用户的帖子İn django
EN

Stack Overflow用户
提问于 2022-06-05 16:06:40
回答 1查看 23关注 0票数 0

嘿,DetailViewı试图将用户的帖子添加到细节页面,但是ı不能添加,这是ı编写的代码

profile.py

代码语言:javascript
复制
        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

代码语言:javascript
复制
    path('kullanıcı/<str:username>',ProfilDetailView.as_view(),name='profil'),

profil.html

代码语言:javascript
复制
{% 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

代码语言:javascript
复制
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()启动服务器时,ı()丢失了一个必需的位置参数:‘ı’,你能帮我谢谢吗?

EN

回答 1

Stack Overflow用户

发布于 2022-06-05 16:30:51

没有必要做"get函数“。相反,您需要在视图中定义模型。像这样做:

代码语言:javascript
复制
from .models import YazilarModel

class ProfilDetailView(DetailView):
        template_name = 'pages/profil.html'
        model = YazilarModel
        context_object_name='profil'
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72508973

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档