小编Bre*_*den的帖子

清除Django中的特定缓存

我正在为django项目使用视图缓存.

它说缓存使用URL作为密钥,所以我想知道如果用户更新/删除对象,如何清除其中一个密钥的缓存.

例如:用户将博客帖子发布到domain.com/post/1234/..如果用户编辑了该文章,我想通过在保存编辑后的帖子的视图末尾添加某种删除缓存命令来删除该URL的缓存版本.

我正在使用:

@cache_page(60 * 60)
def post_page(....):
Run Code Online (Sandbox Code Playgroud)

如果post.id是1234,似乎这可能会起作用,但它不是:

def edit_post(....):
    # stuff that saves the edits
    cache.delete('/post/%s/' % post.id)
    return Http.....
Run Code Online (Sandbox Code Playgroud)

python django memcached django-cache

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

如何在Django-Registration中使用不同的表单

Django-Registration在forms.py文件中有几个表单类.一个是"类RegistrationFormTermsOfService(RegistrationForm)..

我在Django注册代码的其余部分更改了什么,以便在我的注册流程中启用此表单而不是RegistrationForm?

django django-registration

7
推荐指数
3
解决办法
6133
查看次数

在django模板中使用JSON

我有一个包含JSON的变量,我需要传递给模板.我将其定义为变量,然后将其成功传递到模板中.但是,我需要格式用"替换引号",但是替换为'.这会导致我将此传递给服务的问题.

image_upload_params = 
{
  "auth": {
    "key": "xxx"
  },
  "template_id": "xxx",
  "redirect_url": "url-here",
}
Run Code Online (Sandbox Code Playgroud)

以下是模板中的内容:

{'redirect_url': 'url-here', 'template_id': 'xxx', 'auth': {'key': 'xxx'}}
Run Code Online (Sandbox Code Playgroud)

任何想法如何让它使用"而不是?

django django-templates django-template-filters

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

Python在新变量中将双引号转换为单引号

我试图在python中使用双引号保存变量,因为我需要将双引号JSON传递给模板,单引号不能用于我正在做的事情.所以我将python中的变量设置为:

json = {
  "auth": {
    "key": "auth-code-here"
  },
  "template_id": "id-here",
  "redirect_url": "url-here",
}
Run Code Online (Sandbox Code Playgroud)

但是直接在python中,它被保存为

{'redirect_url': 'url-here', 'template_id': 'id-here', 'auth': {'key': 'auth-code-here'}}
Run Code Online (Sandbox Code Playgroud)

有没有办法可以将它保存为双引号?或者我是否需要在django模板中处理这个以替换双引号的单引号?

谢谢!

python

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

显示Python/Django中朋友关注的Twitter粉丝

我正在构建我的应用程序的快速部分,该应用程序会查看用户的关注者,并突出显示用户关注的人(朋友)所遵循的部分.

我想知道两件事:

  1. 有没有更有效的方法来做到这一点?似乎这样会影响Twitter的API限制,因为我需要检查每个用户朋友的朋友.

  2. 这将创建一个包含朋友ID和他们关注的关注者的词典列表.相反,dict会更好地作为跟随者ID然后跟随他们的朋友.提示?

码:

# Get followers and friends
followers = api.GetFollowerIDs()['ids']
friends = api.GetFriendIDs()['ids']

# Create list of followers user is not following
followers_not_friends = set(followers).difference(friends)

# Create list of which of user's followers are followed by which friends
followers_that_friends_follow = []
for f in friends:
    ff = api.GetFriendIDs(f)['ids']
    users = followers_not_friends.intersection(ff)
    followers_that_friends_follow.append({'friend': f, 'users': users })
Run Code Online (Sandbox Code Playgroud)

python django twitter

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

强制Django登录表单使用超过30个字符的用户名

我正在尝试将电子邮件用于用户名,并且几乎完美地运行它.我按照这些指示:http://www.micahcarrick.com/django-email-authentication.html

问题是,我的登录表单仍然抛出一个错误,表示用户名只能是30个字符.我已经将输入表单更改为接受75个字符和数据库表.但是Django中的某些内容仍然阻止了这一点.

有任何想法吗?

更新:

<form method="post" action="." class="full">
{% csrf_token %}
<ul>
    {% if form.non_field_errors %}<li>{{ form.non_field_errors }}</li>{% endif %}
    <li>
        {{ form.username.errors }}
        <label for="id_username">Email/Username</label>
        <input type="text" id="id_username" name="username" maxlength="75">
    </li>
    <li>
        {{ form.password.errors }}
        <label for="id_password">{{ form.password.label }}</label>
        {{ form.password }}
    </li>
</ul>
<input type="submit" value="Login">
<a href="{% url django.contrib.auth.views.password_reset %}">Forgot your password?</a>
</form>
Run Code Online (Sandbox Code Playgroud)

django django-registration

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

检查Django模板中列表变量项中是否存在某些内容

我有一个传递给Django模板的部分列表.这些部分有不同的类型.我想说"如果有这种类型的部分,在我的模板中显示此行",但有问题.我基本上要做的就是这个.

{% if s.name == "Social" for s in sections %}
    Hello Social!
{% endif %}
Run Code Online (Sandbox Code Playgroud)

但当然这不起作用.任何想法如何基本上在一行循环通过列表中的项目并执行if语句?

附加信息:我可能有多个"社交"部分.我在模板中尝试做的是说"如果有任何社交部分,则显示此div.如果没有,则不显示div." 但是我不希望div重复,这就是上面代码会发生的情况.

python django django-templates

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

使用或不使用/可以使Django URL工作

我有一个django应用程序,在每个URL conf的末尾都有/.例:

# user home page
(r'^home/$', 'user_home_page'),
Run Code Online (Sandbox Code Playgroud)

但是,我注意到这导致我的服务器上出现大量重定向,因为当人们不添加/时,它会重定向它们.有没有办法让它接受两个没有重定向除了做:

# user home page
(r'^home$', 'user_home_page'),
(r'^home/$', 'user_home_page'),
Run Code Online (Sandbox Code Playgroud)

或者我应该避免像这样的URL混淆?

python django

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

允许用户在Heroku上为Django应用程序使用自定义域

我有一个在Heroku上托管的Django应用程序.在应用程序中,用户在http://domain.com/username上创建页面

我想让用户使用CNAME为他们的页面使用自己的域名.理想情况下,如果我将来更改主机并且我的IP发生变化,我想避免使用A-Record.

这对我来说是一个全新的领域,甚至不知道从哪里开始,或者寻找什么.有没有人建议从哪里开始?我见过提到通配符DNS,但不确定这与我的应用程序有什么联系.

任何建议都会非常感激.

python django heroku

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

按startswith match排序查询结果

我们正在进行站点搜索,搜索带有icontains. 它运行良好,但出现的结果顶部没有最相关的结果。

一个例子是搜索“权力的游戏”。如果搜索是“Game of”,第一个结果可能是“Crazy Game of...”,第二个结果是“Game of Thrones”

本质上,我想使用 进行搜索icontains,但按 进行排序startswith。有任何想法吗?

python django django-queryset

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