Using external links inside django admin to create or update

I use the wysiwyg editor editor to record my content. There is only a wysiwyg editor on the page and a save button.

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> 

And in urls.py I added a url to go to this page.

 url(r'^add-update-blog/$', views.add_update_blog), url(r'^add-update-blog/save/(?P<blog_id>\d+)$', views.add_update_blog), 

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 }) 

Now, in the django-admin panel, there may be a list of already written content:

  • If I click add, I want to go to the editor page.
  • If I click on any of the already written content object, I want to get this object and load it on the editor page.

At the moment, it displays a list and when I click the add button or the content that appears only in the admin panel. How can I achieve what I want? Your help and guidance is really very studied. Thanks.

+5
source share
2 answers

One way is to grab the admin URLs and use your own views for these URLs, i.e. The admin URL will not change, but your pageviews in the editor will be displayed 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 
0
source

First, it’s possible that your problems are caused by a missing (or extra) slash at the end of your URLs. Your url add-update-blog requires a trailing slash, while the save URL will not match if there is a trailing slash. Can you use regex syntax /? to make the slash optional, as in my example below. Notably, the Django admin URLs only match the trailing slash.

As mentioned in jatinderjit, you need to β€œgrab” admin URLs. However, this can be done in a simple way within urls.py By including a URL that matches the admin URL before we include the admin URLs, we can guarantee that redirects to our custom view will be agreed first. This is easy to do with RedirectView and with the name url patterns , which will pass your blog_id parameter to the already recorded view.

 ''' Snippet of urls.py''' from django.conf.urls import include, url from django.contrib import admin from django.views.generic.base import RedirectView urlpatterns = [ # Put this before the admin include so that it is found first. # Don't forget to replace 'app-label' and 'blog' with the # appropriate names, if different. url(r'^admin/app-label/blog/(?P<blog_id>\d+)/?$', RedirectView.as_view(pattern_name='edit_blog', permanent=False)), # The actual admin urls. url(r'^admin/', include(admin.site.urls)), # The urls to redirect to. Note that naming them makes # redirecting much easier. url(r'^add-update-blog/?$', views.add_update_blog, name='add_blog'), url(r'^add-update-blog/save/(?P<blog_id>\d+)/?$', views.add_update_blog, name='edit_blog'), # All your other urls... ] 
0
source

Source: https://habr.com/ru/post/1259714/


All Articles