簡體   English   中英

在評論部分將單個用戶的個人資料鏈接到用戶-Django

[英]link individual user's profile to user in comment section - Django

我需要將各個用戶的個人資料鏈接到評論。 目前,我所有的鏈接都將我帶到已登錄用戶的個人資料本身。

我的用戶模型@ user_profile / models.py:

class MyUser(AbstractBaseUser, PermissionsMixin):        
    email = models.EmailField(_('email address'), max_length=254, unique=True)        
    .......
    .......

    objects = manager.CustomUserManager()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['date_of_birth']

    class Meta:
        verbose_name = _('user')
        verbose_name_plural = _('users')

    def get_absolute_url(self):
        return "/users/%s/" % urlquote(self.email)

我的個人資料模型:

class Profile(models.Model):
    user = models.OneToOneField(settings.AUTH_USER_MODEL,
                                related_name="profile",
                                on_delete=models.CASCADE)
    bio = models.TextField(max_length=500, blank=True, default='')
    .....
    .....

    objects = manager.CustomProfileManager()

    def get_absolute_url(self):
        return reverse("user_profile:commenters_profile")

    @property
    def get_profile(self):
        instance = self
        content_type = ContentType.objects.get_for_model(instance.__class__)
        return content_type

評論/ models.py:

class Comment(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='comment_by', default=1)
    ....

視圖:

@login_required
def commenters_profile(request):
    instance = get_object_or_404(Profile)

    context = {
        "title": instance.title,
        "instance": instance.get_profile(),
        }
    return render(request, "profile.html", context)

user_profile / urls.py:

urlpatterns = [
    url(r'^$', myProfile, name='myProfile'),
    url(r'^$', commenters_profile, name='commenters_profile'),
]

模板:

<p><a href="{{ comment.user.profile.get_absolute_url }}"> {{ comment.user.get_full_name }}</a></p>

請幫忙。

更新:

追溯 (最近一次通話):

File "/usr/local/lib/python2.7/dist-packages/Django-1.11-py2.7.egg/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/usr/local/lib/python2.7/dist-packages/Django-1.11-py2.7.egg/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/usr/local/lib/python2.7/dist-packages/Django-1.11-py2.7.egg/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/usr/local/lib/python2.7/dist-packages/Django-1.11-py2.7.egg/django/contrib/auth/decorators.py", line 23, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "/home/surajit/website/PubNet/user_profile/views.py", line 173, in commenters_profile
    "instance": instance.get_profile(),
TypeError: 'ContentType' object is not callable

profile.html:

<div class="col-sm-8 col-md-3">
            <img src="{{ user.profile.photo.url }}", width="220", alt="", class="img-rounded img-responsive" />
            <br/><p> <i class="glyphicon glyphicon-envelope"></i> {{ user.email }}
                <br /> <i class="glyphicon glyphicon-gift"></i> {{ user.date_of_birth }}</p>
        </div>
        <div class="col-sm-4 col-md-6">
            <blockquote>
                <p><h2>{{ user.first_name }} {{ user.last_name }}</h2></p>
                <small><cite title="Source Title">Some Text Here <i class="glyphicon glyphicon-map-marker"></i></cite></small>
            </blockquote>
            <br/><p> {{ user.profile.bio }}</p>
        </div>

評論部分的屏幕截圖:

在此處輸入圖片說明

因為myProfilecommenters_profile具有相同的正則表達式,所以您永遠不會到達commenters_profile ,所有請求都將發送到myProfile 更改commenters_profile網址:

url(r'^(?P<pk>\d+)/$', commenters_profile, name='commenters_profile'),

修復get_absolute_url方法:

def get_absolute_url(self):
    return reverse("user_profile:commenters_profile", kwargs={'pk': self.pk})

並查看:

@login_required
def commenters_profile(request, pk):
    instance = get_object_or_404(Profile, pk=pk)

    context = {
        "title": instance.title,
        "instance": instance.get_profile(),
        }
    return render(request, "profile.html", context)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM