gpt4 book ai didi

django - 在 django admin 中使用外部链接来创建或更新

转载 作者:行者123 更新时间:2023-12-03 17:39:10 24 4
gpt4 key购买 nike

我正在使用 redactor wysiwyg editor写我的内容。该页面只有所见即所得的编辑器和一个保存按钮。

html:

<div id="editor-wrapper">
<input type="text" id="editor-title" {%if blog %} value="{{blog.title}}" {% else %} placeholder="Your title" {% endif %}>
<textarea id="editor-redactor" name="content">
{% if blog %}
{{ blog.body }}
{% else %}
<p>Enter you body in here...</p>
{% endif %}
</textarea>
<button id="save-btn"><a href="/save-blog/">Save</a> </button>
</div>

在 urls.py 中,我添加了转到该页面的 url。
url(r'^add-update-blog/$', views.add_update_blog),
url(r'^add-update-blog/save/(?P<blog_id>\d+)$', views.add_update_blog),

View .py:
def add_update_blog(request):
return render(request, 'editor.html')

def add_update_blog_save(request, blog_id):
blog = Blog.objects.get(id=blog_id)
return render(request, 'editor.html', {
blog: blog
})

现在,在 django-admin 面板中可能有已经写好的内容列表:
  • 如果我点击添加,我想转到编辑器页面。
  • 如果我单击任何已编写的内容对象,我想获取该对象并将其加载到编辑器页面中。

  • 现在它显示列表,当我单击添加或内容时,它仅显示在管理面板内。我如何实现我想要的?您的帮助和指导真的非常有用。谢谢你。

    最佳答案

    一种方法是劫持管理 url,并为这些 url 使用您自己的 View ,即管理 url 不会更改,但将调用您的编辑器页面 View 而不是默认的管理 View 。 ( Documentation , Source )

    from .views import add_update_blog, add_update_blog_save

    class BlogAdmin(admin.ModelAdmin):
    def get_urls(self):
    urls = super(BlogAdmin, self).get_urls()
    new_urls = [
    url(r'^add/$', add_update_blog),
    url(r'^(?P<blog_id>\d+)/change/$', add_update_blog_save),
    ]
    return new_urls + urls # new_urls have to be first

    关于django - 在 django admin 中使用外部链接来创建或更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40585533/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com