小编Paw*_*wan的帖子

Django Rest框架文件上传

我正在使用Django Rest Framework和AngularJs上传文件.我的视图文件如下所示:

class ProductList(APIView):
    authentication_classes = (authentication.TokenAuthentication,)
    def get(self,request):
        if request.user.is_authenticated(): 
            userCompanyId = request.user.get_profile().companyId
            products = Product.objects.filter(company = userCompanyId)
            serializer = ProductSerializer(products,many=True)
            return Response(serializer.data)

    def post(self,request):
        serializer = ProductSerializer(data=request.DATA, files=request.FILES)
        if serializer.is_valid():
            serializer.save()
            return Response(data=request.DATA)
Run Code Online (Sandbox Code Playgroud)

由于post方法的最后一行应该返回所有数据,我有几个问题:

  • 如何检查是否有任何内容request.FILES
  • 如何序列化文件字段?
  • 我该如何使用解析器?

python django angularjs django-rest-framework

76
推荐指数
9
解决办法
9万
查看次数

AngularJs删除ng-repeat中的重复元素

我有一个存储的词典 field_detail

<li ng-repeat = "field in field_detail">{{field.displayName}}</li>
Run Code Online (Sandbox Code Playgroud)

现在我不想包括重复displayNamefield_detail,什么filter应该怎么用?

angularjs angularjs-ng-repeat

36
推荐指数
2
解决办法
6万
查看次数

Opencv 3.0 - 模块对象没有属性'xfeatures2d'

我已经从OpenCV 2.4.9转移到3.0以利用drawMatchesdrawMatchesKnn功能.我开始知道它没有像SIFT,SURF这样的非自由算法.所以我通过以下步骤从https://github.com/Itseez/opencv_contrib安装了opencv_contrib

cmake -DOPENCV_EXTRA_MODULES_PATH=/home/zealous/Downloads/opencv_contrib-master/modules /usr/local ..

make -j5

make install
Run Code Online (Sandbox Code Playgroud)

我还交叉检查了opencv的模块,xfeatures2d就在那里.然后,当我试图做

>>> import cv2
>>> help(cv2.xfeatures2d)
Run Code Online (Sandbox Code Playgroud)

它给了我以下错误

Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    help(cv2.xfeatures2d)
AttributeError: 'module' object has no attribute 'xfeatures2d'
Run Code Online (Sandbox Code Playgroud)

我在这做错了什么.仅供参考,我使用的是OpenCV 3.0测试版.有没有为xfeatures2d打开 OpenCV的python包装器或者我没有正确安装它?

python opencv cmake surf sift

29
推荐指数
1
解决办法
1万
查看次数

输入类型= angularjs中的数字验证

我试图< input type = number >通过使用angularjs模块ng的输入[number]指令进行验证.

使用类型编号的输入时,将max(或min)属性设置为数字,例如

<input type=number min="20" max="40">

它工作正常,但我的最小和最大数据是使用ng-repeat动态地传输数据,例如

<input type=number min="configRow.valueStart" max="configRow.valueEnd"> ,那它不起作用.

我知道min和max只接受数字而且我写的指令不太好,请帮助我任何这样的目录或任何建议将不胜感激.

javascript angularjs

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

Python:将cookie设置到另一个网站

我正在实施一个广告系统,当一个用户将脚本代码放入他们的网站时A.当时我想做的是在网站A中设置一个cookie,同时显示网站B(广告系统)的响应或资源,所以在用户再来的今天,我可以记录它的条目.我已经读过这个问题并且知道当该网站A显示来自另一个网站B的内容时,可以将cookie设置到其他网站A.

在脚本中,我正在执行一个rest API并返回一个响应,如下所示.

source_image = "http://example.com/media/format.png"
response = Response({'success':source_image})
response.set_cookie( 'cookie_name', 'cookie_value' )
return response
Run Code Online (Sandbox Code Playgroud)

现在我能够在浏览器中看到此URL的响应中设置的cookie.但是当我重新加载它时,cookie无法显示.也就是为什么它没有显示在我放下脚本代码的网站的cookies部分.

我是否正确设置cookie?我试图设置cookie的有效期为1天.但它仍然被摧毁..请帮助..

javascript python django cookies

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

Django错误:userprofile匹配查询不存在

我是Django的新手.我正在尝试创建UserProfile.首先,我在models.py中创建了一个模型及其处理程序,如下所示.

class UserProfile(models.Model):
    user = models.ForeignKey(User,null=False)

    name = models.CharField(max_length=200)

    def __unicode__(self):
        return self.name

def create_user_profile(sender, instance, created, **kwargs):
    if created:
        UserProfile.objects.create(user=instance)

post_save.connect(create_user_profile, sender=User)
Run Code Online (Sandbox Code Playgroud)

然后用.编辑settings.py

AUTH_PROFILE_MODULE = "lib.UserProfile"
Run Code Online (Sandbox Code Playgroud)

其中lib是包含init .py,models.py和all的根文件夹.

然后我删除了集合中的所有当前用户,当我从管理面板重新输入它们时,将自动创建一个新集合lib_userprofile,其中包含我在模型中提到的字段.现在我提出如下观点

  class CurrentUser(APIView):
        authentication_classes = (authentication.TokenAuthentication,)
        def get(self,request):
            if request.user.is_authenticated():
                        profile=request.user.get_profile()
                        return Response(profile)
Run Code Online (Sandbox Code Playgroud)

但是给了我以下错误..

UserProfile matching query does not exist.
Request Method: GET
Request URL:    http://pawan.demoilab.pune/api/currentuser
Django Version: 1.3.7
Exception Type: DoesNotExist
Exception Value:    
UserProfile matching query does not exist.
Exception Location: /usr/local/lib/python2.7/dist-packages/django/db/models/query.py in get, line 351
Python Executable:  /usr/bin/python …
Run Code Online (Sandbox Code Playgroud)

python django

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

使用Django和AngularJs在Mongodb中上传Pdf文件

我想在我的项目中添加一个上传文件的功能,但我很困惑在django或AngularJS中实现该代码的位置,即使我在AngularJs中这样做如何将其与django集成,我也非常困惑于此,请建议我可以从哪里开始实施的一些链接,因为我无法找到.任何建议,将不胜感激..

django file-upload mongodb angularjs

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

(输入类型=日期)点击框时显示日历

我正在使用<input type="date" name="bday">,当我单击框的右侧时它会显示日历,如下所示..

在此处输入图片说明

现在我想在单击框的任何部分时显示日历..我应该编写什么代码来实现这一点..

在此处输入图片说明

html css jquery

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

Opencv Python - 来自特征匹配+同形的相似度得分

我的数据库中有几个鱼图像,我的目标是在用户输入的鱼图像和数据库中的图像之间找到相似度得分.为此我从这个链接使用opencv功能匹配+ Homograpy.

http://opencv-python-tutroals.readthedocs.org/en/latest/py_tutorials/py_feature2d/py_feature_homography/py_feature_homography.html#feature-homography

我目前的代码如下.

query_image = '/home/zealous/Pictures/train_images/AbudefdufWhitleyiJER.jpg'
trained_image_folder = '/home/zealous/Pictures/train_images'
Run Code Online (Sandbox Code Playgroud)

我目前的代码如下.

def feature_matcher(query_image, image_folder):

    min_match_count = 10

    img1 = cv2.imread(query_image, 0)
    surf = cv2.xfeatures2d.SURF_create(800)
    kp1, des1 = surf.detectAndCompute(img1, None)

    bf = cv2.BFMatcher(cv2.NORM_L2)

    all_files = next(os.walk(image_folder))[2]

    for file_name_temp in all_files:
        try:
            train_image = image_folder + '/' + file_name_temp
            img2 = cv2.imread(train_image, 0)
            surf = cv2.xfeatures2d.SURF_create(800)
            kp2, des2 = surf.detectAndCompute(img2, None)

            matches = bf.knnMatch(des1, des2, k=2)

            good = []
            for m, n in matches:
                if m.distance < 0.7*n.distance:
                    good.append(m)

            if len(good) > …
Run Code Online (Sandbox Code Playgroud)

python opencv matching surf feature-detection

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

迭代字典列表并创建新的字典列表

我的数据如下.

[
    {
        "id" : "123",
        "type" : "process",
        "entity" : "abc"
    },
    {
        "id" : "456",
        "type" : "product",
        "entity" : "ab"
    }

]
Run Code Online (Sandbox Code Playgroud)

我循环虽然如下获取id和实体

for test in serializer.data:
    qaResultUnique['id'] = test['id']
    qaResultUnique['entity'] = test['entity']
    uniqueList.append(qaResultUnique)
Run Code Online (Sandbox Code Playgroud)

但得到错误的输出只是两次获得第二本字典.

[
        {
            "id" : "456",
            "entity" : "ab"
        },
        {
            "id" : "456",
            "entity" : "ab"
        }

    ]
Run Code Online (Sandbox Code Playgroud)

我做错了什么,请帮忙.

python dictionary

4
推荐指数
1
解决办法
58
查看次数

基于字段值的Django过滤

我正在使用Django,我想在其中使用过滤器

我的产品和公司型号是

class Product(models.Model):
    name = models.CharField(max_length=200)
    companyId = models.ForeignKey(Comapany)

class Company(models.Model):
    domain = models.CharField(max_length=200)
Run Code Online (Sandbox Code Playgroud)

我想根据当前用户的companyId检索产品.所以我已经像这样实现了我的观点..

class ListProducts(APIView):
    authentication_classes = (authentication.TokenAuthentication,)
    permission_classes = (permissions.IsAdminUser,)

def get(self, request):
        if request.user.is_authenticated():
            userCompanyId = request.user.get_profile().companyId
        products = Product.objects.filter(companyId__id__exact = userCompanyId)
        serializer = ProductSerializer(products)
        return Response(serializer.data)
Run Code Online (Sandbox Code Playgroud)

我的产品数据

{
   "_id": ObjectId("5284ceaae9cfff79368e1f29"),
   "companyId": "528458c4bbe7823947b6d2a3",
   "name": "Apple Juice" 
}
{
   "_id": ObjectId("5267bb4ebbe78220588b4567"),
   "companyId": "52674f02bbe782d5528b4567",
   "name": "Small Soft & Moist Cranberry" 
}
Run Code Online (Sandbox Code Playgroud)

我的公司数据

{
   "_id": ObjectId("528458c4bbe7823947b6d2a3"),
   "domain": "Manufacturing" 
}
{
   "_id": ObjectId("52674f02bbe782d5528b4567"),
   "domain": "Manufacturing" 
}
Run Code Online (Sandbox Code Playgroud)

我得到输出为 []

问题是,在研究django …

python django mongodb django-rest-framework

3
推荐指数
1
解决办法
284
查看次数