小编Ole*_*ive的帖子

类没有“对象”成员

所以我是 django 的新手,我正在努力打造一个小市场。我做了一个产品应用程序。这是内部代码:这是用于views.py:

from django.shortcuts import render
from django.http import HttpResponse
from products.models import product


def index(request):
  Products = product.objects.all()
  return render(request, 'index.html', {'products': Products})

def new(request):
  return HttpResponse('New Product')
Run Code Online (Sandbox Code Playgroud)

这是用于models.py:

from django.db import models


class product(models.Model):
  name = models.CharField(max_length=150)
  price = models.FloatField()
  stock = models.IntegerField()
  image_url = models.CharField(max_length=2083)
Run Code Online (Sandbox Code Playgroud)

我还制作了一个模板文件夹并将其放入其中进行实验:

<h1>Products</h1>
<ul>
  {% for product in Products %}
    <li>{{ product.name }}</li>
  {% endfor %}
</ul>
Run Code Online (Sandbox Code Playgroud)

以及其他一些常用代码。但我在这部分收到一个pylint错误:

<h1>Products</h1>
<ul>
  {% for product in Products %}
    <li>{{ product.name }}</li>
  {% endfor %} …
Run Code Online (Sandbox Code Playgroud)

django django-models python-3.x

6
推荐指数
1
解决办法
6054
查看次数

接口{}到[]字符串

我正在尝试将一些 YAML 文件解析为 go 结构,但文件本身不能被视为普通的 go 结构:某些值可能是 string 或 map[string][]string。

我尝试过的是自定义解组函数:

func (domain *Domain) UnmarshalYAML(unmarshal func(interface{}) error) error {
    fmt.Println("Parsing domain")

    var hostUrl interface{}
    unmarshal(&hostUrl)
    fmt.Println(reflect.TypeOf(hostUrl))

    switch hostUrl.(type) {
    case string:
        domain.Host = hostUrl.(string)
    case map[interface {}]interface {}:
        fmt.Println("got map")
        v := reflect.ValueOf(hostUrl)
        fmt.Println(v.MapKeys()[0])
        for _, host := range v.MapKeys() {
            domain.Host = host.Interface().(string)

            fmt.Println(v.MapIndex(host)) 
            //HERE I NEED TO DO SMTH LIKE THIS:
            //domain.Urls = v.MapIndex(host).([]string)

        }
    default:
        return errors.New("invalid config file, cant parse domains")
    }

    return nil
}
Run Code Online (Sandbox Code Playgroud)

我的域结构如下所示: …

yaml go

5
推荐指数
1
解决办法
8643
查看次数

标签 统计

django ×1

django-models ×1

go ×1

python-3.x ×1

yaml ×1