我正在使用django-favorites来提取用户所拥有的对象列表.该应用程序有一个模型和一个经理
class FavoriteManager(models.Manager):
""" A Manager for Favorites
"""
def favorites_for_user(self, user):
""" Returns Favorites for a specific user
"""
return self.get_query_set().filter(user=user)
class Favorite(models.Model):
user = models.ForeignKey(User)
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type', 'object_id')
created_on = models.DateTimeField(auto_now_add=True)
objects = FavoriteManager()
class Meta:
verbose_name = _('favorite')
verbose_name_plural = _('favorites')
unique_together = (('user', 'content_type', 'object_id'),)
def __unicode__(self):
return "%s added %s as a favorite" % (self.user, self.content_object)
Run Code Online (Sandbox Code Playgroud)
在我看来,我正在为用户拉扯最爱
from favorites.models import Favorite
def myfavorites(request):
item = Favorite.objects.favorites_for_user(user=request.user)
return render_to_response('myfavorites.html', …
Run Code Online (Sandbox Code Playgroud) 我想使用Date.UTC在postgresql 9.2"json"字段中存储日期和日期时间,但当然它失败了:
hp=> update formapp_record set data='{"dt": Date.UTC(120, 10, 2)}' where id=17;
ERROR: invalid input syntax for type json
LINE 1: update formapp_record set data='{"dt": Date.UTC(120, 10, 2)}...
^
DETAIL: Token "Date" is invalid.
CONTEXT: JSON data, line 1: {"dt": Date...
Run Code Online (Sandbox Code Playgroud)
可以直接存储UTC时间戳,但是解码器怎么知道该值应该解码为日期或日期时间而不是int?
也可以将Date.UTC调用存储为字符串,如下所示:
update formapp_record set data='{"dt": "Date.UTC(120, 10, 2)"}' where id=17;
Run Code Online (Sandbox Code Playgroud)
虽然这有效,但它需要0.检查字符串是否以Date.UTC开头,并且1.在plv8中使用eval
解决方案是存储一些元数据,如:
update formapp_record set data='{"dt": {"_type": "date", "_value": [120, 10, 2]}}' where id=17;
Run Code Online (Sandbox Code Playgroud)
但那不是很"标准",甚至是"黑客".
你对这件事有什么看法?
我正在使用教程创建这个django应用程序,我要完成第4部分https://docs.djangoproject.com/en/dev/intro/tutorial04/
该应用程序显示基本轮询,当您单击它时,它会显示一些选项和一个投票按钮.
问题是当我点击投票.它显示找不到页面.我认为问题是重定向,但我不知道在哪里确定问题.第一页是index.html,它显示问题,然后是detail.html,显示选项和问题.我知道当我点击投票时,它会回到应用程序URLconf,然后urlconf执行视图功能,视图功能执行结果.
我的detail.html是
<h1>{{ poll.question }}</h1>
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
<form action="/myapp/{{ poll.id }}/vote/" method="post">
{% csrf_token %}
{% for choice in poll.choice_set.all %}
<input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}" />
<label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br />
{% endfor %}
<input type="submit" value="Vote" />
</form>
Run Code Online (Sandbox Code Playgroud)
myapp里面的urls.py是:
from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.conf import settings
from django.conf.urls import patterns, include, url
urlpatterns = patterns('myapp.views',
url(r'^$', …
Run Code Online (Sandbox Code Playgroud) Django有两种方法可以在一个请求中处理多个表单:
哪个用例最好?
在我的特定情况下,表单列出了要从diff更新的对象的字段.对于每个字段,可以定义一个操作(如"更新值","保持值").该页面包含多个对象的表单.
我正在为一个类似于Twitter转发的应用程序开发一个功能.
在模型中Item
,我想添加一个相关的字段reposted_from
,以引用另一个Item
.我不认为我使用ForeignKey
它,因为它是相同的模型,但我使用什么呢?
在 Django 站点中发表评论后,如何摆脱用户被定向到“感谢您的评论”页面的情况?我希望用户被重定向到他们评论的同一页面。我正在使用 Django 注释。
我试过添加:
<input type=”hidden” name=”next” value=”"{% url
django.contrib.comments.views.comments.comment_done %}" />
Run Code Online (Sandbox Code Playgroud)
但它不起作用。下面是我的 comment/form.html 中的代码
{% load comments %}
{% get_comment_count for sol as comment_count %}
{% get_comment_list for sol as comment_list %}
{% get_comment_form for sol as form %}
{% if user.is_authenticated %}
<form action="{% comment_form_target %}" method="post">
{% csrf_token %}
{% if next %}<input type="hidden" name="next" value="{% url
django.contrib.comments.views.comments.comment_done %}" />{% endif %}
{% for field in form %}
{% if field.is_hidden %}
{{ field }} …
Run Code Online (Sandbox Code Playgroud) 我最近尝试使用以下内容扩展django的注册表单,但我只能看到默认的四个字段.有什么我想念的吗?
或者,如果我要创建自定义表单,我应该创建自己的注册后端吗?
class RegistrationForm(forms.Form):
username = forms.RegexField(regex=r'^\w+$',
max_length=30,
widget=forms.TextInput(attrs=attrs_dict),
label=_(u'Username'))
email = forms.EmailField(widget=forms.TextInput(attrs=dict(attrs_dict,
maxlength=75)),
label=_(u'Email address'))
first_name =forms.CharField(widget=forms.TextInput(attrs=attrs_dict),label=_(u'First Name'))
last_name =forms.CharField(widget=forms.TextInput(attrs=attrs_dict),label=_(u'Last Name'))
password1 = forms.CharField(widget=forms.PasswordInput(attrs=attrs_dict, render_value=False),
label=_(u'Password'))
password2 = forms.CharField(widget=forms.PasswordInput(attrs=attrs_dict, render_value=False),
label=_(u'Password (again)'))
keywords = forms.ModelMultipleChoiceField(queryset=Keyword.objects.all())
#keywords = forms.ModelChoiceField(queryset=Keyword.objects.all())
def clean_username(self):
try:
user = User.objects.get(username__iexact=self.cleaned_data['username'])
except User.DoesNotExist:
return self.cleaned_data['username']
raise forms.ValidationError(_(u'This username is already taken. Please choose another.'))
def clean(self):
if 'password1' in self.cleaned_data and 'password2' in self.cleaned_data:
if self.cleaned_data['password1'] != self.cleaned_data['password2']:
raise forms.ValidationError(_(u'You must type the same password each …
Run Code Online (Sandbox Code Playgroud) 我在Django中创建了一些Badge类,每个类都包含一些字符串变量中的描述:
"You get this badge because you've runned %d meters in %d minutes"
"You get this badge because you've killed %d monsters of the type %s"
Run Code Online (Sandbox Code Playgroud)
这些类也有一个函数get_description(badge_level_requirements)
,因此在模板中它将与列表一起调用以组合特定用户的字符串:
class RunnerBadge(Badge):
des=ugettext_lazy("You get this badge because you've runned %d meters in %d minutes")
def get_description(cls,badge_level_requirements):
return cls.des%badge_level_requirements
Run Code Online (Sandbox Code Playgroud)
并且我已经将需求列表存储在数据库中而没有任何参数名称:(如示例所示,不同的类具有不同数量的值来填充字符串,值也意味着不同的东西.所以我不能真的命名参数.
但是,如果我想将这些字符串国际化,那么就会出现错误:'msgid' format string with unnamed arguments cannot be properly localized
并且无法为此事项生成语言文件.
有没有办法绕过这个错误?
更新
我遇到过这种绕过错误而不改变数据库的方法.在数据库中,级别要求以dict格式存储在文本字段中:
#Requirment of Runner's badge
"{'gold':(100,10),'silver':(50,5),'bronze':(25,2)}"
Run Code Online (Sandbox Code Playgroud)
在类定义中,手动将参数名称添加为'arg_0','arg_1'...到描述中.在用于填充描述字符串之前,将get_description方法更改为预处理数据.
class RunnersBadge(Badge):
requirements=#get the previous dict from database
description="You get this …
Run Code Online (Sandbox Code Playgroud) 我在我正在构建的配置文件中有这个代码:
define("PROCESSOR_PATH", realpath(dirname(__FILE__).'/usermanager/processors'));
define("LIBRARY_PATH", PROCESSOR_PATH.'/library');
define("TEMPLATE_PATH", realpath(dirname(__FILE__).'/usermanager/assets/templates'));
Run Code Online (Sandbox Code Playgroud)
如果我在包含我的配置文件后调用这些常量中的任何一个,我什么也得不到.所以即使我这样做:
echo PROCESSOR_PATH;
Run Code Online (Sandbox Code Playgroud)
什么都没有回应.有人能指出我做错的方向吗?
django ×8
python ×8
datetime ×1
django-users ×1
json ×1
localization ×1
mysql ×1
php ×1
postgresql ×1
sql ×1
windows ×1