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

Kak*_*kar 5 django django-templates django-admin

我正在使用编辑器wysiwyg编辑器来编写我的内容.该页面只有wysiwyg编辑器和一个保存按钮.

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>
Run Code Online (Sandbox Code Playgroud)

在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),
Run Code Online (Sandbox Code Playgroud)

views.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
    })
Run Code Online (Sandbox Code Playgroud)

现在,在django-admin面板中可能有已写入内容的列表:

  • 如果我点击添加,我想进入编辑页面.
  • 如果我点击任何已编写的内容对象,我想获取该对象并将其加载到编辑器页面中.

现在它显示列表,当我单击添加或内容时,它仅显示在管理面板内.我如何实现我想要的目标?你的帮助和指导非常非常重要.谢谢.

jat*_*jit 0

One way is to hijack the admin urls, and use your own views for those urls, i.e., the admin url won't change, but your editor page views will be called instead of the default admin views. (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
Run Code Online (Sandbox Code Playgroud)