【问题标题】:Django Markdown Editor does not show upDjango Markdown 编辑器不显示
【发布时间】:2015-02-10 19:39:25
【问题描述】:

我正在创建我的第一个 Django 应用程序(我也是 Python 新手,所以问题可能出在任何地方。)

我正在一步一步地学习本教程,在 5:53 (here) 获得 HTML 编辑器,但我仍然在 http://127.0.0.1:8000/admin/blog/entry/add/ 获得默认 TextField

任何有关诊断问题的帮助将不胜感激。 谢谢!

我的文件:

projects/qblog/blog/admin.py:

from django.contrib import admin
from . import models
from django_markdown.admin import MarkdownModelAdmin

class EntryAdmin(MarkdownModelAdmin):
    list_display = ("title" , "created")
    prepopulated_fields = {"slug" : ("title", )}

admin.site.register(models.Entry, EntryAdmin)

projects/qblog/qblog/urls.py:

from django.conf.urls import patterns, include, url
from django.contrib import admin


urlpatterns = patterns(
    '',
    url(r'^admin/', include(admin.site.urls)),
    url(r'^markdown/', include("django_markdown.urls")),
)

projects/qblog/blog/models.py:

from django.db import models

# Create your models here.

class EntryQuerySet(models.QuerySet):
    def published(self):
        return self.filter(publish=True)

class Entry(models.Model):
    title=models.CharField(max_length=200)
    body = models.TextField()
    slug = models.SlugField(max_length=200,unique = True)
    publish = models.BooleanField(default=True)
    created = models.DateTimeField(auto_now_add = True)
    modified = models.DateTimeField(auto_now = True)

    objects = EntryQuerySet.as_manager()

    def __str__(self):
        return self.title
    class Meta:
        verbose_name = "Blog Entry"
        verbose_name_plural = "Blog Entries"
        ordering = ["-created"]

projects/qblog/qblog/settings.py:

"""
Django settings for qblog project.

Generated by 'django-admin startproject' using Django 1.9.dev20150210173028.

For more information on this file, see
https://docs.djangoproject.com/en/dev/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/dev/ref/settings/
"""

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/dev/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'secretkey'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    'blog',
    'django_markdown',
]

MIDDLEWARE_CLASSES = [
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',
]

ROOT_URLCONF = 'qblog.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'qblog.wsgi.application'


# Database
# https://docs.djangoproject.com/en/dev/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}


# Internationalization
# https://docs.djangoproject.com/en/dev/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/dev/howto/static-files/

STATIC_URL = '/static/'

【问题讨论】:

    标签: python django django-admin django-apps


    【解决方案1】:

    在视频的 cmets 中,您可以得到答案。修改下一个文件:

    models.py

    from django_markdown.models import MarkdownField
    ...
    body = MarkdownField()
    

    settings.py

    ...
    # Static files (CSS, JavaScript, Images)
    # https://docs.djangoproject.com/en/1.7/howto/static-files/
    
    STATIC_URL = '/static/'
    STATIC_ROOT = os.path.join(BASE_DIR, "static")
    
    # Markdown
    MARKDOWN_EDITOR_SKIN = 'simple'
    

    urls.py

    ...
    from yourapp import settings
    if settings.DEBUG:
        from django.conf.urls.static import static
        urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    

    在 shell 中运行:

    python manage.py collectstatic
    

    【讨论】:

    • 谢谢。 +1。我搬到了另一台电脑上,但我会试试这个,并尽快通知你。
    • 我认为最后一行有错字。我无法让它工作。 collectstatic 运行,我重新启动服务器,没有任何改变。
    • 在开发中提供静态文件,在你的底部添加 urls.py 的代码(为你的应用更改你的应用程序)
    • 对我来说,收集静态文件是解决问题所需的唯一事情。谢谢!
    【解决方案2】:

    我也有同样的问题。但是,当我添加没有urls.py 部分的 iago1460 先生的代码时,运行后:

    python manage.py collectstatic,
    

    django_markdown 可以工作,我使用 python 2.7 和 django 1.8 版本

    这里是 urls.py 代码

    from django.conf.urls import  include, url
    from django.contrib import admin
    #import settings
    
    #if settings.DEBUG:
        #from django.conf.urls.static import static
        #urlpatterns += static(settings.STATIC_URL,document_root=settings.STATIC_ROOT)
    
    urlpatterns = [
        # Examples:
        # url(r'^$', 'cblog.views.home', name='home'),
        # url(r'^blog/', include('blog.urls')),
    
        url(r'^admin/', include(admin.site.urls)),
        url(r'^markdown/', include("django_markdown.urls")),
        url(r'^',include('blog.urls')),
    

    ]

    这是我的项目文件,我的项目名称是 cblog,app 是 blog:

    djtest/cblog/cblog:

    __init__.py  __init__.pyc  settings.py  settings.pyc  urls.py  urls.pyc  wsgi.py  wsgi.pyc
    

    djtest/cblog:

     blog  cblog  db.sqlite3  manage.py  static
    

    【讨论】:

      【解决方案3】:

      是的,我在尝试 qblog 教程时也发现了这个问题。我正在使用 Python 3.4 和 Django 1.8.1,但是在安装 markdown 包的步骤中,我决定将包更改为 Django CKEditor。

      访问https://pypi.python.org/pypi/django-ckeditor进行安装。

      CKEditor 正确安装后,如果在运行服务器时发现 import forms.util flattat 有问题。 更改 ckeditor 文件夹下的 widgets.py(在 windows 环境下,该目录位于 C:\Python34\Lib\site-packages\ckeditor 下),然后更改行:

      从 django.forms.util 导入 flatatt

      从 django.forms.utils 导入 flatatt

      了解如何使用 CKEditor,然后在您的 qblog 上实现它。 结果会是这样的:

      【讨论】:

        【解决方案4】:

        试试这个更简单的方法:

        admin.py 中:

        from django_markdown.admin import MarkdownModelAdmin
        from django_markdown.widgets import AdminMarkdownWidget
        from django.db.models import TextField
        
        
        class EntryAdmin(MarkdownModelAdmin):
            ... #your_code
            formfield_overrides = {TextField: {'widget': AdminMarkdownWidget}}
        
        admin.site.register(models.Entry, EntryAdmin)
        

        【讨论】:

          【解决方案5】:

          当您使用 python 2x 时会发生这种情况 现在,发生的情况是样式表和 javascript 没有加载

          所以你可以做的是:

          from django_markdown.widgets import AdminMarkdownWidget
            from django.db.models import TextField
          
            class EntryAdmin(MarkdownModelAdmin):
                list_display = ("title", "created")
                prepopulated_fields = {"slug": ("title",)}
                # Next line is a workaround for Python 2.x
                formfield_overrides = {TextField: {'widget': AdminMarkdownWidget}}
          

          这会起作用的!!!!!!!!!!!!!

          【讨论】:

            【解决方案6】:

            Python 2.x 你需要补充一下:

            formfield_overrides = {TextField: {'widget': AdminMarkdownWidget}}

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2017-08-05
              • 1970-01-01
              • 2010-11-14
              • 2016-08-18
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多