Answer the question
In order to leave comments, you need to log in
Editing a blog post via the widget_tweaks form?
There is a blog entry that you can create, edit and delete.
The creation goes like this:
url(r'^post/new/$', views.post_new, name='post_new'),
def post_new(request):
if request.method == "POST":
form = PostForm(request.POST, request.FILES)
if form.is_valid():
post = form.save(commit=False)
post.published_date = timezone.now()
post.save()
return redirect('/', pk=post.pk)
else:
form = PostForm(request.POST, request.FILES)
return render(request, 'blog/post_edit.html', {'form': form})
{% load widget_tweaks %}
{% block content %}
<h2>Новий пост</h2>
<div class="col-md-12 col-lg-8 main-content">
<form method="POST" enctype="multipart/form-data">{% csrf_token %}
<div class="row">
<div class="col-md-4 form-group">
{% render_field form.title type="text" id="name" class="form-control " title="Заголовок" %}
</div>
</div>
<div class="row">
<div class="col-md-12 form-group">
{% render_field form.text name="message" id="message" class="form-control " %}
</div>
</div>
<div class="row">
<div class="col-md-12 form-group">
{% render_field form.image %}
</div>
</div>
<div class="row">
<div class="col-md-6 form-group">
<input type="submit" value="Опубликовать" class="btn btn-primary">
</div>
</div>
</form>
</div>
{% endblock %}
url(r'^post/(?P<pk>[0-9]+)/edit/$', views.post_edit, name='post_edit'),
def post_edit(request, pk):
post = get_object_or_404(Post, pk=pk)
if request.method == "POST":
form = PostForm(request.POST, request.FILES, instance=post)
if form.is_valid():
post = form.save(commit=False)
post.published_date = timezone.now()
post.save()
return redirect('/', pk=post.pk)
else:
form = PostForm(request.POST, request.FILES, instance=post)
return render(request, 'blog/post_edit.html', {'form': form})
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