Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
383 views
in Technique[技术] by (71.8m points)

Django Error: Reverse for 'editpost' with arguments '('',)' not found. 1 pattern(s) tried: ['create/editpost/(?P<pk>[^/]+)/$']

I get this error when trying to use the template that should link to the edit page.

I have seen someone do this using UpdateView but it gives the same error when i try it

here is some more relevant info SO says i need to add more text so here is some more text (ignore this): more text more more text

templates:

{% block content %}
<div class="content_section">
    <div class="btn_container">
        <a class="new_post" href="{% url 'newpost' %}"><button class="new_post_btn">New Post</button></a>
    </div>
    <div class="content_section_two">
        {% for product in products %}
            <div class="post_short_container">
                <div class="title_container">
                
                    <a href="#" class="post_link">
                        <div><b>{{ product.title }}</b></div>
                    </a>
                    
                </div>
                <div class="right_btns">
                    <a href="{% url 'editpost' post.id %}" class="edit_link">Edit</a>
                    <a href="#" class="stats_link">Stats</a>
                    <div class="ad_btn_container">
                        <div class="ad_btn">Ad</div>
                    </div>
                    <div class="delete_btn_container">
                        <div class="delete_btn">Delete</div>
                    </div>
                </div>
            </div>
        {% endfor %}
    </div>

</div>

{% endblock content %}

urls.py:

urlpatterns = [
    path('editpost/<int:pk>/', views.editPost, name='editpost'),
]

views.py

def editPost(request, pk):
    post = Post.objects.get(id=pk)
    form = NewPost(instance=post)

    if request.method == 'POST':
        form = NewPost(request.POST, request.FILES, instance=post)
        if form.is_valid():
            form.save()
            return redirect('myposts')

    return render(request, 'create/new_post.html', {'form': form})

ListView:

def myPosts(request):

    context = {
         'products': Post.objects.all()
    }

    return render(request, 'create/my_posts.html', context)

class PostListView(ListView):
    model = Post
    template_name = 'my_posts.html'
    context_object_name = 'products'
question from:https://stackoverflow.com/questions/65545410/django-error-reverse-for-editpost-with-arguments-not-found-1-pattern

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...