在作者模型中:
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位随机作者.
我试图去掉所有的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) 我正在根据请求的主机名更改中间件中的默认 urlconf,并且它在开发过程中按预期工作,但是我担心赛车/线程,因为我在运行时修改了 Django 设置!
我担心的是 Django 会将 urlconfs 与许多并发请求混淆。这是一个有效的担忧吗?
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) 以下代码显示" 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 "?
我试图在连接状态发生变化时显示并发出警报但我的代码根本没有效果(下面的警报没有被执行).
就这个:
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) 我正在尝试使用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)
怎么了?
这是我失败的尝试:
(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
怎么了?
我有我的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无法识别.
@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)
为什么?我想我正在关注文档。
有没有办法使模板顺序无关紧要。
这是我的代码:
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}}
为什么这个:
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') 正确地返回对象.
我有这个,但它正在进行部分匹配:
for il in ignore_list:
if il.word in title or il.word in text:
return True
Run Code Online (Sandbox Code Playgroud)
我怎么才能匹配整个单词?