小编Ada*_*ver的帖子

'Model'对象不可迭代

在作者模型中:

century = models.ManyToManyField(Century)
Run Code Online (Sandbox Code Playgroud)

在视图中:

a = get_object_or_404(Author.objects, id=id)

s = Author.objects.filter(century__in=a).order_by('?')[:3]
Run Code Online (Sandbox Code Playgroud)

错误:

异常值:'Author'对象不可迭代

怎么了?作者可能属于两个世纪,我希望从他的世纪/世纪中获得3位随机作者.

python django

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

从HTML字符串中删除所有div标记

我试图去掉所有的div.

输入:

<p>111</p>

<div class="1334">bla</div>

<p>333</p>

<p>333</p>

<div some unkown stuff>bla2</div>
Run Code Online (Sandbox Code Playgroud)

期望的输出:

   <p>111</p>

    <p>333</p>

    <p>333</p>
Run Code Online (Sandbox Code Playgroud)

我试过这个,但它不起作用:

release_content = re.sub("/<div>.*<\/div>/s", "", release_content)
Run Code Online (Sandbox Code Playgroud)

python regex

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

在中间件安全中通过 set_urlconf 和 request.urlconf 修改 Django urlconf 吗?

我正在根据请求的主机名更改中间件中的默认 urlconf,并且它在开发过程中按预期工作,但是我担心赛车/线程,因为我在运行时修改了 Django 设置!

我担心的是 Django 会将 urlconfs 与许多并发请求混淆。这是一个有效的担忧吗?

python django multithreading

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

主要字段名称(document = True)

Django Haystack 文档说:

**Warning**
When you choose a document=True field, it should be consistently named across all of your SearchIndex classes to avoid confusing the backend. The convention is to name this field text.

There is nothing special about the text field name used in all of the examples. It could be anything; you could call it pink_polka_dot and it won’t matter. It’s simply a convention to call it text.
Run Code Online (Sandbox Code Playgroud)

但我不明白这意味着什么.这是他们的示例模型:

从myapp.models import中的haystack导入索引导入datetime

class NoteIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, …
Run Code Online (Sandbox Code Playgroud)

python django django-haystack

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

如何使用Bootstrap 3将中间列放在小屏幕上?

以下代码显示" 1 2 3 ":

<div class="row">
    <div class="col-md-3">
        1
    </div>
    <div class="col-md-6">
        2
    </div>
    <div class="col-md-3">
        3
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

如何在较小的屏幕(手机)上放置主要内容; " 2 1 3 "?

css twitter-bootstrap twitter-bootstrap-3

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

为什么没有检测到网络连接更改?

我试图在连接状态发生变化时显示并发出警报但我的代码根本没有效果(下面的警报没有被执行).

就这个:

var app = {
    // Application Constructor
    initialize: function() {
        this.bindEvents();
    },
    // Bind Event Listeners
    //
    // Bind any events that are required on startup. Common events are:
    // 'load', 'deviceready', 'offline', and 'online'.
    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    },
    // deviceready Event Handler
    //
    // The scope of 'this' is the event. In order to call the 'receivedEvent'
    // function, we must explicity call 'app.receivedEvent(...);'
    onDeviceReady: function() {
        app.receivedEvent('deviceready');

        document.addEventListener("online", onOnline, false);
        document.addEventListener("offline", onOffline, false); …
Run Code Online (Sandbox Code Playgroud)

android network-connection cordova

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

Laravel dingo/api定制变压器

我正在尝试使用dingo api(https://github.com/dingo/api/wiki/Transformers#custom-transformation-layer)为我的Post模型实现一个自定义转换器,我得到了这个例外:

缺少PostTransformer :: transform()的参数2,在第298行的/home/.../vendor/league/fractal/src/Scope.php中调用并定义

我的控制器:

$post = Post::findOrFail(2);

return $this->item($post, new PostTransformer);
Run Code Online (Sandbox Code Playgroud)

我的PostTransformer课程:

<?php

use Illuminate\Http\Request;
use Dingo\Api\Transformer\Binding;
use Dingo\Api\Transformer\TransformerInterface;

class PostTransformer implements TransformerInterface
{
    public function transform($response, $transformer, Binding $binding, Request $request)
    {
        // Make a call to your transformation layer to transformer the given response.

        return [
            'kkk' => 'val'
        ];

    }
}
Run Code Online (Sandbox Code Playgroud)

怎么了?

php api rest restful-architecture laravel

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

实现直到作为宏

这是我失败的尝试:

(defmacro until
  [condition body setup increment]
  `(let [c ~@condition]
    (loop [i setup]
      (when (not c)
        (do
          ~@body
          (recur ~@increment))))))

(def i 1)

(until (> i 5)
  (println "Number " i)
  0
  (inc i))
Run Code Online (Sandbox Code Playgroud)

我得到:CompilerException java.lang.RuntimeException:不能让限定名:clojure-noob.core/c

我期待这个输出: Number 1 Number 2 Number 3 Number 4 Number 5

怎么了?

macros clojure

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

在列表外的Django Admin中显示UserProfile字段

我有我的phone领域UserProfile.如何将其显示在Django Admin › Auth › Users外部列表中(列表显示) - 而不是记录内部?

我现在有:

class UserAdmin(UserAdmin):
    list_display = ('email', 'first_name', 'last_name', 'userprofile__phone')
    inlines = (UserProfileInline,)


# Re-register UserAdmin
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
Run Code Online (Sandbox Code Playgroud)

userprofile__phone无法识别.

python django django-admin

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

get_context_data 没有被调用

@method_decorator(login_required, name='dispatch')
class BaseView(TemplateView):
    template_name = '...html'

    def dispatch(self, request, *args, **kwargs):
        # ...

        return super(BaseView, self).dispatch(request, *args, **kwargs)


class ConfigureView(BaseView):
    form_class = Form
    template_name = 'configure.html'

    def get(self, request, *args, **kwargs):
        form = self.form_class(user=request.user)

        return render(request, self.template_name, {
            'form': form
        })

    def post(self, request, *args, **kwargs):
        form = self.form_class(request.POST, user=request.user)

        if form.is_valid():
            form.save()

            return redirect('...')

        return render(request, self.template_name, {'form': form})

    def get_context_data(self, **kwargs):
        print('**********')  # Never printed

        context = super(ConfigureView, self).get_context_data(**kwargs)

        context['app'] = App.objects.get(slug=kwargs['slug'])

        return context
Run Code Online (Sandbox Code Playgroud)

为什么?我想我正在关注文档。

python django

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

Golang 模板顺序

有没有办法使模板顺序无关紧要。

这是我的代码:

var overallTemplates = []string{
    "templates/analytics.html",
    "templates/header.html",
    "templates/footer.html"}

func HomeHandler(w http.ResponseWriter, r *http.Request) {
    render(w,
        append([]string{"templates/home.html"}, overallTemplates...),
        nil)
}

func render(w http.ResponseWriter, files []string, data interface{}) {
    tmpl := template.Must(template.ParseFiles(files...))
    err := tmpl.Execute(w, data)

    if err != nil {
        fmt.Printf("Couldn't load template: %v\n", err)
    }
}
Run Code Online (Sandbox Code Playgroud)

它有效,但如果我将顺序更改overallTemplates为:

var overallTemplates = []string{
    "templates/header.html",
    "templates/footer.html",
    "templates/analytics.html"}
Run Code Online (Sandbox Code Playgroud)

我得到了一个空白页面,因为analytics.html内容是一样的东西{{define "analytics"}}...{{end}}和它是由被称为footer.html{{define "footer"}}{{template "analytics"}} ...{{end}}

go go-templates

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

动态设置name属性

可能重复:
getElementByClass().setAttribute不起作用

为什么这个:

document.getElementsByClassName('cke_source').setAttribute('name', "mymessage") 
Run Code Online (Sandbox Code Playgroud)

回来了:

TypeError: Object #<NodeList> has no method 'setAttribute'
Run Code Online (Sandbox Code Playgroud)

document.getElementsByClassName('cke_source') 正确地返回对象.

  • 请不要jQuery.

javascript

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

Python中的单词匹配

我有这个,但它正在进行部分匹配:

for il in ignore_list:
    if il.word in title or il.word in text:
        return True
Run Code Online (Sandbox Code Playgroud)

我怎么才能匹配整个单词?

python django

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