TemplateSyntaxErrorDjango 可能是什么原因造成的?
Invalid block tag: 'endfor', expected 'endblock'
Run Code Online (Sandbox Code Playgroud)
到目前为止,我的模板非常简单:
{% extends 'base.html' %}
{% block content %}
<div id='beerslist'>
{$ for beer in beers %}
{{ beer }}
{% endfor %}
</div>
{% endblock %}
Run Code Online (Sandbox Code Playgroud)
查看回溯列表后,'beer'变量存在并返回一个数组.如何修复此语法错误?
我一直在努力创建django FormWizard。我认为我已经很接近了,但是我无法弄清楚如何保存到数据库。
我尝试了这里建议的解决方案:
def done(self, form_list, **kwargs):
instance = MyModel()
for form in form_list:
for field, value in form.cleaned_data.iteritems():
setattr(instance, field, value)
instance.save()
return render_to_response('wizard-done.html', {
'form_data': [form.cleaned_data for form in form_list],
})
Run Code Online (Sandbox Code Playgroud)
但是将其放在done方法中会导致No Exception Supplied错误。save另一方面,将此代码放在方法中不会保存信息。
我也尝试过这里建议的解决方案:
def done(self, form_list, **kwargs):
for form in form_list:
form.save()
return render_to_response('wizard-done.html', {
'form_data': [form.cleaned_data for form in form_list],
})
Run Code Online (Sandbox Code Playgroud)
但这会返回另一个错误:AttributeError at /wizard/ 'StepOneForm' object has no attribute 'save'。你遇到这个问题了吗?提交向导后如何将信息保存到数据库?谢谢
ImportError: no module named location.models 在以下行:
from location.models import Zipcode
Run Code Online (Sandbox Code Playgroud)
但是在我的项目中调用的已安装应用程序中有一个models.py,一个__init__.py和一个Zipcode模型location.
此外,使用相同的命令可以在python shell中轻松导入模块.这可能是什么问题?谢谢你的想法!
我正在尝试为用户设置一种“观看”某些项目的方法(即将项目添加到包含其他用户的其他项目的列表中):
class WatchList(models.Model):
user = models.ForeignKey(User)
class Thing(models.Model):
watchlist = models.ForeignKey(WatchList, null=True, blank=True)
Run Code Online (Sandbox Code Playgroud)
如何Thing向用户添加 a WatchList?
>>> from myapp.models import Thing
>>> z = get_object_or_404(Thing, pk=1)
>>> a = z.watchlist.add(user="SomeUser")
AttributeError: 'NoneType' object has no attribute 'add'
Run Code Online (Sandbox Code Playgroud)
如何将项目添加到监视列表?和/或这是设置我的模型字段的适当方法吗?感谢您的任何想法!
我试图扩展我的Django模型中的关系.我有一个系统,其中元素存储在类别中.如何构建my models.py以使每个类别与子类别相关?
这是我的类别模型的样子:
class Category(models.Model):
site = models.ForeignKey(Site)
template_prefix = models.CharField(max_length=200, blank=True)
name = models.CharField(max_length=200)
slug = models.SlugField()
description = models.TextField(default='')
sortby_fields = models.CharField(max_length=200,
help_text=_(u'A comma separated list of field names that should show up as sorting options.'),
blank=True)
sort_order = models.PositiveIntegerField(default=0)
def __unicode__(self):
return self.name + u' Category'
class Meta:
verbose_name_plural = u'categories'
Run Code Online (Sandbox Code Playgroud)
谢谢你的任何建议.
试图更好地处理 django 数据库关系的处理方式。任何想法表示赞赏。
考虑以下示例模型:
class Things(models.Model):
name = models.CharField(max_length=20)
class Stuff(models.Model):
name = models.CharField(max_length=20)
information = models.ManyToManyField('Information')
things = models.ForeignKey('Things')
class Information(models.Model):
name = models.CharField(max_length=20)
stuff = models.ForeignKey('Stuff')
Run Code Online (Sandbox Code Playgroud)
错误来自syncdb:AttributeError: 'ManyToManyField' object has no attribute 'ForeignKey'。如果我同时包含ManyToManyField和Foreign Key字段,则会导致错误Stuff模型中。
有没有办法让这两种关系都存在?感谢您的任何想法。
我将这些字段添加到我的模型中:
class WatchList(models.Model):
name = models.CharField(max_length=20)
class Thing(models.Model):
watchlist = models.ForeignKey(WatchList)
Run Code Online (Sandbox Code Playgroud)
成功运行架构迁移:
>>> $ python2.7 manage.py schemamigration myapp --auto
+ Added model myapp.WatchList
? The field 'Thing.watchlist' does not have a default specified, yet is NOT NULL.
? Since you are adding this field, you MUST specify a default
? value to use for existing rows. Would you like to:
? 1. Quit now, and add a default to the field in models.py
? 2. Specify a one-off value to …Run Code Online (Sandbox Code Playgroud) 如果我想将django-tastypie与我已经在Django 1.4中启动的项目集成,它会起作用吗?如果只是部分,有什么影响?如果不是,那么在我开始尝试集成它之前,这将是一件好事.我也不希望在Django 1.5中重建整个应用程序,理想情况下可以将Tastypie添加到我现有的1.4项目中.
谢谢你的建议!
我希望能够在悬停时滑动切换div,但它不起作用..这是代码:
<div class='slide-header'>
header....
</div>
<div class='slide-content'>
Content..................
</div>
<script>
$(".slide-header").hover(function () {
$(".slide-content").slideToggle("slow");
});
</script>
<style><!-- initially, I don't want to display .slide-content -->
.slide-content { display:none; }
</style>
Run Code Online (Sandbox Code Playgroud)
我怎么能在悬停时滑动?滑动?谢谢!
我想删除一个外键的值。这是我的模型:
class WatchList(models.Model):
user = models.ForeignKey(User)
class Thing(models.Model)
watchlist = models.ForeignKey(WatchList, null=True, blank=True)
Run Code Online (Sandbox Code Playgroud)
我想Thing从用户的WatchList. 我试图这样做,但这会删除整个Thing,而不是它在监视列表中的位置:
def delete(request, id):
thing = get_object_or_404(Thing, pk=id)
if thing.watchlist.user == request.user:
thing.watchlist.delete() ## also tried thing.watchlist.user.delete() unsuccessfully
return HttpResponseRedirect('somewhere')
else:
# other stuff
Run Code Online (Sandbox Code Playgroud)
如何在不删除整个内容Thing的WatchList情况下从用户中删除 a ?
编辑(意识到我应该使用ManyToMany关系。感谢评论者!)
class Thing(models.Model)
watchlist = models.ManyToManyField(WatchList)
Run Code Online (Sandbox Code Playgroud)
编辑(试图删除多对多):
thing = get_object_or_404(Thing, pk=id)
wl = WatchList.objects.get(user=request.user)
if wl.user == request.user:
thing.watchlist.remove(wl)
Run Code Online (Sandbox Code Playgroud) django ×9
python ×7
models ×4
database ×2
api ×1
categories ×1
django-1.4 ×1
django-south ×1
forms ×1
hover ×1
importerror ×1
integration ×1
jquery ×1
migration ×1
render ×1
save ×1
slidetoggle ×1
syntax-error ×1
tastypie ×1
view ×1