我有下面的功能。我想在 myModal 加载完成后调用该函数。这是我到目前为止。
(function ($) {
$.ajaxSetup({"error":function(XMLHttpRequest,textStatus, errorThrown) {
alert(textStatus);
alert(errorThrown);
alert(XMLHttpRequest.responseText);
}});
function get_fun() {
$.getJSON('/sms/fetch/', function(json) {
alert('json: ' + json + ' ...');
});
};
})(jQuery, window, document);
Run Code Online (Sandbox Code Playgroud)
在页面上:我现在如何调用我的函数?
<script src="/js/smart.js"></script>
<script>
$(document).ready(function() {
$('#myModal').on('shown', function() {
get_fun()
})
});
</script>
Run Code Online (Sandbox Code Playgroud)
我收到错误:ReferenceError: get_fun is not defined
我正在努力解决以下问题需要一些指导,下面你会看到我的form.py. 我有两个叫做group和的字段single.我需要对他们应用以下规则/验证......
因此,用户永远不能同时选择组和输入单个数字,但它们必须具有一个或另一个.希望有道理吗?
由于这些规则,我不能只添加required = true并需要某种自定义验证.这是自定义验证我遇到了问题.
任何人都可以根据我需要的验证形式给我一个例子吗?
谢谢.
Forms.py
class myForm(forms.ModelForm):
def __init__(self, user=None, *args, **kwargs):
super(BatchForm, self).__init__(*args, **kwargs)
if user is not None:
form_choices = Group.objects.for_user(user).annotate(c=Count('contacts')).filter(c__gt=0)
else:
form_choices = Group.objects.all()
self.fields['group'] = forms.ModelMultipleChoiceField(
queryset=form_choices, required=False
)
self.fields['single'] = forms.CharField(required=False)
Run Code Online (Sandbox Code Playgroud) I would like to use inheritance and have all my resources inheritance from a base resource class.
You will see what I have tried so far below. My issues is I now need to add in the meta class at it seems to overwrite at the moment. How can this be done?
class BasedModelResource(ModelResource):
class Meta:
authentication = ApiKeyAuthentication()
authorization = UserObjectsOnlyAuthorization()
class AccountResource(BasedModelResource):
"""
Account Object Resource
"""
class Meta:
queryset = Account.objects.all()
resource_name = 'account'
Run Code Online (Sandbox Code Playgroud) 我有一个使用FormView的类视图.我需要更改表单的名称,即这是我以前在旧功能视图中的名称:
upload_form = ContactUploadForm(request.user)
context = {'upload': upload_form,}
Run Code Online (Sandbox Code Playgroud)
在我的新视图中,我假设我可以使用get_context_data方法重命名但不确定如何.
How can I rename this form to **upload** not **form** as my templates uses `{{ upload }}` not `{{ form }}`? Thanks.
Run Code Online (Sandbox Code Playgroud)
当前班级观点:
class ImportFromFile(FormView):
template_name = 'contacts/import_file.html'
form_class = ContactUploadForm
def get_context_data(self, **kwargs):
"""
Get the context for this view.
"""
# Call the base implementation first to get a context.
context = super(ImportFromFile, self).get_context_data(**kwargs)
return context
Run Code Online (Sandbox Code Playgroud) 我正在尝试进行第一次测试.测试失败了:
DoesNotExist:联系人匹配查询不存在.查询参数为{'mobile':'07000000000'}
我似乎在设置功能中创建用户联系人,为什么它不可用?
谢谢
test.py
class BatchTestCase(TestCase):
def setup(self):
user = User.objects.get(username='glynjackson')
contact = Contact.objects.get(mobile="07000000000", contact_owner=user, group=None)
def test_get_contact(self):
contact = Contact.objects.get(mobile='07000000000')
self.assertEqual(contact.full_name(), 'Got Contact')
Run Code Online (Sandbox Code Playgroud)
完全错误
ERROR: test_get_contact (sms.tests.test_sms_simulation.BatchTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/user/Documents/workspace/example/sms/tests/test_sms_simulation.py", line 18, in test_get_contact
contact = Contact.objects.get(mobile='07000000000')
File "/Users/user/Documents/workspace/example/django-env/lib/python2.7/site-packages/django/db/models/manager.py", line 143, in get
return self.get_query_set().get(*args, **kwargs)
File "/Users/user/Documents/workspace/example/django-env/lib/python2.7/site-packages/django/db/models/query.py", line 389, in get
(self.model._meta.object_name, kwargs))
DoesNotExist: Contact matching query does not exist. Lookup parameters were {'mobile': '07000000000'}
Run Code Online (Sandbox Code Playgroud) 在TastyPie中,obj_create在我的表单验证之前运行,它似乎被跳过,为什么?
我的代码
class AccountCreateResource(ModelResource):
class Meta:
queryset = CompanyUser.objects.all()
resource_name = 'accounts/create'
allowed_methods = ['post']
validation = FormValidation(form_class=UserCreationForm)
def obj_create(self, bundle, request=None, **kwargs):
CompanyUser.objects.create_user(email=bundle.data['email'],
company=bundle.data['company'],
password=bundle.data['company'])
Run Code Online (Sandbox Code Playgroud) 我有以下JSON:
{
"meta": {
"limit": 20,
"next": null,
"offset": 0,
"previous": null,
"total_count": 0
},
"objects": []
}
Run Code Online (Sandbox Code Playgroud)
我对对象感兴趣:我想知道对象是否为空并显示警告:
这样的事情:
success: function (data) {
$.each(data.objects, function () {
if data.objects == None alert(0)
else :alert(1)
});
Run Code Online (Sandbox Code Playgroud) 我只是在学习Python和Django。
我只想获取以下字符串'col'的最终值,最终值始终是一个数字,即col1,col2等
用其他语言,我可以用多种方法来做...
left(value,3) - only leave the value after 3.
findreplace(value, 'col', '') - fine the string col replace with blank leaving nothing but the number I need.
Run Code Online (Sandbox Code Playgroud)
所以我的问题是, 在Django(Python)中,我该怎么做? (在视图中不是模板)
Django也严格吗?我需要对值进行整型以使其成为数字吗?
因此,下面是我在观看 Django Con EU 谈话视频后通过基于第一类的视图创建的。
它有效并且理解它的作用。我不明白基于类的视图和我刚刚构建的通用类基视图之间的区别?
class GroupListView(ListView):
"""
List all Groups.
"""
context_object_name = 'groups'
template_name = 'contacts/home.html'
def get_context_data(self, **kwargs):
"""
Get the context for this view.
"""
# Call the base implementation first to get a context.
context = super(GroupListView, self).get_context_data(**kwargs)
# Add more contexts.
context['tasks'] = Upload.objects.filter(uploaded_by=self.request.user).order_by('-date_uploaded')[:5]
context['unsorted'] = Contact.objects.unsorted_contacts(user=self.request.user).count()
return context
def get_queryset(self):
"""
Get the list of items for this view. This must be an iterable, and may
be a queryset (in which …Run Code Online (Sandbox Code Playgroud) 这也许很简单,但是我不确定要搜索什么。
我想做这样的事情:
temp = ""
for item in instance:
temp = temp + item
Run Code Online (Sandbox Code Playgroud)
我想我以前看过这样:
temp ++ item
Run Code Online (Sandbox Code Playgroud)
但这不起作用。