小编jap*_*ape的帖子

Django 1.9中的新url格式

我最近将我的Django项目升级到1.9版.

当我尝试运行时migrate,我收到以下两个错误:

  1. Support for string view arguments to url() is deprecated and will be removed in Django 1.10 (got app.views.about). Pass the callable instead.
  2. django.conf.urls.patterns() is deprecated and will be removed in Django 1.10. Update your urlpatterns to be a list of django.conf.urls.url() instances instead.

有人可以告诉我如何做到这一点的正确语法?我的简短示例urls.py如下:

urlpatterns = patterns('',
    url(r'^about/$', 'app.views.about',
        name='about'),
)

urlpatterns += patterns('accounts.views',
    url(r'^signin/$', 'auth_login',
        name='login'),
)
Run Code Online (Sandbox Code Playgroud)

谢谢!

django django-urls django-1.9

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

使用django rest framework api更新用户配置文件

我想创建一个用户可以更新其个人资料的API.在我的情况下,用户可以更新他/她的用户名和密码.要更改他/她的个人资料,API链接应该是/api/change/usernameOfThatUser.当我在链接中使用不存在的用户名时,我仍然会获得userProfileChange API页面,并且输入框未填充先前的数据.我怎么解决这个问题?

serializers.py

User = get_user_model()

class UserProfileChangeSerializer(ModelSerializer):
    username = CharField(required=False, allow_blank=True, initial="current username")
    class Meta:
        model = User
        fields = [
            'username',
            'password',
        ]

    def update(self, instance, validated_data):
        instance.username = validated_data.get('username',instance.username)
        print('instance of username',instance.username)
        return instance 
Run Code Online (Sandbox Code Playgroud)

views.py

class UserProfileChangeAPIView(UpdateAPIView):
    serializer_class = UserProfileChangeSerializer
    lookup_field = 'username'
Run Code Online (Sandbox Code Playgroud)

urls.py

  url(r'^change/(?P<username>[\w-]+)$', UserProfileChangeAPIView.as_view(), name='changeProfile'),
Run Code Online (Sandbox Code Playgroud)

python api django django-rest-framework

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

根据内容自动更改单元格高度 - Swift

在Swift中使用UITableView,有人可以帮我根据标签,图片和描述自动更改单元格的高度吗?

所有信息都正确传递,我只需要帮助格式化它.

我尝试使用它进行调整cell.frame.size.height,但这没有效果.我可以在故事板中更改单元格的大小,但如果可能的话,我希望它更具动态性.

先感谢您!

我的细胞如下:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell

        // Creator
        let creator = self.photos[indexPath.row].creator
        let user = UILabel()
        user.frame = CGRectMake(self.headerView.frame.origin.x, self.headerView.frame.origin.y + self.headerView.frame.height + 3, self.view.frame.width, 12)
        user.text = creator
        cell.addSubview(user)

        // Photo
        let picture = self.photos[indexPath.row].image()
        let imageView = UIImageView(image: picture!)
        imageView.frame = CGRectMake(user.frame.origin.x, user.frame.origin.y + user.frame.height, self.view.frame.width, 400)
        imageView.contentMode = UIViewContentMode.ScaleAspectFit
        cell.addSubview(imageView)

        // Photo description
        let description = UITextView()
        let …
Run Code Online (Sandbox Code Playgroud)

uitableview ios swift

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

切片后重新排序Django查询

有没有办法在切片拍摄后重新排序Django查询?

我正在尝试执行以下操作,但是我收到此错误: Cannot reorder a query once a slice has been taken.

models.py:

class PhotoManager(models.Manager):
    def most_commented(self):
        return super(PhotoManager, self).get_queryset().annotate(
            the_count=(Count('comment'))).order_by('-the_count')[:100]
Run Code Online (Sandbox Code Playgroud)

views.py:

def home(request):
    most_commented = Photo.objects.most_commented()
    photos = most_commented.order_by('?')

    context = {
        'photos': photos
    }
return render(request, 'home.html', context)
Run Code Online (Sandbox Code Playgroud)

我的目标是在图片上评论最多的前100名,然后随机化他们显示的顺序.

先感谢您!

python django django-models django-queryset django-views

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

尝试从对象中插入 nil 对象[1]

在我的项目中,当我滚动得太快时,我收到一条错误消息:

Terminating app due to uncaught exception
'NSInvalidArgumentException', reason:
'*** -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]:
attempt to insert nil object from objects[1]'
*** First throw call stack:
(0x180a39900 0x1800a7f80 0x1809281a8 0x180928040 0x1000a3994 0x1000a2b30
0x10016e784 0x185f4c108 0x185790810 0x18578b89c 0x185727778
0x183136b2c 0x183131738 0x1831315f8 0x183130c94 0x1831309dc 0x183183770
0x180cae1e8 0x1809db1f8 0x1809f1634 0x1809f0d6c 0x1809eeac4 0x18091d680
0x181e2c088 0x185794d90 0x100183934 0x1804be8b8)
libc++abi.dylib: terminating with uncaught exception of type NSException
Run Code Online (Sandbox Code Playgroud)

然后它返回到以下代码(突出显示NSDictionary *attributes底部的行):

- (NSDictionary *)attributesFromProperties{
    // Setup shadow attributes
    NSShadow *shadow = shadow = [[NSShadow alloc] init];
    if (self.shadowColor){ …
Run Code Online (Sandbox Code Playgroud)

objective-c nsexception ios

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