我使用 django v1.11.7 和 Jinja2 v2.10 作为模板引擎。
在我的网站中,我希望使用引导程序呈现表单。我发现django-widget-tweaks可用于此集成。
该包无法与 Jinja 模板一起使用,因为它需要将自定义标签集加载到 Jinja 环境中。加载标签集可能很复杂,因为我可能需要编写扩展。
有没有一种简单的方法可以让 django-widget-tweaks 与 Jinja 一起工作?或者还有其他方法将 boostrap 与 Jinja 集成吗?
我正在使用 django v2.0.2 构建一个项目,该项目由 3 个具有 24 个模型的应用程序组成。其中一款应用有 14 个模型。在一个应用程序中拥有如此多的模型变得越来越复杂,我想创建一个新应用程序并将少数模型移至此应用程序。
我找到了一个答案,解释了如何使用南来做到这一点。我一直在使用 django 核心迁移,由于不推荐使用南,此时我不想切换到南。
我想要移动的模型非常复杂 - 它们有ForeignKey字段、ManyToMany字段等。我需要一个工作流来展示如何使用 django 核心迁移来移动这些模型。
我正在使用 django 的基于类的通用视图CreateView将图像上传到一本书。这是代码:
# models.py
class Upload(models.Model):
image = models.ImageField(upload_to=get_upload_path, help_text='Image to process')
uploader = models.ForeignKey(settings.AUTH_USER_MODEL, models.CASCADE, related_name='uploader')
language = models.ForeignKey(Language, models.CASCADE)
book = models.ForeignKey(Book, models.CASCADE)
def __str__(self):
return str(os.path.split(self.image.name)[-1].split('_', 1)[-1])
@models.permalink
def get_absolute_url(self):
return ('book:upload_new', (self.book.id,)) # upload_new is linked to the view below
def save(self, *args, **kwargs):
super(Upload, self).save(*args, **kwargs)
def delete(self, *args, **kwargs):
self.image.delete(False)
super(Upload, self).delete(*args, **kwargs)
# views.py
@method_decorator(login_required, name='dispatch')
class PictureCreateView(CreateView):
model = Upload
fields = ("image",)
book_id = None
def dispatch(self, *args, …Run Code Online (Sandbox Code Playgroud) 我正在使用 djangoCreateView将图像添加到书中。我将书的 id 作为 url 中的参数传递给基于类的视图。诸如book和 之类的表单字段language不会呈现在模板上,而是借助书籍 ID 来获取。
# views.py
class PictureCreateView(CreateView):
model = Upload
fields = "__all__"
book_id = None
def get_initial(self):
initial = super(PictureCreateView, self).get_initial()
initial = initial.copy()
self.book_id = self.kwargs['book_id']
book = Book.objects.get(id=self.book_id)
initial['book'] = book
initial['language'] = language
initial['uploader'] = self.request.user
return initial
# set book_id so it used in the template
def get_context_data(self, **kwargs):
context = super(PictureCreateView, self).get_context_data(**kwargs)
context['book_id'] = self.book_id
return context
def form_valid(self, form, **kwargs):
print('Form …Run Code Online (Sandbox Code Playgroud) django ×3
django-forms ×2
bootstrap-4 ×1
css ×1
django-views ×1
javascript ×1
jinja2 ×1
jquery ×1