Home > Mobile >  I have trouble in editing a post in django project
I have trouble in editing a post in django project

Time:01-10

I am working on a Django project where I can create a post and also edit a specific post using the post's id.

views.py

@login_required
def editpost(request,id):
  postform = NewPost.objects.get(id=id)

  if request.method == "GET":
    post = NewPostForm(request.GET, instance=postform)
    user = request.user.id
    timestamp = datetime.now()
    if post.is_valid:
        post = post.save()
        postdata = post.save(id = id,post = post,user = user,timestamp = timestamp)
        postdata.save()
        return HttpResponseRedirect(reverse("index"))

    return render(request,"network/postform.html",{
      "postform" : postform,
      "id" : id,
    })

urls.py

urlpatterns = [
   path("", views.index, name="index"),
   path("login", views.login_view, name="login"),
   path("logout", views.logout_view, name="logout"),
   path("register", views.register, name="register"),
   path("postform", views.createpost, name="postform"),
   path("editform/<int:id>",views.editpost,name="editpost"),
]

editform.html

{% extends 'network/layout.html' %}

{% block body %}
<div style="margin: 70px 10px 10px 10px;">
    <!-- <h1>Create New Post</h1> -->
    <div  style="margin: 10px 10px 10px 10px; position: relative;">
        <div style="margin: 10px 10px 10px 10px;">
            <label>New post</label>
            <form id="NewPostForm" action="{% url 'index' %}" method="GET">
                {% csrf_token %}
                <div >
                    {{ postform }}
                </div>
            
                <input  type="Submit" value="Submit">
            </form>
        </div>
    </div>
</div>
{% endblock %}

I have trouble routing my template.

enter image description here

What should I do now!?

CodePudding user response:

Looks like the error is actually in like.html. When rendering the edit link, django tries to find using {% url 'editpost' postform.id %}. What django is trying to tell you, is that although editpost is in your urls.py, the arguments provided don't match.

I didn't actually check that, but it appears that you have to pass arguments with their names specified in urls.py, so since the urlconf looks like "editform/<int:id>", you have to pass {% url 'editpost' id=postform.id %}.

  •  Tags:  
  • Related