H
H
howuu2018-08-17 22:15:40
Django
howuu, 2018-08-17 22:15:40

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 %}

I want that when editing the form, the necessary fields are already inserted, an example from the django girls blog
5b771ec362f54310008612.png
Editing goes like this:
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})

When I made the form not through widget_tweaks everything worked, now only the picture is visible
5b771ed062841228876823.png
How to solve the problem?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question