如何将数据从模型收集到选择标签中

时间:2019-02-26 13:40:41

标签: python django

Model.py

class techgroup(models.Model):
    Technology=models.CharField(max_length=50)

    def __str__(self):
       return self.Technology

views.py

def technology(request,id,*args, **kwargs):
    Technology = techgroup.objects.all()
    print(Technology)
    selected_item = get_object_or_404(techgroup, 
                   pk=request.POST.get('Technology_id'))
    # get the user you want (connect for example) in the var "user"
    techgroup.Technology = selected_item
    techgroup.save()
    context = {'Technology': Technology}
    return render(request, "analytics/radio.html", context)

url.py

path('technology/<int:pk>/', views.technology, name='technology'),

此代码有什么问题?我收到以下错误

  

technology()缺少1个必需的位置参数:'id'

1 个答案:

答案 0 :(得分:0)

此问题与您的标题无关。

您的视图需要一个名为id的参数。但是您的网址提供了一个称为pk的网址。这些需要匹配。更改URL:

path('technology/<int:id>/', ...)