小编Sud*_*fle的帖子

定义具有太多参数的函数以遵守PEP8标准

我已经定义了一个带有很长参数列表的函数.定义中的总字符数大于80,不符合PEP8.

def my_function(argument_one, argument_two, argument_three, argument_four, argument_five):
Run Code Online (Sandbox Code Playgroud)

什么是避免水平滚动的最佳方法.

python pep8

57
推荐指数
6
解决办法
3万
查看次数

在Django REST框架序列化程序中动态排除或包含字段

我在Django REST框架中有一个序列化程序,定义如下:

class QuestionSerializer(serializers.Serializer):
    id = serializers.CharField()
    question_text = QuestionTextSerializer()
    topic = TopicSerializer()
Run Code Online (Sandbox Code Playgroud)

现在我有两个使用上述序列化程序的API视图:

class QuestionWithTopicView(generics.RetrieveAPIView):
    # I wish to include all three fields - id, question_text
    # and topic in this API.
    serializer_class = QuestionSerializer

class QuestionWithoutTopicView(generics.RetrieveAPIView):
    # I want to exclude topic in this API.
    serializer_class = ExamHistorySerializer
Run Code Online (Sandbox Code Playgroud)

一种解决方案是编写两个不同的序列化器.但必须有一个更容易的解决方案来有条件地从给定的序列化器中排除一个字段.

python django serialization django-serializer django-rest-framework

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

获取lxml中元素的内部HTML

我试图在Python中使用lxml和xpath获取子节点的HTML内容.如下面的代码所示,我想找到每个产品节点的html内容.它有像product.html这样的方法吗?

productGrids = tree.xpath("//div[@class='name']/parent::*")
for product in productGrids:
    print #html content of product
Run Code Online (Sandbox Code Playgroud)

python xpath lxml

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

如何使用Regex指定长URL模式,以便它们遵循PEP8指南

我在Django中有一个长网址模式,类似于:

url(r'^(?i)top-dir/(?P<first_slug>[-\w]+?)/(?P<second_slug>[-\w]+?)/(?P<third_slug>[-\w]+?).html/$',
    'apps.Discussion.views.pricing',
Run Code Online (Sandbox Code Playgroud)

绝对不会遵循PEP8指南,因为字符在一行中超过80.我找到了解决这个问题的两种方法:

第一个(使用反斜杠):

   url(r'^(?i)top-dir/(?P<first_slug>[-\w]+?)/(?P<second_slug>[-\w]+?)'\
       '/(?P<third_slug>[-\w]+?).html/$',
       'apps.Discussion.views.pricing',
Run Code Online (Sandbox Code Playgroud)

第二个 - 使用():

 url((r'^(?i)top-dir/(?P<first_slug>[-\w]+?)/(?P<second_slug>[-\w]+?)',
      r'/(?P<third_slug>[-\w]+?).html/$'),
      'apps.Discussion.views.pricing'),  
Run Code Online (Sandbox Code Playgroud)

它们都被正则表达式打破.有没有更好的方法来解决这个问题.或者为网址编写这么长的正则表达式是不好的做法.

python django pep8

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

无法使用docker-compose安装Python包

我正在使用docker运行Django项目.现在我想在Docker容器中安装Python包并运行以下命令:

docker-compose django run pip install django-extra-views

现在,当我这样做时docker-compose up,我得到一个错误ImportError: No module named 'extra_views'.docker-compose django run pip freeze也没有显示上述包装.

我错过了什么吗?

python docker docker-compose

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

在Web应用程序中使用Dynamodb进行分页

我的数据库中有大约5000多个视频,我创建了一个页面http://mysite.com/videos列出所有视频.现在我正在实施分页,因此每页只列出20个视频.例如

http://mysite.com/videos?page=1显示前20个视频,http://mysite.com/videos?page = 2显示接下来的20个视频.

我在选择实现分页的最佳方法时遇到问题.我想过每次执行新页面时都使用table.scan(),然后根据Python代码的某些逻辑选择只需要.但这似乎相当昂贵.

我正在使用Python/Django和boto库.

python django boto amazon-web-services amazon-dynamodb

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

远程模式不在引导程序3中显示页眉和页脚

我正在使用Bootrap 3和远程模式来加载远程页面.代码类似于文档中指定的代码.页面的内容以模态加载,但是模态标题和包含关闭按钮的模态页脚未显示.

触发:

<a data-toggle="modal" data-remote="remote.html" video.id %}" 
data-target="#myModal"><button class="btn btn-success" >Edit details </button></a>
Run Code Online (Sandbox Code Playgroud)

莫代尔:

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                 <h4 class="modal-title">Modal title</h4>

            </div>
            <div class="modal-body"><div class="te"></div></div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <button type="button" class="btn btn-primary">Save changes</button>
            </div>
        </div>
        <!-- /.modal-content -->
    </div>
    <!-- /.modal-dialog -->
</div>
<!-- /.modal -->
Run Code Online (Sandbox Code Playgroud)

remote.html:

<p style="background-color:#ffffff">My name is duip </p>
Run Code Online (Sandbox Code Playgroud)

html css jquery twitter-bootstrap twitter-bootstrap-3

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