小编fer*_*ngb的帖子

如何在django-allauth上自定义activate_url?

我使用Django和Django REST框架作为后端,AngularJS作为前端.

对于用户管理,我使用的是django-rest-auth,它使用django-allauth进行用户管理.作为基础,我使用了django-rest-auth的demo.

问题出在注册后,当您尝试验证通过激活网址发送电子邮件的电子邮件时: 127.0.0.1:8000/account/confirm-email/yhca8kmijle0ia7k3p7ztbvnd2n1xepn9kyeuaycmlzll5xw19ubjarnvjrot7eu/

其中*127.0.0.1:8000是Django后端.

但在我的情况下,它应该发送类似的URL localhost:9000/#/verifyEmail/akv2dcvrfnk9ex5fho9jk0xx1ggtpfazmi8sfsoi2sbscoezywfp7kzcyqnizrc0.所以这个验证将从前端完成,localhost:9000我的AngularJS前端在哪里.

有没有办法在django-allauth上定制activate_url?

django django-urls angularjs django-rest-framework django-allauth

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

Django Haystack:模板和模型索引的职责

我已经浏览了文档,我甚至创建了一些搜索后端,但我仍然对这些事情在haystack中做了什么感到困惑.搜索结束时搜索你在类中继承的字段indices.SearchIndex,indexes.Indexable,或者是后端搜索模板中的文本?有人能解释一下吗?

在django haystack中,您将创建一个类来定义应该查询的字段(以及我理解它的方式),如下所示:

class ProductIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)
    name = indexes.CharField(model_attr='title', boost=1.75)
    description = indexes.CharField(model_attr='description')
    short_description = indexes.CharField(model_attr='short_description')

    def get_model(self):
        return Product

    def index_queryset(self, using=None):
        """Used when the entire index for model is updated."""
        return self.get_model().objects.filter(active=True,
                                           published_at__lte=datetime.now())
Run Code Online (Sandbox Code Playgroud)

您还将创建一个可以执行某项操作的模板txt - 我不确定是什么.我知道在搜索算法期间搜索后端将覆盖此模板.

{{ object.name }}
{{ object.description }}
{{ object.short_description }}
{% for related in object.related %}
    {{ related.name }}
    {{ related.description }}
{% endfor %}

{% for category in object.categories.all %}
    {% if category.active %}
        {{ category.name }}
    {% …
Run Code Online (Sandbox Code Playgroud)

django django-templates django-haystack

7
推荐指数
1
解决办法
1452
查看次数

django:value的日期格式无效.它必须是YYYY-MM-DD格式

我有一个问题,
我使用xpath item['releaseday']从网站获取 .
当xpath没有得到价值

它会导致错误:

django.core.exceptions.ValidationError: [u"'' value has an invalid date format. It must be in YYYY-MM-DD format."]
Run Code Online (Sandbox Code Playgroud)

在我的models.py中我设置null = True和blank = True似乎不起作用

releaseday      = models.DateField(null=True,blank=True)    
Run Code Online (Sandbox Code Playgroud)

我尝试添加此代码,但无法正常工作

    if 'releaseday' not in item:
        item['releaseday']=''   
Run Code Online (Sandbox Code Playgroud)

怎么编辑???
请指导我,谢谢

python django date

7
推荐指数
1
解决办法
6538
查看次数

Django Rest Framework身份验证被拒绝

我使用PSA在Django上验证了我的用户并让用户在用户模型中注册,甚至令牌模型都注册了用户和令牌.

但是当我发送此请求时:

curl -X POST -H "Authorization:Token 87e939184457ccc064485444a90e3ebf417xxxxx" http://192.168.x.x:8000/user-profiles/>error.html 
Run Code Online (Sandbox Code Playgroud)

我明白了

{"detail":"您无权执行此操作."}

如果我寄这个:

curl -X POST --user "VedantDasSwain:87e939184457ccc064485444a90e3ebf417xxxxx" http://192.168.x.x:8000/user-profiles/>error.html 
Run Code Online (Sandbox Code Playgroud)

我明白了

{"detail":"用户名/密码无效"}

这些是我的设置文件中的相关代码段:

'DEFAULT_PERMISSION_CLASSES': ('rest_framework.permissions.IsAuthenticated',
        'rest_framework.permissions.IsAdminUser',),
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.BasicAuthentication',
        'rest_framework.authentication.SessionAuthentication',
        'rest_framework.authentication.TokenAuthentication',
    ),
Run Code Online (Sandbox Code Playgroud)

这是PSA设置的一部分:

AUTHENTICATION_BACKENDS = (
    'social.backends.facebook.FacebookOAuth2',

    'django.contrib.auth.backends.ModelBackend',
)
Run Code Online (Sandbox Code Playgroud)

有没有人遇到过这种类型的东西?这是什么解决方案?

python django django-rest-framework

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

Django - 防止模板中可用语言名称的翻译

我在 settings.py 中有可用的语言

LANGUAGE_CODE = 'en'
LANGUAGES = (
    ('fr', _('French')),
    ('en', _('English')),
)
Run Code Online (Sandbox Code Playgroud)

当我在模板中循环时,我认为 django 通过 {{ lang.1 }} 为我提供翻译名称是预期的行为。但我不想翻译语言名称,所以我更改了我的settings.py,如下所示:

LANGUAGES = (
        ('fr', 'Francais'),
        ('en', 'English'),
    )
Run Code Online (Sandbox Code Playgroud)

我仍然得到翻译后的语言名称。你有好主意吗?{% get_available_languages as languages %} 模板标签是否会自动翻译列表项?如果是这样,我如何在可用语言循环时使用未翻译的语言名称?

- - 编辑 - -

我检查了django的get_available_languages模板标签的代码。我想这里翻译的是:

class GetAvailableLanguagesNode(Node):
    def __init__(self, variable):
        self.variable = variable

    def render(self, context):
        context[self.variable] = [(k, translation.ugettext(v)) for k, v in settings.LANGUAGES]
        return ''
Run Code Online (Sandbox Code Playgroud)

也许我必须编写自己的模板标签......

django django-i18n

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

使用Django REST Framework的TokenAuthentication在查询字符串中进行标记

在使用Django REST Framework构建的API中,可以使用TokenAuthentication方法进行身份验证.它的文档说应该通过Authorization标头发送身份验证令牌.

通常,人们可以通过查询字符串发送API密钥或令牌以进行身份​​验证,例如https://domain.com/v1/resource?api-key=lala.

有没有办法对Django REST Framework的TokenAuthentication做同样的事情?

python authentication django django-rest-framework

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

Django 在序列化器的自定义字段上执行 Rest Order?

我正在尝试使用 Django Rest 根据自定义字段的排序返回模型的 json 表示形式,该自定义字段未附加到模型,但附加到序列化器。我知道如何使用模型特定字段执行此操作,但是当该字段仅在序列化器类中时,如何使用 django rest 返回排序?我想返回按“分数”排序的图片列表。谢谢!

------视图.py

class PicList(generics.ListAPIView):
    queryset = Pic.objects.all()
    serializer_class = PicSerializerBasic
    filter_backends = (filters.OrderingFilter,)
    ordering = ('score')
Run Code Online (Sandbox Code Playgroud)

------序列化器.py

class PicSerializer(serializers.ModelSerializer):
    userprofile = serializers.StringRelatedField()
    score = serializers.SerializerMethodField()

    class Meta:
        model = Pic
        fields = ('title', 'description', 'image', 'userprofile', 'score')
        order_by = (('title',))

    def get_score(self, obj):
        return Rating.objects.filter(picc=obj).aggregate(Avg('score'))['score__avg']
Run Code Online (Sandbox Code Playgroud)

python django serialization django-rest-framework

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

在 Django 中测试页面内容中的链接

再会!

所以去我的问题,

我有一个带有一些链接的模板,我想断言它们确实存在于我的页面中。

我的 html 部分,

<section>
    <p>Welcome to open radio. We help you get on air!</p>
    <p><a href="{% url 'userlogin' %}">Login</a></p>
    <p><a href="{% url 'liststations' %}">View all our stations</a></p>
    <p><a href="{% url 'userregistration' %}">SignUp!</a></p>      
</section>
Run Code Online (Sandbox Code Playgroud)

我现在的测试,

response = self.client.get(reverse("home"))
assert reverse("userlogin") in response.content
assert reverse("liststations") in response.content
assert reverse("userregistration") in response.content
Run Code Online (Sandbox Code Playgroud)

我的测试通过了,我明白我并不是真正为这里的链接断言,而是为 URL 字符串断言。我如何专门测试链接?

django django-testing python-2.7

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

在Python 2.7.3中为numpy数组指定字段名称

我很担心这一点,因为我显然错过了重点,解决方案太简单了:(

我有一个带有x列的np.array,我想指定一个字段名称.所以这是我的代码:

data = np.array([[1,2,3], [4.0,5.0,6.0], [11,12,12.3]])
a = np.array(data, dtype= {'names': ['1st', '2nd', '3rd'], 'formats':['f8','f8', 'f8']})
print a['1st']
Run Code Online (Sandbox Code Playgroud)

为什么会这样

[[  1.    2.    3. ]
 [  4.    5.    6. ]
 [ 11.   12.   12.3]]
Run Code Online (Sandbox Code Playgroud)

而不是[1, 2, 3]?

python arrays numpy user-defined-types python-2.7

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

打印出第一行以外的csv行

我想用Python读取一个CSV文件,然后打印出除第一行之外的每一行.

我知道如何打印出所有行:

with open('myfile.csv', 'rb') as csvfile:
    reader = csv.reader(csvfile, delimiter=',')
    for row in reader:
        print row
Run Code Online (Sandbox Code Playgroud)

我能想到的不打印第一行的唯一方法是:

with open('myfile.csv', 'rb') as csvfile:
    reader = csv.reader(csvfile, delimiter=',')
    for i, row in enumerate(reader):
        if i != 0:
            print row
Run Code Online (Sandbox Code Playgroud)

但这似乎并不优雅.还有其他方法吗?

python csv

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