form.is_valid()条件在使用tinymce时在html文件中使用模式为textarea后给出false

时间:2014-06-11 08:10:32

标签: django tinymce

我正在使用tinymce和django。我创建了models.py(forms.py的内容也仅包含在models.py中)。问题是,当在tinymce init块中的html文件form.html中时: 我使用:mode:" textareas",  我的数据没有在数据库中更新,我在表单的textarea中输入(我相信我的表单没有被验证)但是当我评论它时,表单的textarea加载了tinymce的非常基本的功能但是表单有效,当我点击保存按钮时数据更新到数据库。

models.py:

在这里输入代码

from django.db import models
from django.forms import ModelForm
from django.contrib.admin import widgets
from tinymce.widgets import TinyMCE
from django import forms
# Create your models here.

class HomePage(models.Model):
    homecopy = models.TextField()

    def __unicode__(self):
        return 'Home Page Copy'

class HomePageForm(ModelForm):
    class Meta:
        model = HomePage
        fields = ['homecopy']
        #widgets = {'homecopy': forms.Textarea(attrs={'required':True})}    
        widgets = {'homecopy': TinyMCE(attrs={'cols': 80, 'rows': 30})}

form.html:

enter code here
{% load static %}
{% load staticfiles %}
<html>
<head> <title> TinyMce </title>
<script type="text/javascript" src="{% static "js/tiny_mce/tiny_mce.js" %}"></script>
<script type="text/javascript">
tinyMCE.init({
    // General options
    mode : "textareas",
    theme : "advanced",
    plugins : "pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,wordcount,advlist,autosave",

// Theme options
theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,fontselect,fontsizeselect,fullscreen,code",
theme_advanced_buttons2 : "bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,|,forecolor,backcolor",
theme_advanced_buttons3 : "tablecontrols,|,hr,sub,sup,|,charmap",

theme_advanced_toolbar_location : "top",
theme_advanced_toolbar_align : "left",
theme_advanced_statusbar_location : "bottom",
theme_advanced_resizing : true,

// Example content CSS (should be your site CSS)
//content_css : "/css/style.css",

template_external_list_url : "lists/template_list.js",
external_link_list_url : "lists/link_list.js",
external_image_list_url : "lists/image_list.js",
media_external_list_url : "lists/media_list.js",

// Style formats
style_formats : [
    {title : 'Bold text', inline : 'strong'},
    {title : 'Red text', inline : 'span', styles : {color : '#ff0000'}},
    {title : 'Help', inline : 'strong', classes : 'help'},
    {title : 'Table styles'},
    {title : 'Table row 1', selector : 'tr', classes : 'tablerow'}
],

width: '700',
height: '400'
});
</script>
</head>
<body>
<style>
#text{
height : 500px;
width : 1000px;
}

</style>
</head>
<body>
<p><h2> Hi {{ name }} ..Save your text... </h2></p>
</br>
<form id = "form" action = "/myform/" method = "post">{% csrf_token %}
{{ form.as_p }}
<p><input type = "submit" id = "butt" value = "save"/></p>
</form>
</body>
</html>

views.py:

from django.shortcuts import render_to_response
from django.template import RequestContext,Context
from django.template.loader import get_template
from myapp.models import HomePage,HomePageForm
from django.http import HttpResponse,HttpResponseRedirect
from django.core.context_processors import csrf
from tinymce.widgets import TinyMCE

def MainHomePage(request):
    homepage = HomePage.objects.get(pk = 1)
    context = {'homepage' : homepage}
    return render_to_response('index.html',context,context_instance=RequestContext(request))

def display(request):
    context = {'name' : request.user}
    return render_to_response('form.html',context,context_instance = RequestContext(request))

def save_form(request):
    if request.method == 'POST':
        #print request.POST.get('homecopy','')
        #print "$$$$$$$$$$$$$$$$$$"
        form = HomePageForm(request.POST)
        if form.is_valid():
            #print "%%%%%%%%%%%%%%%%"
            form.save()
            return render_to_response('display.html')
    args = {}
    args.update(csrf(request)) 
    args['form'] = HomePageForm()
    return render_to_response('form.html',args)

admin.py:

from django.contrib import admin
from myapp.models import HomePage
class TinyMCEAdmin(admin.ModelAdmin):
    class Media:
        js = ('/static/static/js/tiny_mce/tiny_mce.js','/static/static/js/tiny_mce/textareas.js',)
admin.site.register(HomePage, TinyMCEAdmin)  

主要项目的urls.py:

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

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'tiny.views.home', name='home'),
    # url(r'^tiny/', include('tiny.foo.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    url(r'^admin/', include(admin.site.urls)),
    url(r'^myform/', 'myapp.views.save_form'),
    (r'^tinymce/', include('tinymce.urls')),
    (r'^$','myapp.views.MainHomePage'),
)




project folder structure : 
tiny - 
      tinymce
      tiny -
            setiings.py, urls,py , wsgi.py, __init__.py
      myapp - 
           admin.py
           models.py 
           __init__.py
           views.py
           tests.py
      static - 
            static -
                js - 
                   tiny_mce - 
                       langs, plugins, themes, utils, textareas.js, tiny_mce.js, tiny_mce_popup.js, tiny_mce_src.js
     static_only

     templates - 
            display.html, form.html, index.html
     manage.py
     test - database

0 个答案:

没有答案