direduryan
direduryan

Reputation: 33

Django form action "." reloads same page with missing slug

I'm following a basic tutorial which adds commenting to a blog post detail page. I am navigating to the detail page with the absolute_url method and it works perfectly fine.

def get_absolute_url(self):
return reverse('blog:post_detail',
               args=[self.publish.year,
                     self.publish.month,
                     self.publish.day,
                     self.slug])

Here is a sample url created by the get_absolute_url

http://localhost:8000/blog/2019/5/2/second-post

However, when I submit the form within the detail page with the action=".", it only returns the date parameters and missing the slug part.

<form action="." method="post">
  {% csrf_token %}
  {{ comment_form.as_p }}
  <p><input type="submit" value="Add comment"></p>
</form>

Here is the returned url

http://localhost:8000/blog/2019/5/2/

adding action="{{ post.get_absolute_url }}" seems to solve the problem but the book I am following Django 2 By Example tells it should just work fine with action="."

I'm new to Django and Development so thank you for your help and understanding if the question is noob in any way :)

Upvotes: 3

Views: 321

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599758

You didn't show your URL patterns, but they should all end with /. So the original URL of http://localhost:8000/blog/2019/5/2/second-post should be http://localhost:8000/blog/2019/5/2/second-post/. For example, the pattern might be:

path('blog/<int:year>/<int:month>/<int:day>/<slug:slug>/', views.blog, 'blog'),

which ends with a slash and so the generated path will also end with a slash. Then posting to "." will work properly.

Upvotes: 3

Related Questions