Modifying or updating a post does not return an HTTPResponse object

1

Starting in Django I ran into a problem that I can not solve.

The application is a blog. of the DjangoGirl tutorial.

The invaluable help is appreciated.

When I want to add a post from a form or I want to update a post I get the following error:

in urls.py the code is as follows:

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^$', views.post_list),
    url(r'^post/(?P<pk>[0-9]+)/$', views.post_detail, name= 'post_detail'),
    url(r'^post/new/$', views.post_new, name='post_new'),
    url(r'^post/(?P<pk>[0-9]+)/edit/$', views.post_edit, name= 'post_edit')
]

and in views.py the code is as follows:

from django.shortcuts import render, get_object_or_404
from django.utils import timezone
from .models import Post
from .forms import PostForm
from django.shortcuts import redirect

def post_list(request):
    posts =  Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date')
    return render(request, 'blog/post_list.html', {'posts': posts})

def post_detail(request, pk):
    post = get_object_or_404(Post, pk=pk)
    return render(request, 'blog/post_detail.html', {'post':post})

def post_new(request):
    if request.method == "GET":
        form = PostForm(request.GET)
        if form.is_valid():
            post = form.save(commit=False)
            post.author = request.user
            post.published_date = timezone.now()
            post.save()
            return redirect('post_detail', pk=post.pk)
        else:
            form = PostForm()
        return render(request, 'blog/post_edit.html', {'form': form})

def post_edit(request, pk):
    post = get_object_or_404(Post, pk=pk)
    if request.method == "GET":
        form = PostForm(request.GET,instance=post)
        if form.is_valid():
            post = form.save(commit=False)
            post.author = request.user
            post.save()
            return redirect('post_detail', pk=post.pk)
        else:
            form = PostForm(instance=post)
        return render(request, 'blog/post_edit.html', {'form': form})
    
asked by Seba 21.04.2018 в 21:39
source

3 answers

0

Use GET instead of POST ... I do not know if you're doing the front with templates or you use postman to test .. if you use html it would be something like   <form method="GET"> tus inputs </form> ... or failing that, put the if as: if request.method == "GET" or request.method == "POST":

    
answered by 22.04.2018 в 18:39
0

The first problem is with the tabulation because you surely copied the code and you hit it but you did not realize that this else does not belong to that if , it should be like this:

def post_new(request):
    if request.method == "POST":
        form = PostForm(request.POST)
        if form.is_valid():
            post = form.save(commit=False)
            post.author = request.user
            post.published_date = timezone.now()
            post.save()
            return redirect('post_detail', pk=post.pk)
    else:
        form = PostForm()
    return render(request, 'blog/post_edit.html', {'form': form})

in the way you had it declared the else belonged to the condition if form.is_valid(): when you should belong to: if request.method == "POST": therefore did not return anything.

    
answered by 24.04.2018 в 17:46
0

Solve the problem by adding an else to if form.is_valid ()

def post_new(request):
    if request.method == "POST":
        form = PostForm(request.POST)
        if form.is_valid():
            post = form.save(commit=False)
            post.author = request.user
            post.published_date = timezone.now()
            post.save()
            return redirect('post_detail', pk=post.pk)
        else:
            return render(request, 'blog/post_edit.html', {'form': form})
    else:
        form = PostForm()
    return render(request, 'blog/post_edit.html', {'form': form})
    
answered by 27.04.2018 в 04:39