lqez / django-summernote

Simply integrate Summernote editor with Django project.
MIT License
1.04k stars 227 forks source link

How fix problems with adding pictures and objects? #186

Open nurzhannogerbek opened 7 years ago

nurzhannogerbek commented 7 years ago

Hello! First of all THANK YOU for this project!

I have model Specification with field description. I am tring to create form where user can create new Specification object by adding text, picture to description field. I have 2 problems.

1) When I add picture from computer I dont see it inside field however I see that picture in media_root. How to fix this problem?! default

2) When I click submit button, it dont create new Specification object. It just show my form with content without editing. I thought next my code would be create new object and update list of objects.

settings.py:

INSTALLED_APPS = [
   'django_summernote'
]

STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static_root')
MEDIA_ROOT = os.path.join(BASE_DIR, 'media_root')
MEDIA_URL = '/media/'

models.py:

class Specification(models.Model):
    *******
    description = models.TextField(_('Description'))
    *******

forms.py:

class SpecificationForm(forms.ModelForm):
    description = forms.CharField(widget=SummernoteInplaceWidget(attrs={'width': '100%'}))

    class Meta:
        model = Specification
        fields = ('description',)

urls.py:

urlpatterns = i18n_patterns(
    url(r'^summernote/', include('django_summernote.urls')),
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

views.py:

def specification_add(request):
    data = dict()
    if request.method == 'POST':
        specification_form = SpecificationForm(request.POST)
        if specification_form.is_valid():
            specification = specification_form.save(commit=False)
           ***Some code***
            specification.save()
            data['form_is_valid'] = True
           specifications = Specification.objects.all()
           context = {'specifications': specifications}
                context.update(csrf(request))
                data['html_specification'] = render_to_string('project/specification_list.html', context)
        else:
            data['form_is_valid'] = False
    else:
        specification_form = SpecificationForm()
    context = {'specification_form': specification_form}
    data['html_specification_form'] = render_to_string('project/specification_add.html', context, request=request)
    return JsonResponse(data)

specification_add.html:

{% load widget_tweaks %}

<form method="post" action="">
    {% csrf_token %}

    {{ specification_form.media }}

    {% for field in specification_form %}
    <div class="form-group{% if field.errors %} has-danger{% endif %}">
         <label class="form-control-label" for="{{ field.id_for_label }}">{{ field.label }}</label>
         {% render_field field class="form-control" %}
         {% for error in field.errors %}
                <div class="form-control-feedback">{{ error }}</div>
          {% endfor %}
     </div>
     {% endfor %}

     <button type="submit" class="btn btn-primary">Create</button>
</form>
maxclax commented 7 years ago

I have same problem via pictures