pythondjangomarkdowndjango-markdownx

Django render admin panel with markdown fields


I am having trouble with markdown on my Django application. I am writing an admin panel and want to use the markdown field. I did everything that is needed to install it:

my settings:

INSTALLED_APPS = [
    'django.contrib.staticfiles',
    'rest_framework',
    'markdownx',
]

models.py

class ProductDescription(models.Model):
    top_description = MarkdownxField(max_length=1500)
    bottom_description = MarkdownxField(max_length=1500, blank=True, null=True)

urls

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('my_app.urls')),
    path('markdownx/', include('markdownx.urls')),
]

admin.py

class ProductDescriptionAdmin(admin.ModelAdmin):
    model = ProductDescription
    fields = (
        'top_description',
        'bottom_description',
     )

admin.site.register(ProductDescription, ProductDescriptionAdmin)

Then I executed these commands:

python manage.py makemigrations
python manage.py collectstatic
python manage.py migrate

I'm expecting that I will be able to see a preview of my markdown field as it showed here:

https://miro.medium.com/v2/resize:fit:4800/format:webp/1*XD_0oQfMsIhmHS0MJ0LP2g.png

it looks like everything works fine except the preview, it tries to load the preview from http://localhost:8000/markdownx/markdownify/ but it receives 404 all the time

it looks like ulrs markdownx/* are not registered and I don't know why is that :( am I missing something here? enter image description here


Solution

  • I found a solution, the problem was the order of the urls:

    urlpatterns = [
        path('admin/', admin.site.urls),
        path('', include('my_app.urls')),
        path('markdownx/', include('markdownx.urls')),
    ]
    

    the empty path should be at the very end:

    urlpatterns = [
        path('admin/', admin.site.urls),
        path('markdownx/', include('markdownx.urls')),
        path('', include('my_app.urls')),
    ]