I
I
i_ikigai2021-01-04 13:40:16
Django
i_ikigai, 2021-01-04 13:40:16

Why is the page not found after sending the email?


After submission , outputs "Page not found (404) "

def post_share(request, post_id):
    post = get_object_or_404(Post, id=post_id, status='published')
    sent = False

    if request.method == 'POST':
        form = EmailPostForm(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            post_url = request.build_absolute_uri(post.get_absolute_url())
            subject = '{} ({}) recommends you reading "{}"'.format(cd['name'], cd['email'], post.title)
            message = 'Read "{}" at {}\n\n{}\'s comments: {}'.format(post.title, post_url, cd['name'], cd['comments'])
            send_mail(subject, message, '[email protected]', [cd['to']])
            sent = True

    else:
        form = EmailPostForm()

    return render(request, 'blog/post/share.html', {'post': post,
                                                    'form': form,
                                                    'sent': sent})

URLs
urlpatterns = [
    path('', views.PostListView.as_view(), name='post_list'),
    path(r'^(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/' r'(?P<post>[-\w]+)/$',
         views.post_detail,
         name='post_detail'),
    path(r'^(?P<post_id>\d+)/share/$', views.post_share, name='post_share'),
]

share
{% extends "blog/base.html" %}
{% block title %}Share a post{% endblock %}
{% block content %}
    {% if sent %}
        <h1>E-mail successfully sent</h1>
        <p>
            "{{ post.title }}" was successfully sent to {{ cd.to }}.
        </p>
    {% else %}
        <h1>Share "{{ post.title }}" by e-mail</h1>
        <form action="." method="post">
            {{ form.as_p }}
            {% csrf_token %}
            <input type="submit" value="Send e-mail">
        </form>
    {% endif %}
{% endblock %}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
i_ikigai, 2021-01-04
@i_ikigai

You need to replace the line with

path('<int:post_id>/share/', views.post_share, name='post_share'),

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question