通过发布请求获取选择django

时间:2020-11-06 16:23:12

标签: python django

我发现许多解决方案可以在Django领域获得选择,但是在使用模板时它们可以工作 例如get_foo_display

CATEGORY_CHOICES = (
        ('M', 'Male'),
        ('F', 'Female'),

但是,如果我需要在views.py中获得request.POST为男性或女性的情况,该怎么办。 目前我正在使用request.POST['gender'],但这给了我M或F

2 个答案:

答案 0 :(得分:0)

您需要在M中将Male更改为F,将Female更改为CATEGORY_CHOICES。第一个是发送到后端的术语,第二个是客户端将“看到”的字符串。

答案 1 :(得分:0)

如果我有选择的话,我会做这样的事情,

   'password' => 'required|string|min:8|regex:/[a-zA-Z0-9]/',          

然后在视图中

   {
"datavalues": [
    {
        "id": "10",
        "userid": "expertcook",
        "foodtitle": "FishCurry",
        "foodpic": "../foodimage/expertcook/Fishcurry",
        "location": "",
        "rating": "4.2"
    },
    {
        "id": "5",
        "userid": "darth2",
        "foodtitle": "Punjabi Mutton Curry Recipe",
        "foodpic": "../foodimage/darth/PunjabiMuttonCurry",
        "location": "",
        "rating": "3.9"
    },
    {
        "id": "8",
        "userid": "darthname3",
        "foodtitle": "Authentic Bangladeshi Beef Curry",
        "foodpic": "../foodimage/darthname3/AuthenticBangladeshiBeefCurry",
        "location": "",
        "rating": "3.7"
    },
    {
        "id": "4",
        "userid": "darth",
        "foodtitle": "Indian Chicken Curry",
        "foodpic": "../foodimage/darth/Indianchickencurry",
        "location": "",
        "rating": "3.6"
    },
    {
        "id": "9",
        "userid": "nadakriger",
        "foodtitle": "Rabbit Curry",
        "foodpic": "../foodimage/nadakriger/Rabbitcurry",
        "location": "",
        "rating": "3.2"
    },
    {
        "id": "11",
        "userid": "cookingartist",
        "foodtitle": "chicken samosa",
        "foodpic": "../foodimage/cookingartist/chickensamosa",
        "location": "",
        "rating": "3"
    }
],
"status": true
}