显示其他用户的用户配置文件

2024-05-14 02:48:50 发布

您现在位置:Python中文网/ 问答频道 /正文

当我在“管理”面板上打开“管理”用户时,管理的id是1。同样地,michael的id是2但是当我单击profile图标而不是显示admin的profile时,我得到了michael的profile。为了获得id,我使用了requested useruser.id

同样的问题是我不能在这样的模型中使用slug

餐厅/base.html

{% if user.is_authenticated %}
    <li class="nav-item">
        <a class="nav-link user-icon" href="{% url 'userprofiles:profile' user.id %}">
          <i class="fa fa-user"></i>
        </a>
    </li>
{% else %}

用户配置文件/url.py

urlpatterns = [
    # url(r'^profile/(?P<profile_name>[-\w]+)/(?P<profile_id>\d+)/$', views.profile, name='profile'),
    url(
        r'^profile/(?P<profile_id>\d+)/$', 
        views.profile, 
        name='profile'
    ),

]

用户配置文件/视图.py

def profile(request, profile_id):
    if profile_id is "0":
        userProfile = get_object_or_404(UserProfile, pk=profile_id)
    else:
        userProfile = get_object_or_404(UserProfile, pk=profile_id)
        user_restaurant = userProfile.restaurant.all()
        user_order = userProfile.order_history.all()
        total_purchase = 0
        for ur in user_order:
            total_purchase += ur.get_cost()
    return render(
                  request, 
                  'userprofiles/profile.html',
                  {
                   'userProfile':userProfile,
                   'user_restaurant':user_restaurant,
                   'user_order':user_order,
                   'total_purchase':total_purchase
                  }
           )

userprofiles/profile.html

{% for user_restaurant in user_restaurant %}
        {{user_restaurant.name}}<br/>
        {{user_restaurant.address }}
{% endfor %}

用户配置文件/模型.py

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    restaurant = models.ManyToManyField(Restaurant)
    order_history = models.ManyToManyField(OrderMenu)
    # favorites = models.ManyToManyField(Restaurant)
    is_owner = models.BooleanField(default=False)

    class Meta:
        def __str__(self):
            return self.user.username

    # def get_absolute_url(self):
    #   return reverse('userprofiles:profile', kwargs={'slug':self.slug, 'id':self.id})

我如何才能使用这样的模型,以便在管理面板的slug为该用户自动保存?因为没有post方法

但主要的问题是我正在获取另一个用户的userprofile


Tags: 用户nameselfidurlgetmodelsorder
2条回答

只要把1添加到你使用的任何地方profile_id

def profile(request, profile_id):
    if profile_id is "0": # Is profile_id a string or integer?
        userProfile = get_object_or_404(UserProfile, pk=(profile_id+1)) # What does this do?
    else:
        userProfile = get_object_or_404(UserProfile, pk=(profile_id+1))
        user_restaurant = userProfile.restaurant.all()
        user_order = userProfile.order_history.all()
        total_purchase = 0
        for ur in user_order:
            total_purchase += ur.get_cost()
    return render(request, 'userprofiles/profile.html', {'userProfile':userProfile, 
                                                        'user_restaurant':user_restaurant,
                                                        'user_order':user_order,
                                                        'total_purchase':total_purchase })

我怀疑代码中的某个地方出现了n-1问题(即计算机从0开始计数,而人类从1开始计数)。我还没有找到确切的位置,但这可能会作为绷带解决方案在此期间

另外,我不确定if在您的代码中做了什么,如果profile_id是一个整数,它似乎永远不会被使用

我使用slug而不是id,对于使用slug,我使用了pre\ u save signal,其中slug值取自用户名

def profile(request, profile_slug):
    if profile_slug is None:
        userprofile = get_object_or_404(UserProfile,slug=profile_slug)
    else:
        userprofile = get_object_or_404(UserProfile, slug=profile_slug)
        user_restaurant = userprofile.restaurant.all()
        user_order = userprofile.order_history.all()
        total_purchase = userprofile.total_purchase
    return render(request, 'userprofiles/profile.html', {'userprofile':userprofile, 
                                                        'user_restaurant':user_restaurant,
                                                        'user_order':user_order,
                                                        'total_purchase':total_purchase})

我用这种方法填充了slug的值

def create_slug(instance, new_slug=None):
    print('instance',instance.user)
    slug = slugify(instance.user.username)
    if new_slug is not None:
        slug = new_slug
    qs = UserProfile.objects.filter(slug=slug).order_by("-id")
    exists = qs.exists()
    if exists:
        new_slug = "%s-%s" %(slug, qs.first().id)
        return create_slug(instance, new_slug=new_slug)
    return slug


def pre_save_post_receiver(sender, instance, *args, **kwargs):
    if not instance.slug:
        instance.slug = create_slug(instance)

from django.db.models.signals import pre_save
pre_save.connect(pre_save_post_receiver, sender=UserProfile)

相关问题 更多 >