小编Cod*_*mon的帖子

使用单引号将字符串解析为JSON?

我有一个字符串

str = "{'a':1}";
JSON.parse(str);
VM514:1 Uncaught SyntaxError: Unexpected token '(…)
Run Code Online (Sandbox Code Playgroud)

我如何解析字符串(str)上面的JSON?这看起来像一个简单的解析; 它不是以某种方式工作.

javascript json

54
推荐指数
7
解决办法
7万
查看次数

Openshift app重定向到https:// domain_name/app

我在Redhat Open shift上主持了一个应用程序.我没有改变任何东西,但它开始重定向到https://www.plovist.com/app并抛出404错误.任何人都可以帮我解决这个问题吗?

cname redhat http-status-code-404 openshift

15
推荐指数
3
解决办法
3328
查看次数

将支付网关与Django-Oscar集成?

我想将支付网关与奥斯卡集成.我已经集成了oscar-paypal它工作正常.我应该遵循oscar-paypal并尝试模仿吗? 本文档 没有给出起始信息但不完全?

我需要这个.创建订单,更改购物篮状态,付款,发送电子邮件以及oscar-paypal正在执行的许多其他步骤.

django django-oscar

15
推荐指数
1
解决办法
1514
查看次数

Django Rest框架中的多个模型?

我正在使用Django Rest框架.我想序列化多个模型并作为响应发送.目前我每个视图只能发送一个模型(如CartView下面只发送Cart对象).以下模型(无关)可以在那里.

class Ship_address(models.Model):
   ...

class Bill_address(models.Model):
   ...

class Cart(models.Model):
   ...

class Giftwrap(models.Model):
   ...
Run Code Online (Sandbox Code Playgroud)

我尝试使用DjangoRestMultiplemodels,它工作正常,但有一些限制.有没有内置的方式?我不能追加到以下视图中创建的序列化程序吗?

from rest_framework.views import APIView

class CartView(APIView):
    """
    Returns the Details of the cart
    """

    def get(self, request, format=None, **kwargs):
        cart = get_cart(request)
        serializer = CartSerializer(cart)
        # Can't I append anything to serializer class like below ??
        # serializer.append(anotherserialzed_object) ??
        return Response(serializer.data)
Run Code Online (Sandbox Code Playgroud)

我真的很喜欢DRF.但是这个用例(发送多个对象)让我觉得如果编写一个普通的旧Django视图将更适合这样的要求.

python django django-rest-framework

14
推荐指数
1
解决办法
9470
查看次数

将Pk或Slug传递给Django中的Generic DetailView?

我是Django Class视图的新手.我正在尝试制作一个简单的视图来获取帖子的详细信息.我的views.py:

from django.views.generic import ListView, View, DetailView 
class GenreDetail(DetailView):
            model = Post
            template_name = "post.html"
Run Code Online (Sandbox Code Playgroud)

我的urls.py:

urlpatterns = [
        url(r'(?P<post_id>[^/]+)', GenreDetail.as_view(), name = 'post'),
        url(r'(?P<post_id>[^/]+)/(?P<slug>[-\w]+)$', GenreDetail.as_view()),
    ] 
Run Code Online (Sandbox Code Playgroud)

我得到的错误:

AttributeError at /2/memoirs-of-a-geisha-by-arthur-golden
Generic detail view GenreDetail must be called with either an object pk or a slug.
Run Code Online (Sandbox Code Playgroud)

因此pk或slug不会传递给Generic Detailview.我怎么通过?我假设从url它可以接收,但事实并非如此.

python django django-class-based-views

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

在Django Rest Framework中访问Viewset和Serializer中的请求对象?

我想在DRF中的Views.py和Serializers.py中访问请求对象.我的Views.py:

class ProductViewSet(viewsets.ReadOnlyModelViewSet):

    """
    This viewset automatically provides `list` and `detail` actions.
    """
    queryset = Product.objects.all()
    serializer_class = ProductSerializer(context={'request': request})
Run Code Online (Sandbox Code Playgroud)

我的Serializers.py:

class ProductSerializer(serializers.HyperlinkedModelSerializer):

    get_sr_price = serializers.SerializerMethodField('get_sr_price_func')

    def get_sr_price_func(self, obj):
        return self.request.user ??

    class Meta:
        model = Product
        fields = (
            'title', 'slug', 'product_stores', 'get_sr_price')
Run Code Online (Sandbox Code Playgroud)

在Serializers.py中,我得到了ProductSerializer' object has no attribute 'request'.另外在views.py中我得到了NameError: name 'request' is not defined

我如何访问请求对象?我是否必须将它从视图传递给序列化程序?那还有views.py和serializers.py之间的区别是什么?通常我会在Views.py中编写所有业务逻辑; 我还应该在视图中执行所有查询/过滤器,还是应该在序列化程序中执行它们,或者它没有任何区别.DRF新手请帮忙.

python django django-rest-framework

10
推荐指数
1
解决办法
9585
查看次数

Django Social Auth 未重定向到下一个网址?

所以我想在登录后重定向到同一页面。但 Django Social Auth 不会重定向。它重定向到"/pins/#_=_

网页:

<a href="{% url socialauth_begin 'facebook' %}?next={{ request.get_full_path }}">Facebook Login</a>
Run Code Online (Sandbox Code Playgroud)

网址.py

url(r'^$', 'pinry.core.views.home', name='home')
Run Code Online (Sandbox Code Playgroud)

视图.py

def home(request):
    if request.user.is_authenticated():
        if 'next' in request.GET:
            return HttpResponseRedirect(request.GET['next'])
        else:    
            return HttpResponseRedirect('/pins'))
    return HttpResponseRedirect(reverse('core:concept'))
Run Code Online (Sandbox Code Playgroud)

设置.py

LOGIN_URL          = '/login-form/'
LOGIN_REDIRECT_URL = '/'
LOGIN_ERROR_URL    = '/login-error/'
SOCIAL_AUTH_LOGIN_REDIRECT_URL = '/'
SOCIAL_AUTH_NEW_USER_REDIRECT_URL = '/pins/'
SOCIAL_AUTH_NEW_ASSOCIATION_REDIRECT_URL = '/new-association-redirect-url/'
SOCIAL_AUTH_DISCONNECT_REDIRECT_URL = '/account-disconnected-redirect-url/'
SOCIAL_AUTH_BACKEND_ERROR_URL = '/new-error-url/'

SOCIAL_AUTH_COMPLETE_URL_NAME  = 'socialauth_complete'
SOCIAL_AUTH_ASSOCIATE_URL_NAME = 'socialauth_associate_complete'
SOCIAL_AUTH_DEFAULT_USERNAME = 'new_social_auth_user'

TEMPLATE_CONTEXT_PROCESSORS = (
...
    "social_auth.context_processors.social_auth_by_name_backends",
    'social_auth.context_processors.social_auth_backends',
    'social_auth.context_processors.social_auth_by_type_backends',
    'social_auth.context_processors.social_auth_login_redirect',
    "django.core.context_processors.csrf"
    )
Run Code Online (Sandbox Code Playgroud)

python django django-socialauth facebook-login

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

在Django Rest Framework中获取相关模型的值?

我希望以序列化形式获得产品的所有图像.我的模型如下.

class Product():
    title 
    subtitle 
    ...
class ProductImage():
    product = models.ForeignKey(
    'Product', related_name='images', verbose_name=_("Product"))
    image_path
Run Code Online (Sandbox Code Playgroud)

我的序列化器:

class ProductImageSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = ProductImage
        fields = ('caption', 'display_order', 'original', 'product')


class ProductSerializer(serializers.HyperlinkedModelSerializer):

    images = ProductImageSerializer()
    class Meta:
        model = Product
        fields = (
            'title', 'slug', 'short_description', 'description',
            'sku', 'pk', 'images')
Run Code Online (Sandbox Code Playgroud)

我收到了这个错误

尝试在序列化程序`ProductImageSerializer`上获取字段`display_order`的值时,/ api/products/Got AttributeError处的AttributeError.序列化程序字段可能命名不正确,并且不匹配`RelatedManager`实例上的任何属性或键.原始异常文本是:'RelatedManager'对象没有属性'display_order'.

如何获取特定产品的所有图像?

python django django-rest-framework

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

使用 OpenCV 计算视频文件中的帧数?

我正在尝试计算视频文件(“foo.h264”)中的总帧数。

>>> import numpy as nm
>>> import cv2
>>> cap = cv2.VideoCapture('foo.h264')
>>> cap.get(CV_CAP_PROP_FRAME_COUNT)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'CV_CAP_PROP_FRAME_COUNT' is not defined
>>> cap.get(5)
25.0
>>> cap.get(7)
-192153584101141.0
Run Code Online (Sandbox Code Playgroud)

所以我认为get(5)给出帧速率并get(7)给出帧总数。显然get(7)上述情况是不正确的。因此,为了验证我尝试在文件中查找这些值.avi

>>> cap = cv2.VideoCapture('foo.avi')
>>> cap.get(5)
29.97002997002997
>>> cap.get(7)
256379.0
Run Code Online (Sandbox Code Playgroud)

我可以通过乘以视频的持续时间来计算总帧数FPS,但我不确定给出的 FPS 是否.h264正确。为什么它给出的总帧数为负数?这是一个错误吗?
PS:这个视频文件是我.h264用树莓派摄像头录制的( )。

python opencv

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

MediaDefiningClass对象是不可迭代的?

尝试使用Inlines在Admin Dashboard上获取自定义视图.下面是代码

from django.contrib import admin # noqa
from oscar.core.loading import get_model
from oscar.apps.catalogue.admin import *

CategoryAttribute = get_model('catalogue', 'CategoryAttribute')
CategoryAttributeValue = get_model('catalogue', 'CategoryAttributeValue')
Category = get_model('catalogue', 'Category')

class CategoryAttributeInline(admin.TabularInline):
    model = CategoryAttributeValue
    fk_name = 'category'
    extra = 1

class CategoryAdmin(admin.ModelAdmin):
    inlines = [CategoryAttributeInline,]

admin.site.register(CategoryAttributeValue)
admin.site.register(CategoryAttribute)
admin.site.register(Category, CategoryAdmin)
Run Code Online (Sandbox Code Playgroud)

我得到的错误是 TypeError: 'MediaDefiningClass' object is not iterable

我的代码有什么问题?

python django django-oscar

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