如何在Django物化视图中传递Checkbox参数?

2024-05-20 14:37:07 发布

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

安装程序包含Django和Materialize视图库。你知道吗

两个问题:

1)使用Materialize复选框时,如何将参数从html传输到.py文件?你知道吗

2)如果这样做有效,那么如何将它们保留在会话中,以便下次访问页面时,复选框不为空?你知道吗

我在google上搜索了很多次,做了很多尝试都没有成功。你知道吗

库:href=“https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css““

你知道吗主页.html地址:

<form action="#">
     {% for sites in wsites %}
        <label >
            <input type="checkbox" name="option{{sites.id}}" id="option{{sites.id}}" value={{sites.id}}/> 
            <span>{{sites.name}} </span>
        </label> 
        <br>
     {% endfor %}
</form>

你知道吗视图.py你知道吗

def homepage(request):
    if request.method == 'POST':
        print(request.POST) 
    return render(request = request, 
                  template_name='main/home.html', 
                  context = {"wsites":Sites.objects.all })

会话参数为空列表,即打印(请求.POST) = []. 如果能得到一些帮助来解决这个问题,我将不胜感激。你知道吗


Tags: namepyformid参数requesthtmlpost