如何在views.py [Django]中传递可选参数

肮脏的德赛

urls.py:

def shopgrid(request, proname=None):
    products = Product.objects.all()
    categories = Categories.objects.all()
    recent_products = Product.objects.order_by('-date')[0:3]
    if proname != None:
        products = Product.objects.filter(categorie__title=proname)
    
    if request.method == "GET":
        recent = request.GET.get('val')
        amount = request.GET.get('amount')
        print('This is amount--->',amount)

        if recent == 'name':
            products = Product.objects.order_by('name')
        if recent == 'price':
            products = Product.objects.order_by('-price')

    if  'val' in request.GET:
        check_values = request.GET.getlist('val')
        first_element=check_values[0]
        last_element=check_values[-1]
        fe = first_element[0:4]
        le = last_element[-4:]
        min_price = int(fe)
        max_price = int(le)

        products = Product.objects.filter(price__range=(min_price, max_price))
        print(products)
            
    context={'p':products, 'rp':recent_products, 'cat':categories}
    return render(request, 'cart/shop-grid.html', context)

urls.py:

urlpatterns = [
    path('', views.index, name="index"),
    path('shopgrid/', views.shopgrid, name="shopgrids"),
    path('shopgrid/<str:proname>/', views.shopgrid, name="shopgrid"),
    path('cat_sort/<str:proname>/', views.cat_sort, name="cat_sort"),
]

错误:

**Reverse for 'shopgrid' with no arguments not found. 1 pattern(s) tried: ['shopgrid/(?P<proname>[^/]+)/$']**

<form id="priceform" action="**{% url 'shopgrid' %}**" method="GET">
{% csrf_token %}
<input type="text" id="amount" name="amount" placeholder="Add Your Price">                      
</form>

def shopgrid(request,proname = None):proname是一个可选参数,问题是如何在Jinja模板中编写可选参数。我已经写了一个proname = None,但是我只遇到HTML错误

威廉·范昂塞姆

没有参数的视图名称,因此url应该为:shopgrids

<form id="priceform" action="{% url 'shopgrids' %}" method="GET">
    {% csrf_token %}
    <input type="text" id="amount" name="amount" placeholder="Add Your Price">
</form>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在views.py django中设置默认图像

如何在views.py索引函数中的django中添加我自己的python代码

如何在views.py中获取_available_languages

django如何在views.py文件中自动执行功能

Python Django如何在views.py中获取登录用户的价值?

如何在Views.py中运行函数并打印结果?-Django

如何在django views.py中获取选定选项的文本值

如何在views.py中重定向CreateView的URL

如何在views.py中调用django的自定义书面AuthenticationBackend的authenticate()方法?

如何在views.py中更新模型的值?

如何在Django中传递多个可选的URL参数?

如何在views.py中打开静态文件

如何在views.py中动态选择Django模型字段

如何在Django中创建views.py的excel下载选项

如何在PowerShell中传递可选参数?

如何在views.py中使用url参数过滤模型(用户)?

如何在django views.py中的函数中设置许多mysql选择查询

如何在views.py django中获取文本框值

如何在 Django 的模板中使用 views.py 中的变量?

如何在 views.py 中创建模型的实例?

如何在 django python 中的 url 中传递可选参数

django 如何在 views.py 中使用减法

如何在 django views.py 中提取模型的所有值实例

如何在 django views.py 中的单个变量或列表中存储多个值?

如何在 views.py 中创建 Django 模型?

如何在java中传递可选参数

如何在views.py中获取ForeignKey的值?

如何在 Django views.py 中而不是在模板中打印数据?

如何在views.py Django中返回ajax響應以及重定向