小编val*_*lov的帖子

如何处理AngularJS中的$ resource服务错误

我正在向我的API发出请求,我正在使用AngularJS $资源模块.它与$ http不同,所以我不知道如何处理我的错误.

我的服务:

var appServices = angular.module('app.services', ['ngResource']);
appServices.factory('Category', ['$resource',
    function($resource){
        return $resource('/apicategoryerr/?format=:format', {}, {
            query: {
                method: 'GET', 
                params: { format: 'json'}, 
                isArray: true,

            }
        });
    }]);
Run Code Online (Sandbox Code Playgroud)

我的控制器:

...
Category.query(function(data) {
                console.log(data);
            });
...
Run Code Online (Sandbox Code Playgroud)

我想要这样的东西或..如果我的API无效,我不知道如何处理错误..

Category.query().success(function() {
                console.log('success');
            }).error(function() {
                console.log('error');
            });
Run Code Online (Sandbox Code Playgroud)

angularjs angular-resource

96
推荐指数
2
解决办法
8万
查看次数

Facebook Auth与AngularJS和Django REST框架

我正在使用AngularJS开发一个SPA应用程序,它使用Django后端作为服务器.我从SPA与服务器通信的方式是使用django-rest-framework.所以现在我想用facebook(google和twitter)进行身份验证,我在这个主题上阅读了很多内容,发现OAuth.io正在客户端SPA端进行验证,而python-social-auth正在做同样的事情但在服务器端.

所以目前我只有客户端身份验证,我的应用程序连接到Facebook(使用OAuth.io)并成功登录.此过程返回access_token然后我向我的API发出请求,该请求必须登录此用户或通过给定令牌为该用户创建帐户,此部分不起作用.所以我不确定我错在哪里,也许是因为没有关于使用python-social-auth的完整教程所以也许我错过了什么或者......我不知道..

所以我的一些代码:

在SPA方面:这是与OAuth.io的连接,并且正在工作,因为我正在获取访问令牌.然后我必须向我的其他API发出请求.后端是'facebook','google'或'twitter'

OAuth.initialize('my-auth-code-for-oauthio');
OAuth.popup(backend, function(error, result) {
    //handle error with error
    //use result.access_token in your API request

    var token = 'Token ' + result.access_token;
    var loginPromise = $http({
         method:'POST', 
         url: 'api-token/login/' + backend + '/', 
         headers: {'Authorization': token}});

         loginPromise.success(function () {
             console.log('Succeess');
         });
         loginPromise.error(function (result) {
             console.log('error');
         });
});
Run Code Online (Sandbox Code Playgroud)

在我的settings.py中的服务器上,我已经为已安装的应用程序,模板上下文预处理器,一些auth后端添加了社交插件,这是我的文件:

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    ...,
    'rest_framework',
    'rest_framework.authtoken',
    'api',
    'social.apps.django_app.default',
    'social'
)
TEMPLATE_CONTEXT_PROCESSORS = ("django.contrib.auth.context_processors.auth", …
Run Code Online (Sandbox Code Playgroud)

facebook angularjs django-rest-framework python-social-auth

28
推荐指数
2
解决办法
7696
查看次数

Django REST框架发布对象数组

我正在使用Django REST框架用于API和Angular SPA与Restangular用于通信API.有时我必须添加多个对象才能添加,我认为我可以将它们一起发送到数组中并发出一个请求.

当我尝试从Restangular中执行此操作时,我错误地输入了错误输入.如果我尝试从REST框架Web界面添加多个对象,我将传递对象或对象数组:

// this { "text": "gdhg", },{ "text": "gdhg", },{ "text": "gdhg", }
// or this [{ "text": "gdhg", },{ "text": "gdhg", },{ "text": "gdhg", }]
Run Code Online (Sandbox Code Playgroud)

但我收到了ParseError.我错了,我必须改变或如何正确地做.

django django-rest-framework restangular

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

Django REST框架教程无法正常工作

我想为我的项目制作一个API,我找到了Django REST Framework - http://django-rest-framework.org/

我在这里尝试了他们的教程http://django-rest-framework.org/tutorial/quickstart.唯一的区别是我的应用程序被称为api.我的问题是,当我使用我的管理员用户登录时,我收到以下错误:

Exception Type: NoReverseMatch
Exception Value:    
Reverse for 'user-list' with arguments '()' and keyword arguments '{}' not found.
Run Code Online (Sandbox Code Playgroud)

我试图找到一个解决方案,但结果是我在这里问是否有人有想法:)

urls.py

from django.conf.urls import patterns, include, url
from django.conf.urls.static import static
from django.conf import settings

#not sure
from django.contrib.staticfiles.urls import staticfiles_urlpatterns

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

from dajaxice.core import dajaxice_autodiscover, dajaxice_config
dajaxice_autodiscover()

js_info_dict = {
    'packages': ('cards',),
}

urlpatterns = patterns('',
    # Examples:
    url(r'^$', …
Run Code Online (Sandbox Code Playgroud)

python django django-rest-framework

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

Django REST框架的ManyRelatedField序列化

我试图用Django Rest框架序列化我的模型 - http://django-rest-framework.org/

我想要的是序列化其中包含ManyToMany关系的模型:

class ImageResource(models.Model):
    # Some code here
    image = models.ImageField(upload_to=upload_images_to)
    keywords = models.ManyToManyField('cards.Keyword', related_name='image_keywords', blank=True);
    # More code here
Run Code Online (Sandbox Code Playgroud)

所以这是我的模型(我删除了一些字段以帮助您专注于关键字字段)

我的serialallizer看起来像这样:

class ImageResourceSerializer(serializers.HyperlinkedModelSerializer):
    keywords = serializers.ManyRelatedField(source='keywords')

    class Meta:
        model = ImageResource
        fields = ('id', 'url', 'image', 'keywords')
Run Code Online (Sandbox Code Playgroud)

我将展示的最后一件事是API的结果

{
        "id": 2, 
        "url": "http://127.0.0.1:3004/apiimageresource/2/", 
        "image": "images/1386508612-97_img-02.JPG", 

        "keywords": [
            "birthday", 
            "cake"
        ]
    },
Run Code Online (Sandbox Code Playgroud)

如您所见,关键字从字符串(它们的名称)返回为数组.我希望将它们作为键值对返回其id和值:

"keywords": [
    "1":"birthday",
    "3":"cake"
]
Run Code Online (Sandbox Code Playgroud)

如果您知道如何使用我的serialallizer,我将感谢:)

django serialization django-rest-framework

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

使用访问器进行C#单元测试 - 构造函数不起作用

我必须在我的应用程序中编写单元测试,但我遇到了问题.我正在使用C#和.NET 4.在我的测试中,我无法访问类的私有属性和方法,所以我在单元测试中为每个类使用自动生成的Accessors但是......

我的Accessor类的构造函数不接受它们的参数.例:

class SearchControl(bool isLogged, MainWindow mainWindow);
class MainWindow();
Run Code Online (Sandbox Code Playgroud)

要创建SearchControl类型的对象,您需要传递mainWindow对象.因此,如果我用Accessor类创建它,我无法访问私有方法和属性,我无法测试它们.

MainWindow mainWindow = new MainWindow();
SearchControl serchControl = new SearchControl(false, mainWindow);
Run Code Online (Sandbox Code Playgroud)

我必须使用Accessor clasees,但是当我这样做时,我的代码用红色加下划线,Visual Studio说不能接受参数.为什么,当我传递相同类型的参数时.如果我再次将MainClass对象传递给SearchControl_Accessor对象,我无法访问MainClass中的propeerties.所以带有访问器的代码如下所示:

MainWindow_Accessor mainWindow = new MainWindiow_Accessor();
SearchControl_Accessor searchControl = new SearchControl_Accessor(false, mainWindow);
Run Code Online (Sandbox Code Playgroud)

任何人都知道什么是错的,我必须做些什么来解决它.谢谢 :)

.net c# wpf unit-testing accessor

0
推荐指数
1
解决办法
5554
查看次数

IE不了解CSS继承

我有Internet Explorer的问题,当然它在其他浏览器中工作得很好.所以我有一个CSS clases.我正在做一个像左边,中间和右边部分的框架,但有三种不同的配色方案.所以我不想制作9个不同的类,我使用CSS功能就像这个例子:

.container-header .left { /* Some styles here... */ }
.container-header .left.style1 { /* Some styles here... */ }
.container-header .left.style2 { /* Some styles here... */ }
.container-header .left.style3 { /* Some styles here... */ }

.container-header .middle { /* Some styles here... */ }
.container-header .middle.style1 { /* Some styles here... */ }
.container-header .middle.style2 { /* Some styles here... */ }
.container-header .middle.style3 { /* Some styles here... */ }

.container-header .right { /* Some …
Run Code Online (Sandbox Code Playgroud)

html css internet-explorer

0
推荐指数
1
解决办法
369
查看次数