代码之家  ›  专栏  ›  技术社区  ›  Steve

Django视图,按用户向访问者显示对象

  •  2
  • Steve  · 技术社区  · 7 年前

    我创建了一个应用程序,允许注册用户使用表单创建产品。每个注册用户都有一个配置文件页面,显示他们的产品,只有他们登录时才能看到。我想创建一个视图,允许未注册用户通过单击用户名查看任何用户的产品。我该怎么做?

    以下是产品表单:

    class ProductForm(forms.ModelForm):
        class Meta:
            model = Product
            fields = ['name', 'description', 'url', 'product_type', 'price', 'image', 'image_url']
            labels = {
                'name': 'Product Name',
                'url': 'Product URL',
                'product_type': 'Product Type',
                'description': 'Product Description',
                'image': 'Product Image',
                'image_url': 'Product Image URL',
                'price': 'Product Price'
            }
            widgets = {
                'description': Textarea(attrs={'rows': 5}),
            }
    

    产品视图很简单:

        def products(request):
        products = Product.objects.all()
        form = ProductForm()
        return render(request, 'products.html', {'products': products, 'form':form})
    
    
    def post_product(request):
        # if this is a POST request we need to process the form data
        if request.method == 'POST':
            # create a form instance and populate it with data from the request:
            form = ProductForm(data = request.POST, files = request.FILES)
            # check whether it's valid:
            if form.is_valid():
                # process the data in form.cleaned_data as required
                product = form.save(commit = False)
                product.user = request.user
                product.likes = 0
                product.save()
            # redirect to a new URL:
            return HttpResponseRedirect('/products')
    

    1 回复  |  直到 7 年前
        1
  •  2
  •   Ykh    7 年前
    class ProductListView(ListView):
        template_name = 'products.html'
        context_object_name = 'product_list'
        paginate_by = None
    
        def get_queryset(self):
            username = self.request.GET.get('username',None)
            user = None
            if username:
                try:
                    user = User.objects.get(username=username)
                except (User.DoesNotExist, User.MultipleObjectsReturned):
                    pass
            if user:
                return Product.objects.filter(user=user)
            return Product.objects.none()
    

    URL。py:

    url(r'^product/$', ProductListView.as_view(), name='product_list'),
    

    访问类似页面 www.example.com/product?username=testuser

    根据您的编辑:

    def products(request):
        username = request.GET.get('username',None)
        user = None
        if username:
            try:
                user = User.objects.get(username=username)
            except (User.DoesNotExist, User.MultipleObjectsReturned):
                pass
        if user:
            return Product.objects.filter(user=user)
        else:
            products = Product.objects.all()
        form = ProductForm()
        return render(request, 'products.html', {'products': products, 'form':form})