HTML Button Error

Asked 2 years ago, Updated 2 years ago, 58 views

I am trying to create a blog using Python's Django according to the tutorial DjangoGirls.So I created a blog post page and was able to save the data to the database, but I couldn't move to the next screen and got an error.

Enter a description of the image here

post_edit.html

{%extends'posts/base.html'%}
{% US>block content%}
    <h1>New post</h1>
    <form method="POST" class="post-form">{%csrf_token%}
        {{ form.as_p}}
        <button type="submit" class="save btn btn-default">Save</button>
    </form>
{% endblock%}

views.py

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


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

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

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.save()
            return redirect ('posts.views.post_detail', pk=post.pk)
    else:
        form = PostForm()
    return render(request, 'posts/post_edit.html', {'form':form})

def post_edit(request,pk):
    post=get_object_or_404(Post,pk=pk)
    if request.method=="POST":
        form = PostForm (request.POST, instance=post)
        if form.is_valid():
            post=form.save(commit=False)
            post.author=request.user
            post.save()
            return redirect ('posts.views.post_detail', pk=post.pk)
    else:
        form = PostForm (instance=post)
    return render(request, 'posts/post_edit.html', {'form':form})

base.html
{% load staticfiles%}

<html>
    <head>
        <title> Blog</title>
        <link href="http://fonts.googleapis.com/css?family=Lobster&subset=latin,latin-ext"rel="stylesheet" type="text/css">
    </head>
    <body>
        <div class="page-header">
            <h1><a href="/"> Blog</a></h1>
            <input type="button" value="post" onClick="location.href='/post/new';">
        </div>

        <div class="content container">
            <div class="row">
                <div class="col-md-8">
                {% US>block content%}
                {% endblock%}
                </div>
            </div>
        </div>
    </body>
</html>

`

python html css django

2022-09-29 22:04

1 Answers

If you look at the error screen, the Request URL is http://127.0.0.1/post/5/edit/posts.views.post_detail.
Perhaps the view in redirect('posts.views.post_detail', pk=post.pk) is incorrectly specified, and the string 'posts.views.post_detail' is still used in the URL.
I need to change 'posts.views.post_detail' to a view that exists, but I guess it's 'post.views.post_detail' from the URL.

See the redirect specification below.
https://docs.djangoproject.com/en/1.10/topics/http/shortcuts/ #redirect


2022-09-29 22:04

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.