如果我能弄清楚为什么我的模型没有在管理员中显示,我该死的.
models.py看起来像这样:
from django.db import models
class Publication(models.Model):
title = models.CharField(max_length=255)
def __unicode__(self):
return unicode(self.title)
Run Code Online (Sandbox Code Playgroud)
admin.py
from django.contrib import admin
from publications.models import Publication
admin.site.register(Publication)
Run Code Online (Sandbox Code Playgroud)
settings.py
...
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
# Uncomment the next line for simple clickjacking protection:
# 'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
TEMPLATE_CONTEXT_PROCESSORS = (
'django.core.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
# Uncomment the next line to enable the admin:
'django.contrib.admin',
# Uncomment the next line to enable …Run Code Online (Sandbox Code Playgroud) 我有一个脚本,一个Django应用程序的一部分,从pdfs制作缩略图.我正在使用优秀的魔杖包来完成它:这是魔杖文档
如果我./manage.py runserver从命令行运行它运行正常,但如果我从PyCharm运行,它会中断.
当我单步执行代码时,问题是用于打开blob的代码总是返回一个空wand.image对象.它是正确的类(wand.image)但它是空的.我传递的对象是pdf,但是blob转换(根本不产生任何错误)是空的.
错误发生在下一行(single_image = all_pages.sequence[0])中,因为它all_pages是空的,因此索引超出范围.
同样,如果我从命令行启动服务器,它可以工作,但如果我从PyCharm启动,它会中断.
我正在使用virtualenv.
这是我正在运行的代码:
from wand.image import Image as WandImage
from wand.color import Color
def convert_to_thumb(pdf_path, slug):
with open(pdf_path) as f:
image_binary = f.read()
all_pages = WandImage(blob=image_binary) #<-- Here image_binary is a pdf
single_image = all_pages.sequence[0] #<-- BOOM! all_pages is a wand.image, but it's empty. Gives an Index error
with WandImage(single_image) as i:
i.format = 'png'
i.background_color = Color('white')
i.alpha_channel = 'remove'
i.transform(resize='x100') …Run Code Online (Sandbox Code Playgroud) 回购中的相同代码在野外工作正常.但是在我的本地环境中,我在表单提交时遇到了CSRF错误.
Django 1.11.13,Python 2.7
调试非常困难,因为实际上没有任何断点.错误发生在视图之前.
CSRF令牌在表单中{% csrf_token %},隐藏字段在那里,cookie就在那里.
我试过创建一个新的虚拟环境,擦除数据库,不同的浏览器.还有什么?
我正在尝试将一些数据传递给,autocomplete_light.AutocompleteModelBase以便我可以从搜索中排除一些模型。我正在尝试使用此处文档中的依赖项信息
但我似乎能明白。
输入的 id 是id_alternate_version-autocomplete,所以我正在尝试:
$("#id_alternate_version-autocomplete").yourlabsWidget().autocomplete.data = {'id': 'foo'};
Run Code Online (Sandbox Code Playgroud)
但调用的网址看起来像http://127.0.0.1:8000/autocomplete/FooAutocomplete/?q=bar
我想:http://127.0.0.1:8000/autocomplete/FooAutocomplete/?q=bar&id=foo
我怎样才能做这样的事情呢?
假设我有一些继承自基类Animal的模型。我可以使用通用视图,并将Cat / 12路由到详细视图,将Dod / 10路由到具有不同上下文的同一详细视图。但是我想从URL中获取Model名称,这样就不必定义路由。
我有这样的事情:
url(r'^cat/(?P<slug>[-\w]+)/$',
DetailView.as_view(
queryset=Cat.objects.filter(),
model=Cat,
context_object_name='animal',
template_name='animal/detail.html'),
name='detail'),
url(r'^dog/(?P<slug>[-\w]+)/$',
DetailView.as_view(
queryset=Dog.objects.filter(),
model=Dog,
context_object_name='animal',
template_name='animal/detail.html'),
name='detail'),
...
Run Code Online (Sandbox Code Playgroud)
显然,这是太多重复的代码。我宁愿做这样的事情:
url(r'^?P<my_animal>\w+/(?P<slug>[-\w]+)/$',
DetailView.as_view(
queryset=my_animal.objects.filter(),
model=my_animal,
context_object_name='animal',
template_name='animal/detail.html'),
name='detail'),
...
Run Code Online (Sandbox Code Playgroud)
我可以这样做吗?
编辑
在达尔文的帮助下,这就是我最终得到的结果。它避免了if / else来获得Model名称:
class AnimalDetailView(DetailView):
context_object_name='animal'
template_name='animals/detail.html'
def dispatch(self, request, *args, **kwargs):
my_animal = kwargs.get('my_animal', None)
self.model = get_model('animals',my_animal.capitalize())
try:
ret = super(AnimalDetailView, self).dispatch(request, *args, **kwargs)
except AttributeError:
raise Http404
return ret
def get_queryset(self):
return self.model.objects.filter()
Run Code Online (Sandbox Code Playgroud)
下次我对继承有疑问时,我将咨询达尔文!大声笑
这甚至可能吗?
我需要存储一些要作为json/rest检索的文档.
A Document有很多Sections,有一个部分有一个标题,一个正文和许多Images.
有没有办法用这种结构制作表格?
Publication
|-- Section
|-- Image
|-- Image
|-- Section
|-- Image
|-- Section
|-- Image
|-- Image
|-- Image
Run Code Online (Sandbox Code Playgroud)
我的模特:
class Publication(models.Model):
title = models.CharField(max_length=64)
class Section(models.Model):
publication = models.ForeignKey(Publication)
heading = models.CharField(max_length=128)
body = models.TextField()
class Image(models.Model):
section = models.ForeignKey(Section)
image = models.ImageField(upload_to='images/')
caption = models.CharField(max_length=64, blank=True)
alt_text = models.CharField(max_length=64)
Run Code Online (Sandbox Code Playgroud)
当Image与之相关时Publication,我可以相对容易地做到这一点,因为只有一个级别的嵌套.
但是,当Image属于时Section,我不确定如何构建表单.似乎没有简单的方法可以使用内联表单集来完成此操作.
有人可以帮忙吗?
另一个与 prefetch_ 有关的问题,哈欠。
使用 Django 1.7 给定模型如下:
class Armor(models.Model):
name = models.CharField(max_length=32)
defense = models.IntegerField(max_length=18)
class Weapon(models.Model):
name = models.CharField(max_length=32)
attack = models.IntegerField(max_length=18)
class Alignment(models.Model):
name = models.CharField(max_length=32)
# many more attribute models
class Knight(models.model):
name = models.CharField(max_length=32)
strength = models.IntegerField(max_length=18)
iq = models.IntegerField(max_length=18)
alignment = models.ManyToManyField(Alignment)
weapons = models.ManyToManyField(Weapon, blank=True)
armor = models.ManyToManyField(Armor, blank=True)
#... many more m2ms
Run Code Online (Sandbox Code Playgroud)
我需要在页面上查看所有骑士,但如果用户选择一个或多个属性作为过滤器,我实际上只关心他们的盔甲、武器或阵营。
例如,如果用户在页面导航上选择“对齐”和“武器”,我想显示每个骑士的武器和对齐集。
在我看来,我有一个用户选择的所有属性类型的列表,我想将该列表传递给查询集prefetch_related()方法,即
selected_attributes = ['alignment','weapons']
Knight.objects.all().prefetch_related(selected_attributes)
Run Code Online (Sandbox Code Playgroud)
但我似乎无法让它发挥作用。我可以做吗?
django ×7
django-1.7 ×1
django-admin ×1
django-csrf ×1
django-forms ×1
pycharm ×1
python ×1
wand ×1