小编Ben*_*Ben的帖子

具有上下文变量参数的Django自定义模板标记

我有一个自定义模板标签,显示日历.我想根据动态值填充日历上的某些项目.

这是标签:

@register.inclusion_tag("website/_calendar.html")
def calendar_table(post):
     post=int(post)
     imp=IMP.objects.filter(post__pk=post)
     if imp:
         ...do stuff
Run Code Online (Sandbox Code Playgroud)

在我的模板中,当我传递硬编码值时,它可以正常工作,例如

    {% load inclusion_tags %}

    {% calendar_table "6" %}
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试类似{%calendar_table"{{post.id}}"%}的内容时,会为int()尝试引发一个错误ValueError.我怎么能绕过这个?

django django-templates django-models

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

创建EC2 AMI图像

我想创建我的实例的AMI,但我对AWS上的以下提示感到害怕:

The instance you're using as a template for a new image has the following volumes:
/dev/sda1, vol-a9 (15 GiB), will delete on termination
Total size of EBS volumes: 15 GiB.
When you create an EBS image an EBS snapshot will also be created for each of the above volumes.
Run Code Online (Sandbox Code Playgroud)

现在,当它说,EBS卷将删除终止,是否意味着创建AMI或只是当我终止实例脚本的终止?

amazon-s3 amazon-ec2 amazon-web-services

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

Searchkick 并行更新等待未完成

我已经设置了索引的并行重新索引。

Product.reindex(async: {wait: true})

我正在 DelayedJob 中运行该代码并等待,因为似乎替代方法是定期检查完成状态然后提升新索引 - 这似乎更简单。

令人困惑的是,重新索引似乎永远不会完成。尽管我已经在单个文档的索引上对其进行了测试,但我仍然看到“剩余批次:1”的日志消息。

我希望我误解了文档。

ruby-on-rails elasticsearch searchkick

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

Django ImageField验证(足够)?

我有很多用户上传的内容,我想验证上传的图像文件实际上不是恶意脚本.在Django文档中,它声明了ImageField:

"从FileField继承所有属性和方法,但也验证上传的对象是有效的图像."

这完全准确吗?我已经读过压缩或以其他方式操作图像文件是一个很好的验证测试.我假设PIL做了这样的事......

ImageField是否会大大覆盖我的图片上传安全性?

security django file-upload django-models

5
推荐指数
2
解决办法
4960
查看次数

Django 1.2会话丢失

我以前曾问过类似的问题,但我做了一些研究,这次迭代应该有点不同.似乎有几个SO用户在单个视图中注册和登录用户时遇到了问题,但实际上并没有得到回答.

问题是我在一个Django视图中注册,验证和登录用户.对于大多数用户来说没问题,但对于其他用户,他们的后续请求(他们点击我网站上的链接)会返回匿名用户.不知何故,登录用户丢失了他们的会话,并被重定向到我坐下的页面,不需要身份验证.

然后,当他们通过纯登录视图(而不是注册+登录视图)登录时,会话数据保持不变.问题实际上似乎是在单个视图中注册和登录.

有关同一问题,请参阅此文章:https: //stackoverflow.com/questions/1693726/problem-with-combined-authentication-login-view.

有人认为这可能是一个线程问题.我也看到它表明它与缓存会话数据的后端有关.

有什么想法与它真正有关吗?我无法重现错误,这实际上阻碍了我.

编辑 - 我应该注意到我正在使用默认的数据库支持的会话.

这是我的注册/登录视图

def splash_register(request):
  if request.session.get('beta'):

    if request.method=='POST':
        userform=MyUserCreationForm(request.POST)
        if userform.is_valid():
            #username of <30 char is required by Django User model.  I'm storing username as a hash of user email 

            user=userform.save(commit=False)
            user.username=hash(user.email)
            user.save()



            username=user.username
            password=str(userform.cleaned_data['password'])
            user=auth.authenticate(username=username, password=password)
            if user is not None:
                auth.login(request,user)
                request.session['first_visit']=True
                return HttpResponseRedirect("/")
            else:
                return HttpResponseRedirect('/splash/register/')
        else:
            userform=MyUserCreationForm(request.POST)
            return render_to_response("website/splash_register.html", {'userform':userform}, context_instance=RequestContext(request))
    return render_to_response("website/splash_register.html", context_instance=RequestContext(request))     
else:
    return HttpResponseRedirect('/splash/')        
Run Code Online (Sandbox Code Playgroud)

apache django cookies session caching

5
推荐指数
1
解决办法
1893
查看次数

使用Python创建嵌套的JSON请求

用户需要将json对象作为请求的一部分传递。它看起来像这样:

   {"token" :"ayaljltja",
   "addresses": [
      {'name':'Home','address':'20 Main Street', 
       'city':'new-york'}, 
      {'name':'work', 'address':'x Street', 'city':'ohio'}
   ]}
Run Code Online (Sandbox Code Playgroud)

我现在有两个问题。首先,我不知道如何通过重新创建嵌套的POST来测试此代码。我可以成功地发布字典,但是在JSON对象中发布地址列表却使我感到困惑。

只需使用cURL,我该怎么做?我如何用urrlib2做到这一点?

然后,我的第二个问题是在服务器端反序列化JSON POST对象。我想我只需要查看成功的POST即可确定输入(然后使用json模块反序列化)。

有小费吗?

python serialization json curl deserialization

5
推荐指数
1
解决办法
2420
查看次数

MongoEngine如何处理索引(创建,更新,删除)?

关于设置Mongo索引的最佳实践问题.Mongoengine是Python ORM包装器,允许您在Document元类中设置索引.

这个元类何时被内省并添加了索引?我可以通过mongoengine Document类构建一个集合,然后在事后添加一个索引吗?

如果我从元类中删除索引,是否会从相应的集合中自动删除索引?

谢谢,

python indexing mongodb mongoengine

5
推荐指数
1
解决办法
1801
查看次数

在Git repo中查找包含特定字符串模式的最后一次提交

我想回到最后一次包含代码的提交,例如"ThisClass(object):".该代码随后在稍后的提交中从项目中删除.

我想过使用git bisect + grep/awk-ing.我也听说git grep可能允许你做这类事情,虽然我无法在手册页中弄清楚正确的命令.

有什么想法吗?

git awk grep

5
推荐指数
1
解决办法
935
查看次数

Angular ui-router似乎在加载时自动重写url

通过ui-sref更改路由可以正常工作.

当我在基本URL(example.com/account)上手动刷新页面时,一切都同样有效.

但是,当我手动刷新页面并且url包含hashbang(即http://example.com/account/#/billing时,我在控制台中看到以下错误.

TypeError: Cannot read property 'charAt' of undefined
at Object.$$parse (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular.js:9321:40)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular.js:9878:21
at Scope.$get.Scope.$eval (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular.js:12701:28)
at Scope.$get.Scope.$digest (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular.js:12513:31)
at Scope.$get.Scope.$apply (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular.js:12805:24)
at done (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular.js:8378:45)
at completeRequest (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular.js:8592:7)
at XMLHttpRequest.xhr.onreadystatechange (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular.js:8531:11)
Run Code Online (Sandbox Code Playgroud)

在阅读了这个错误并添加了断点之后,我注意到,在加载时,路径正在从表格中暂时重写

/account/#/billing
/account/#settings
Run Code Online (Sandbox Code Playgroud)

其中帐户是应用程序的基础,表单是

/billing/ 
/settings/
Run Code Online (Sandbox Code Playgroud)

最终,在错误发生后,地址栏中将恢复正确的URL表单.

这些临时(中间)路径似乎导致$$解析中的以下代码中断.AppBase绑定到"example.com/account/",url绑定到像"mysite.com/billing"这样的值.withoutBaseUrl绑定为undefined,因为url不包含超出appBase的字符.

  this.$$parse = function(url) {
    var withoutBaseUrl = beginsWith(appBase, url) || beginsWith(appBaseNoFile, url);
    var withoutHashUrl = withoutBaseUrl.charAt(0) == '#'
        ? beginsWith(hashPrefix, withoutBaseUrl)
        : (this.$$html5)
          ? withoutBaseUrl
          : '';
Run Code Online (Sandbox Code Playgroud)

这是我的相关代码:

 plaidApp.config(['$stateProvider', '$urlRouterProvider',
'$interpolateProvider', function($stateProvider,
  $urlRouterProvider, $interpolateProvider) {

$stateProvider
  .state('account', …
Run Code Online (Sandbox Code Playgroud)

angularjs angular-routing angular-ui-router

5
推荐指数
1
解决办法
677
查看次数

Django没有ForeignKey ......但它是一个ManyToManyField

我的管理员工作得很好,而不是一个大的路障.我在帖子和标签之间建立了许多关系.我可以在我的管理员中使用CRUD标签,但出于某种原因,我收到以下错误消息:

Exception at /admin/website/post/add/

<class 'website.models.Tag'> has no ForeignKey to <class 'website.models.Post'
Run Code Online (Sandbox Code Playgroud)

这是我的模特:

class Post(models.Model):

  user=models.ForeignKey(User, unique=True)
  title=models.CharField(max_length=80)
  slug=models.SlugField()
  description=models.TextField(max_length=1000, blank=True)
  created=models.DateField(auto_now_add=True)
  #following info is for processing purposes
  management_phone=models.CharField(max_length=200, blank=True)
  management_email=models.CharField(max_length=200, blank=True)
  processing=models.BooleanField(default=False)
  transacted=models.BooleanField(default=False)
  manually_closed=models.BooleanField(default=False)

  def __unicode__(self):
      return self.title 

class Tag(models.Model):
   title=models.CharField(max_length=100)
   posts=models.ManyToManyField(Post, blank=True,null=True)

   def __unicode__(self):
        Return self.title
Run Code Online (Sandbox Code Playgroud)

同样,只有在我尝试添加Post实例时才会出现问题

我的数据库中有一个数据库表"website_tag_posts"用于关系.这有什么问题?

谢谢

django django-models django-admin

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