【问题标题】:How to use django-summernote in templates如何在模板中使用 django-summernote
【发布时间】:2019-02-09 14:44:42
【问题描述】:

我已经在我的项目中设置了 django-summernote,一切都很好,它在 admin 上运行良好,但我想在我的模板上使用它。

注意: 在 django-summernote 文档中,如果您有 forms.py 文件,他们只解释如何使用它,但我没有

我的主要 urls.py :

urlpatterns = [
    path('games/', include('core.urls', namespace='core')),
    path('summernote/', include('django_summernote.urls')),
]

我的应用(名称=核心)urls.py:

from django.urls import path
from . import views
app_name = 'core'
urlpatterns = [
    path('new/', views.GameCreate.as_view(), name='game_new'),
    path('<int:pk>/edit/', views.GameUpdate.as_view(), name='game_edit'),
]

我的意见.py:

class GameCreate(LoginRequiredMixin, CreateView):

    model = Game
    template_name = 'core/game_new.html'
    fields = '__all__'
    redirect_field_name = 'home'

class GameUpdate(LoginRequiredMixin, UpdateView):
    model = Game
    template_name = 'core/game_edit.html'
    fields = '__all__'

我的模板文件“game_new.html”:

{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% block title %} Add New Game {% endblock %}

{% block main %}
<section class="main-section">
    <div class="container">
        <h1>New Game</h1>
        <form action="" method="post" enctype="multipart/form-data">
            {% csrf_token %}
            {{ form|crispy }}
            <input type='submit' value="Save" />
        </form>
    </div>
</section>
{% endblock %}

我的模板文件“game_edit.html”:

{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% block title %} Game Info {% endblock %}

{% block main %}
<section class="main-section"></section>
    <div class="container">
        <h1>Edit Game Info</h1>
        <form action="" method="post">
            {% csrf_token %}
            {{ form|crispy }}
            <input type="submit" value="Update" />
        </form>
    </div>
</section>
{% endblock %}

我的 Models.py :

from django.db import models
from django.urls import reverse

# Create your models here.

class Game(models.Model):
    name = models.CharField(max_length=140)
    developer = models.CharField(max_length=140)
    game_trailer = models.CharField(max_length=300, default="No Trailer")
    game_story = models.TextField(default='No Story')

【问题讨论】:

  • 请给我们看看你的游戏模型吗?
  • 嗨 @rakwen 我已经用游戏模型更新了我的问题

标签: python django summernote


【解决方案1】:

首先在你的模板中不要伪造添加这个:

{{ form.media }} <<<------ To load all js and css attached

在您的模型文档中,您必须继承summernote 小部件。示例:

from django_summernote.widgets import SummernoteWidget, SummernoteInplaceWidget
class FormFromSomeModel(forms.ModelForm):
    class Meta:
        model = SomeModel
        widgets = {
            'foo': SummernoteWidget(),
            'bar': SummernoteInplaceWidget(),
        } 

【讨论】:

    猜你喜欢
    • 2015-09-07
    • 1970-01-01
    • 2011-09-17
    • 2014-02-17
    • 2011-10-01
    • 2020-09-09
    • 2010-09-23
    • 2020-10-24
    • 2016-06-24
    相关资源
    最近更新 更多