【问题标题】:Can we pass two args in url - django?我们可以在 url - django 中传递两个参数吗?
【发布时间】:2020-12-09 12:22:09
【问题描述】:

这是我用于类别和产品的 models.py 文件

models.py

from django.db import models
    
# Create your models here.
class Category(models.Model):
        objects = models.Manager()
        name = models.CharField(max_length=250, null=False)
        slug = models.CharField(max_length=250, null=False)
        desc = models.TextField(blank=True)
    
        class Meta:
            verbose_name = 'category'
            verbose_name_plural = 'categories'
    
        def __str__(self):
            return self.name
    
class Product(models.Model):
        objects = models.Manager()
        name = models.CharField(max_length=250, null=False)
        slug = models.CharField(max_length=250, null=False)
        desc = models.TextField(blank=True)
        category = models.ForeignKey(Category, on_delete=models.CASCADE)
    
        class Meta:
            verbose_name = 'product'
            verbose_name_plural = 'products'
    
        def __str__(self):
            return self.name
    

这是我卡住的url文件:

urls.py

from django.urls import path, include
from website import views
urlpatterns = [
        path('', views.home, name='home'),
        path('categorys/<slug:category_slug>/', views.category_product, name='category_product'),
        path('category/<slug:category_slug>/<slug:product_slug>', views.product, name='product_slug'),
]

是否可以在 url 中从 html 中传递两个参数,以便我们可以将其传递给 product 函数?

views.py

from django.shortcuts import render, get_object_or_404
from website.models import Product, Category
    
def home(request):
        allcategories = Category.objects.all()
        return render(request, 'website/home.html', {'allcategories':allcategories})
    
    
def product(request, category_slug, product_slug):
        product = Product.objects.get(category__slug=category_slug, slug=product_slug)
        return render(request, 'website/display_product.html', {'product':product})
    
def category_product(request, category_slug):
        current_category = get_object_or_404(Category, slug=category_slug)
        product = Product.objects.filter(category=current_category)
        return render(request, 'website/category_product.html',{'current_category':current_category, 'product':product})

这是我试图将两个参数传递给 urls.py 但遇到错误的 html 页面,请指导我,非常感谢任何帮助,谢谢!

category_product.html

    Category name : {{ current_category.name }} <br>   

    <h3>Products </h3>
    
    {% for p in product %}
        <a href="{% url 'product_slug' arg1=current_category.name arg2=p.slug %}">
            <b>{{ p.name  }}</b>  <br>
        </a>
    {% endfor %}
    
    ## how to pass two args (or is it possible here) so that it will be passed to the urls.py 

【问题讨论】:

  • 你得到什么错误?
  • 该错误已解决,但现在面临另一个错误,先生! stackoverflow 不允许我现在再提出一个问题,否则我已经上传了当前问题的代码,但非常感谢先生和对你的爱

标签: html django parameters django-templates django-urls


【解决方案1】:

您已通过category_slugproduct_slug。但是您使用 arg1 和 arg2 时,参数名称很重要。

category_product.html

Category name : {{ current_category.name }} <br>   
    
<h3>Products </h3>
        
{% for p in product %}
      <a href="{% url 'product_slug' category_slug=current_category.name product_slug=p.slug %}">
          <b>{{ p.name  }}</b>  <br>
       </a>
{% endfor %}

【讨论】:

  • 你也欢迎 :) 。别忘了接受这个。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-07
相关资源
最近更新 更多