在站点性能/速度方面,链接到jquery是否更好,如下所示:
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
Run Code Online (Sandbox Code Playgroud)
或者将文件放在服务器上并从那里链接到它们,如下所示:
<script src="js/jquery-1.9.1.js"></script>
Run Code Online (Sandbox Code Playgroud) 使用的技术:
http://www.django-rest-framework.org
例外: http: //www.django-rest-framework.org/api-guide/exceptions/
在自定义Exceptions.py文件中包含rest_framework默认示例:
from rest_framework.views import exception_handler
import sys
def custom_exception_handler(exc, context=None):
# Call REST framework's default exception handler first,
# to get the standard error response.
response = exception_handler(exc)
# Now add the HTTP status code to the response and rename detail to error
if response is not None:
response.data['status_code'] = response.status_code
response.data['request'] = request
response.data['error'] = response.data.get('detail')
del response.data['detail']
return response
Run Code Online (Sandbox Code Playgroud)
这会发送基本错误信息,如“Http404”等,但不会发送请求数据,如 IP 地址等。
将我的请求添加到响应中的最佳方式?提前致谢。
更新(并解决):
因此,我最初尝试使用 DjangoRestFramework 2.4.x 解决此问题,但该版本没有自定义异常处理程序的请求或上下文数据选项。升级到 3.1.3 可以轻松地将数据添加到响应中。新代码现在如下所示(使用版本 3.1.3):
def custom_exception_handler(exc, …Run Code Online (Sandbox Code Playgroud) python django error-handling django-errors django-rest-framework
在我更新到Xcode 7后,我收到此错误"类型的值UIViewController没有成员topViewController." 其他人遇到这个?是topViewController不再的一部分UIViewController?
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.leftBarButtonItem = self.editButtonItem()
let addButton = UIBarButtonItem(barButtonSystemItem: .Search,
target: self, action: "addButtonPressed:")
self.navigationItem.rightBarButtonItem = addButton
if let split = self.splitViewController {
let controllers = split.viewControllers
self.detailViewController =
controllers[controllers.count-1].topViewController as? ### Error heres
DetailViewController
}
model = Model(delegate: self) // create the Model
model.synchronize() // tell model to sync its data
self.navigationController?.toolbarHidden = false;
self.navigationItem.backBarButtonItem = UIBarButtonItem(title:"Home", style:.Plain, target:nil, action:nil)
}
Run Code Online (Sandbox Code Playgroud) 技术:Objective-C,xCode 7
@property (nonatomic, copy) NSString *notes;
@synthesize notes;
example.notes = @"Click <a href='http://www.google.com'>here</a>";
NSString *msg = [@"" stringByAppendingFormat:@"%@", myAnnotation.notes];
UIAlertController *alertViewController = [UIAlertController alertControllerWithTitle: @"Title of Alert Box" message: msg preferredStyle: UIAlertControllerStyleAlert];
[alertViewController addAction: [UIAlertAction actionWithTitle: @"Close" style: UIAlertActionStyleCancel handler: nil]];
[self presentViewController: alertViewController animated: YES completion: nil];
Run Code Online (Sandbox Code Playgroud)
试图使链接出现在警报框中,但没有运气。只是输出为纯文本。
这有可能吗?如果是这样,使用上面的示例,您将如何实现它?
所以,我在我的一个react.js文件中做了类似的事情:
render: function() {
var stuff = this.props.stuff;
function getHtmlForKey(key_name, title) {
var key_name = key_name.toLowerCase();
return Object.keys(thing).filter(function(key) {
return key && key.toLowerCase().indexOf(key_name) === 0;
}).map(function(key, index) {
var group_title = index === 0 ? title : '';
if(profile[key] != null && thing[key] != "") {
return (
<span>
{group_title ? (
<li className="example-group-class">{group_title}</li>
) : null }
<li key={key} className="example-class">
{thing[key]}
</li>
</span>
);
}
}, this);
}
/** Grouping by keyword **/
var group1 = getHtmlForKey('stuff1', 'Group Title …Run Code Online (Sandbox Code Playgroud) 因此,我正在使用Django的Model Formset生成用于不同数据的表单集。效果很好,但是我想添加一个功能,当用户显示表单集并更新10个项目中的2个时,我只能跟踪更新的2个项目,并输出类似“您已更新2个项目”的消息“ 之类的事情。
Django Model Formsets是否为此具有内置API?我似乎在Django Docs上找不到它。
我尝试了各种方法,但是在使用下面的Peter提供的代码时仍然会遇到这种情况:
'Attendance' object has no attribute 'has_changed.'
Run Code Online (Sandbox Code Playgroud)
如果我将form.has_changed切换为formset.has_changed(),我会得到
'list' object has no attribute 'has_changed'
Run Code Online (Sandbox Code Playgroud)
我的查看和发布方法
class AttendanceView(TemplateView):
template_name = 'example.html'
def changed_forms(self, formset):
return sum(1 for form in formset if form.has_changed())
def post(self, request, *args, **kwargs):
formset = AttendanceFormSet(request.POST)
if formset.is_valid():
formset = formset.save()
forms_changed = self.changed_forms(formset)
context = self.get_context_data(**kwargs)
context['total_changed_forms'] = forms_changed
return self.render_to_response(context)
else:
return HttpResponse("POST failed")
Run Code Online (Sandbox Code Playgroud)
所以我想通了,只需更改:
formset = formset.save()
Run Code Online (Sandbox Code Playgroud)
至
formset.save()
Run Code Online (Sandbox Code Playgroud) 所以,我正在使用Django update_or_create API来构建我的表单数据.它工作得很好......但是,一旦构建,我需要一种方法来检查实际更新的配置文件或它们是否是第一次创建的?
举个例子:
for people in peoples:
people, updated = People.objects.update_or_create(
id=people.person_id,
defaults={
'first_name': people.first_name,
}
)
Run Code Online (Sandbox Code Playgroud)
过滤查询集:
people = People.objects.filter(
id__in=whatever,
)
Run Code Online (Sandbox Code Playgroud)
但是,现在,我正在尝试通过创建或更新来过滤查询集...但是没有看到它的API(例如,各种各样的拟合)
所以,我想做的事情如下:
updated = Person.objects.filter(updated=True, created_for_first_time=False)
and then I can write something like
if updated:
do this
else:
do this
Run Code Online (Sandbox Code Playgroud)
基本上,我只想检查配置文件是否第一次更新或创建.
这对我来说失败了。它给了我一个“405 方法不允许的错误”。它指的是POST方法是吗?如何检查 POST?或者我应该做一些完全不同的事情?
class StuffList(ListView):
template_name = "list.html"
queryset = Stuff.objects.all().order_by('-whatever')
context_object_name = 'stuff'
def get(self, request, *args, **kwargs):
if request.POST:
q = request.POST.get('q')
stuff = Stuff.objects.filter(user__icontains=stuff)
return render(request, self.template_name, {'stuff': stuff, 'q': q })
Run Code Online (Sandbox Code Playgroud)
在我的表单中,我将令牌放在表单操作中,如下所示:
<form action="/stuff/" method="post" name="q">
{% csrf_token %}
Run Code Online (Sandbox Code Playgroud) 到目前为止,我只有默认的MKMapView引脚工作.但我想要一个自定义图像来取代那个小红色别针.这就是我在下面的内容.我错过了什么?我想定义view.image会做的伎俩,但我猜不是.
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:
(id<MKAnnotation>)annotation {
MKPinAnnotationView *view = [[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:@"pin"];
view.enabled = YES;
view.animatesDrop = YES;
view.canShowCallout = YES;
view.image = [UIImage imageNamed:@"custom-pin-image.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage
imageNamed:@"custom-image-for-annotation.png"]];
view.leftCalloutAccessoryView = imageView;
view.rightCalloutAccessoryView = [UIButton buttonWithType:
UIButtonTypeDetailDisclosure];
return view;
}
Run Code Online (Sandbox Code Playgroud) django ×4
python ×4
django-forms ×2
django-views ×2
ios ×2
objective-c ×2
ios9 ×1
javascript ×1
jquery ×1
mapkit ×1
modelform ×1
reactjs ×1
swift ×1
swift2 ×1
xcode6 ×1
xcode7 ×1