Django WSGI Python编码(Django WSGI Python Encoding)
我的Django Apache Wsgi设置有一个奇怪的问题。 我最近将我的网站移动到另一台服务器。 不幸的是,现在编码有些搞砸了。 当我运行这样的命令时:
barcode.generate_barcode("ean", "1341341234234")
生成的图像将在每个数字之间显示一些特殊字符,通常用于某些内切问题。 我猜它使用两个字节而不是一个来表示每个char或类似的东西。
如果我使用django ./manage.py runserver命令运行相同的设置。 生成的图像很好,没有添加特殊字符。
所以我得出结论,这必须是我的apache2 / mod_wsgi设置。 但版本与我的旧版本相同,即debian 6,apache2.2,mod_wsgi 3.3.2。
我想尝试更新版本的mod_wsgi,但编译失败。
有人能指出我正确的方向,这个编码错误可能有其原因吗? 据我所知,WSGI上下文以某种方式使用错误的编码加载整个python脚本,否则我无法解释为什么硬编码字符串在图像上会出错。
在Apache conf.d / charset中:
AddDefaultCharset UTF-8
另外在我的vhost中:
AddDefaultCharset UTF-8
在apache2 / envvars中:
export LANG='en_US.UTF-8' export LC_ALL='en_US.UTF-8'
Envvars用于apache运行级别脚本:
if [ -z "$APACHE_ENVVARS" ] ; then APACHE_ENVVARS=$APACHE_CONFDIR/envvars fi
我还尝试在运行级别脚本中硬编码覆盖env变量。
在我的wsgi文件中:
import sys reload(sys) sys.setdefaultencoding('utf-8')
但似乎没有任何帮助。 我的图像仍然使用破碎的编码生成。
I have a strange issue with my Django Apache Wsgi setup. I recently moved my site to a different server. Unfortunately now the encoding is somewhat messed up. When I run a command like this:
barcode.generate_barcode("ean", "1341341234234")
the resulting image will show some special character between every digit, typically for some endocing issue. I guess it is using two bytes instead of one to represent each char or something similar.
If I run the same setup with django ./manage.py runserver comand. The resulting image is fine, no special characters added.
So I came to the conclusion this must be something with my apache2/mod_wsgi setup. But the versions are identical to my old setup, i.e. debian 6, apache2.2, mod_wsgi 3.3.2.
I would like to try a newer version of mod_wsgi but compilation fails.
Can anyone point me in the right direction to where this encoding error might have its cause? To my understanding the WSGI context somehow loads the entire python script with a wrong encoding, otherwise I cannot explain why a hardcoded string would turn out wrong on an image.
In Apache conf.d/charset:
AddDefaultCharset UTF-8
In my vhost additionally:
AddDefaultCharset UTF-8
In apache2/envvars:
export LANG='en_US.UTF-8' export LC_ALL='en_US.UTF-8'
Envvars is used in apache runlevel script:
if [ -z "$APACHE_ENVVARS" ] ; then APACHE_ENVVARS=$APACHE_CONFDIR/envvars fi
I also tried to hardcode overwrite env vars in the runlevel script.
In my wsgi file:
import sys reload(sys) sys.setdefaultencoding('utf-8')
But nothing seems to help. My image is still generated with broken encoding.
原文:https://stackoverflow.com/questions/17165169
最满意答案
将可选标签的高度约束设置为大于或等于您想要的任何高度。 另外,将标签的抗压缩性和内容拥抱设置为必需。 如果有更多内容,您的标签现在应该消耗额外的高度,或者只是采用设置的最小尺寸。
[label addConstraint:[NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0 constant:15/*the min height you need*/]]; [label setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical]; [label setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
Set the height constraint of the optional label as greater than or equal to whatever height you want. Also, set the compression resistance and content hugging of the label to required. Your label should now consume extra height if there is more content, or just take the minimum size that was set.
[label addConstraint:[NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0 constant:15/*the min height you need*/]]; [label setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical]; [label setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
相关问答
更多-
您可以重新加载特定行,因此它将调用tableview的高度更改heightForRorAtIndexpath的委托方法。 您可以在以编程方式重新加载特定行时设置高度1。 您可以存储要使其为零的临时indexpath 。 通过clipsToBounds=YES剪辑所有子clipsToBounds=YES 调用[self.tableView layoutIfNeeded]; You can reload particular row so it will call tableview's delegate me ...
-
我认为实现tableView的委托方法tableView(_ tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat可能会起作用。 I think implementing the tableView's delegate method tableView(_ tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath ...
-
下面的代码对我来说工作得很好,并且我得到了一个红色和空的集合视图。 Xcode 7 beta 4 - Swift 2.0 - AutoLayout syntax from iOS 9 override func viewDidLoad() { super.viewDidLoad() let collectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: UICollectionViewLayout()) ...
-
将可选标签的高度约束设置为大于或等于您想要的任何高度。 另外,将标签的抗压缩性和内容拥抱设置为必需。 如果有更多内容,您的标签现在应该消耗额外的高度,或者只是采用设置的最小尺寸。 [label addConstraint:[NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil attribute: ...
-
知道了,似乎我需要检查首选宽度复选框。 Got it, It seems I needed to check the preferred width checkbox.
-
同一行中的单元格总是具有相同的高度,即最高的单元格的高度。 什么都不做,没关系 如果您希望单元格宽度稍微调整,以便具有大量内容的单元格比其他单元格更宽更高,则删除table-layout: fixed从父表格中table-layout: fixed (默认情况下不存在) min-height在表中不起作用,你必须在这里使用height属性 你可以调整填充(内部边距)以获得更好的样式: th, td { padding: 4px 6px; } th, td { padding: 4px 6px; } 。 边距 ...
-
单元格使用AutoLayout时设置UICollectionViewCell大小(Setting UICollectionViewCell size when cell is using AutoLayout)[2022-10-02]
集合视图由布局对象工作,指定单元格的显式大小。 与表视图类似,您无法从单元格向外进行布局。 要实现您的设计,您需要使布局对象了解单元格需要基于其内容的大小。 有几种方法可以实现这一点,其中没有一种方法很好。 可能在您的情况下,您需要在布局无效的同时将新内容大小传回,并以某种方式将该索引路径的新大小返回到布局。 我也不会在您的网络视图上更改高度限制。 将其固定到单元格内容视图的边缘,并使其大小由布局对象发送的大小确定。 这样,你就不会受到冲突的限制。 A collection view works by th ... -
我还没有尝试过使用新的iOS 8机制。 但是当我使用iOS 6/7进行此操作时,我遇到了类似的问题。将应用程序更新到iOS 8后,它仍然可以正常工作,所以也许旧的方式仍然是最好的方法? 我在这里有一些代码示例: AutoLayout多行UILabel切断了一些文本 在这里: AutoLayout uitableviewcell在风景和iPad上计算基于肖像iPhone的高度 长话短说明iOS 8之前的方法是保留一个单元格的副本,只是为了计算tableView:heightForRowAtIndexPath: ...
-
使用自调整单元格时,应将两个表的rowHeight属性设置为UITableViewAutomaticDimension,并为两个表设置estimatedRowHeight。 您可以在viewDidLoad中执行此操作,无需实现heightForRowAtIndexPath。 When using self-sizing cells, you should set the rowHeight property of both tables to UITableViewAutomaticDimension, a ...
-
我最终使用以下代码: - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { static GCBaconCell *offscreenCell; if (!offscreenCell) { offscreenCell = [[GCBaconCell alloc] initWithStyle:UITableViewCellStyl ...