2016-08-20 92 views
0

管理员的ID是当我在管理面板中打开管理员用户。同样,michael的编号是但是当我点击配置文件图标而不是显示管理员的配置文件时,我获得了michael的配置文件。为了得到我使用的编号为requested useruser.id其他用户的用户档案显示

另外问题是我不能在这种模型中使用slu slu。

餐厅/ 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 %} 

的UserProfiles/urls.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' 
    ), 

] 

的UserProfiles/views.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 %} 

的UserProfiles/models.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}) 

如何使用金属块对于这样的模型,以便在管理面板该用户的段塞被自动保存?因为没有post方法。

但主要问题是我正在获取其他用户的userprofile。

+0

如果你创建了一个id = 3的第三个用户,你会得到Michael吗? – dtgq

+0

有3个用户。一位管理员,一位迈克尔和另一位匿名。如果我做http:// localhost:8000/userprofiles/profile/1 /我得到michael的用户配置文件和/ 2 /显示匿名和/ 3 /显示404错误的用户配置文件。 – Serenity

+0

在管理面板中,我检查了用户admin的ID是1,michael是2,匿名是3. – Serenity

回答

0

只需添加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是一个整数。

+0

如果我用slug代替id,该怎么办?但我认为如果我使用slug,我必须使用pre_save信号来保存user.username名称中的slug。我对吗? – Serenity

+0

好的,在这种情况下,你会在你的模型中创建一个slug字段,并且你可以在任何你保存实例的地方设置slug字段,如果你愿意,你可以在pre_save中完成。你也必须让url路由器传递给你的视图,而不是整数。 – dtgq

+1

我发布我的新解决方案的答案,但我标记你的答案为答案。谢谢你的帮助。 – Serenity

0

我用slu instead代替id和使用slu i我已经使用pre_save信号,其中slu value值取自用户名。

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}) 

我用这种方式填补了slu value的价值。

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)