Answer the question
In order to leave comments, you need to log in
How to implement the preview feature like in Toaster?
Hello, please help me understand. The Toaster has a preview of the question being created. I'm trying to create something similar in my Django project.
There is an article editing form with two buttons and two fields. The first button is submit, the second button is preview. One of the fields is a CharField and it stores the html markup. A wysiwyg editor has thrown on this field. The form itself is in a modal window.
There is a following task. When you click on the second button (preview), I want to send a post request via ajax to send data from the fields to Redis. Next, change the content of the form to this temporary data in order to get some kind of preview.
I'm trying to link my Django project to Redis. I use this app for this: django-redis .
settings.py:
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://127.0.0.1:6379/1",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
}
}
}
<form method="post" action="{% url 'article:article_edit' article.id %}" class="article-edit-form" enctype="multipart/form-data">
{% csrf_token %}
<a id="article-preview-btn" class="btn btn-info" data-url="{% url 'article:article_preview' article.id %}">
<span>{% trans "Превью" %}</span>
</a>
<button type="submit" class="btn btn-success">{% trans 'Обновить' %}</button>
</form>
from django.views.decorators.cache import cache_page
url(r'^(?P<pk>\d+)/preview/$',
cache_page(15 * 60)(ArticlePreview.as_view()),
name='article_preview'),
class ArticlePreview(FormView):
form_class = ArticleForm
def form_valid(self, form):
data = dict()
data['session'] = self.request.session
data['title'] = form.cleaned_data['title']
data['body'] = form.cleaned_data['body']
return JsonResponse(data)
$("#article-modal").on("click", "#article-preview-btn", function () {
var btn = $(this);
$.ajax({
url: btn.attr("data-url"),
type: 'post',
dataType: 'json',
success: function (data) {
???
}
});
});
Internal Server Error: /article/1/preview/
Traceback (most recent call last):
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35\lib\site-packages\django\core\handlers\exception.py", line 41, in inner
response = get_response(request)
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35\lib\site-packages\django\utils\decorators.py", line 149, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35\lib\site-packages\django\views\generic\base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35\lib\site-packages\django\views\generic\base.py", line 88, in dispatch
return handler(request, *args, **kwargs)
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35\lib\site-packages\django\views\generic\edit.py", line 181, in post
form = self.get_form()
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35\lib\site-packages\django\views\generic\edit.py", line 45, in get_form
return form_class(**self.get_form_kwargs())
TypeError: __init__() missing 1 required positional argument: 'user'
[18/Dec/2017 23:34:23] "POST /article/1/preview/ HTTP/1.1" 500 16682
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question