如何在模板中使用django-summernote

问题描述 投票:0回答:2

我在我的项目上设置了django-summernote,一切都很棒,它在管理员方面非常好用,但我想在我的模板上使用它。

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

我的主要urls.py:

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

我的app(name = core)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'),
]

我的views.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')
python django summernote
2个回答
0
投票

首先在你的模板中不要忘记添加:

{{ 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(),
        } 

0
投票

好吧我认为最好的方法是使用forms.py

© www.soinside.com 2019 - 2024. All rights reserved.