小编vij*_*ker的帖子

乘以django模板

我正在循环推车项目,并希望将数量乘以单位价格,如下所示:

{% for cart_item in cart.cartitem_set.all %}
{{cart_item.quantity}}*{{cart_item.unit_price}}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)

有可能做那样的事吗?任何其他方式!谢谢

python django django-templates

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

通用 FormView 中的 Django Formset

您好,我在通用 FormView 中使用了表单集,如下所示:

class RequestRecommendationView(FormView):
    template_name = "account/stepfour.html"
    form_class = formset_factory(StepFourForm)

    def form_valid(self,form):
        print form.is_valid()
        cleaned_data = form.cleaned_data
        # return redirect(reverse("some_url_name"))
Run Code Online (Sandbox Code Playgroud)

formset_factory 的表单如下:

class StepFourForm(forms.Form):
    contact_person = forms.CharField(required=True)
    email = forms.EmailField(required=True)
    company = forms.CharField(required=True)
    request_message = forms.CharField(required=True)
Run Code Online (Sandbox Code Playgroud)

我的html结构是这样的:

<div style="padding-top:100px;padding-left:10px;">
    <h4> Request a Recommendation </h4>
    <form method="post" action="">
            {% csrf_token %}

            <table id="myForm">
                <tbody>
                {% for f in form %}
                    {% for field in f %}
                        <tr>
                            <td>{{field.label_tag}}</td>
                            <td>{{field}}</td>

                            {% for error in field.errors %}
                            <td><span>{{ error }}</span></td>
                            {% endfor …
Run Code Online (Sandbox Code Playgroud)

python django formset

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

模拟文件中导入的类的 __init__

我有一个文件 copy_x_to_y.py ,如下所示:

from abcd import F

def function_to_be_tested():
     F()
Run Code Online (Sandbox Code Playgroud)

在 abcd.py 文件中,我有这样的内容:

from xyz import XY

class F():
   def __init__(self, arg1):
       self.xy = XY(arg1)
Run Code Online (Sandbox Code Playgroud)

我想在我的测试用例中模拟 XY 的init 。我尝试用以下方法来模拟 F 的 init:

def mock_func(*args, **kwargs):
    pass

@patch('path/to/copy_x_to_y.F.__init__', mock_func)
def test():
    assert function_to_be_tested() is None
Run Code Online (Sandbox Code Playgroud)

但它总是会调用 XY 的 init,导致错误,因为它的初始化调用通过 arg1 与 S3 连接。如何测试这种结构?

python unit-testing mocking

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

基于类视图的装饰器

嗨,我有一个基于类的视图,我想用一些函数装饰它的调度方法,以便在args/kwargs的基础上,我将执行一些有用的东西.基于类的视图的代码如下:

from django.utils.decorators import method_decorator
class ProjectDetailView(FormMixin, DetailView):
    template_name = 'account/inner-profile-page.html'
    model = ProjectDetail
    form_class = CommentForm 
    context_object_name = 'project'

    @method_decorator(view_count)
    def dispatch(self, *args, **kwargs):
        return super(ProjectDetailView,self).dispatch(*args, **kwargs)

    def get_object(self, queryset=None):
        user = User.objects.get(user_slug=self.kwargs['user_slug'])
        title_slug = self.kwargs['title_slug'].replace(' ','-')
        return get_object_or_404(ProjectDetail, title_slug = title_slug, user=user)
Run Code Online (Sandbox Code Playgroud)

我的简化装饰器看起来像这样:

def view_count(func):
    def actual_decorator(*args, **kwargs):
        #do something useful here
        func(*args, **kwargs)
    return actual_decorator
Run Code Online (Sandbox Code Playgroud)

结果是"ProjectDetailView没有返回一个HttpResponse对象".我在哪里犯错,我应该怎么做,我知道它很简单,但这是我第一个做任何有用的装饰!

python django django-views

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

在sql中找到不同的对

当我遇到这个问题时,我正在尝试学习非对等连接。我有一张桌子流行音乐:

+------------+--------------+------+-----+---------+-------+
| Field      | Type         | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| country    | varchar(100) | YES  |     | NULL    |       |
| continent  | varchar(100) | YES  |     | NULL    |       |
| population | bigint(20)   | YES  |     | NULL    |       |
Run Code Online (Sandbox Code Playgroud)

我试图找到人口接近 100 的国家。

+------------+--------------+------+-----+---------+-------+
| Field      | Type         | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| country    | varchar(100) | YES  |     | NULL    |       |
| continent …
Run Code Online (Sandbox Code Playgroud)

mysql sql select join

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

描述在python中做了什么?

我正在浏览https://github.com/hit9/CURD.py/blob/master/CURD.py的代码,这是一个执行正常curd操作的简单orm ..我无法理解代码的一部分这(第616行):

 .....#smthing #..
 for name, attr in cls.__dict__.iteritems():
            if isinstance(attr, Field):
                attr.describe(name, cls)
                fields[name] = attr 
Run Code Online (Sandbox Code Playgroud)

attr.describe(attr,Field)做什么?我用Google搜索了但却一无所获.

python

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