Updating Slug to other pages such as new, delete, update to avoid Page 404 error

Submitted 3 years, 11 months ago
Ticket #7
Views 240
Language/Framework Django
Priority Low
Status Closed

Hi there, 

Thanks to you we have found a solution for moving from PK to slug in the detailed view but now the other pages are getting the same error Page 404 Error. 

Here are the urls: 

urlpatterns = [
    path('', PostListView.as_view(), name='score'),
    path('<slug:slug>/', PostDetailView.as_view(), name='post-detail'),
    path('new/', PostCreateView.as_view(), name='post-create'),
    path('user/<str:username>', UserPostListView.as_view(), name='user-posts'),
    path('<slug:slug>/update/', PostUpdateView.as_view(), name='post-update'),
    path('<slug:slug>/delete/', PostDeleteView.as_view(), name='post-delete')
]

Here is the views.py: 

class PostDetailView(DetailView):
    model = Post
    template_name = "post_detail.html"

    def get_context_data(self, *args, **kwargs):
        context = super(PostDetailView, self).get_context_data()
        post = get_object_or_404(Post, slug=self.kwargs['slug'])
        total_likes = post.total_likes()
        liked = False
        if post.likes.filter(id=self.request.user.id).exists():
            liked = True
        context["total_likes"] = total_likes
        context["liked"] = liked
        return context

class PostCreateView(LoginRequiredMixin, SuccessMessageMixin, CreateView):
    model = Post
    fields = ['title', 'design']
    template_name = "post_form.html"
    success_url = "/score"
    success_message = "Your Design has been submitted for Review"

    def form_valid(self, form):
        form.instance.designer = self.request.user
        return super().form_valid(form)


class PostUpdateView(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
    model = Post
    fields = ['title']
    template_name = "post_form.html"
    success_url = "/score"

    def form_valid(self, form):
        form.instance.designer = self.request.user
        return super().form_valid(form)

    def test_func(self):
        post = self.get_object()
        if self.request.user == post.designer:
            return True
        return False


class PostDeleteView(LoginRequiredMixin, UserPassesTestMixin, DeleteView):
    model = Post
    template_name = "post_confirm_delete.html"
    success_url = '/score'

    def test_func(self):
        post = self.get_object()
        if self.request.user == post.designer:
            return True
        return False

Submitted on May 23, 20
add a comment

1 Answer

Verified

what is the issue?.. in which view you are getting an error?

Submitted 3 years, 10 months ago

creating a new post

- ahmed.hisham87 3 years, 10 months ago

try success_url = "/score/"

- Vengat 3 years, 10 months ago

still it is showing Error Raised by: score.views.PostDetailView

- ahmed.hisham87 3 years, 10 months ago

can we join a quick call to fix it ?

- ahmed.hisham87 3 years, 10 months ago

then try thissuccess_url = reverse_lazy('score:score) import this as well from django.urls import reverse_lazy it should work

- Vengat 3 years, 10 months ago

do you mean success_url = reverse_lazy('score':score) ? as there is a syntax error and when I change it to success_url = reverse_lazy('score') it return back with Page not found (404)

- ahmed.hisham87 3 years, 10 months ago

this is the format success_url = reverse_lazy('appname:redirectpage')

- Vengat 3 years, 10 months ago

Still Page not found (404), the issue is with reaching the page itself not after posting

- ahmed.hisham87 3 years, 10 months ago

which page you need to redirect after success?

- Vengat 3 years, 10 months ago

score.html, but the issue is can't reach this page: path('new/', PostCreateView.as_view(), name='post-create'),

- ahmed.hisham87 3 years, 10 months ago

you mean you are not able to login create page?

- Vengat 3 years, 10 months ago

Exactly the create page is showing Page 404 error I can't log in to it

- ahmed.hisham87 3 years, 10 months ago

what is the path of this html file post_form.html

- Vengat 3 years, 10 months ago

path('new/', PostCreateView.as_view(), name='post-create'),

- ahmed.hisham87 3 years, 10 months ago

no i mean template path?

- Vengat 3 years, 10 months ago

there is no other paths

- ahmed.hisham87 3 years, 10 months ago

ok in other words, what is the location of post_form.html file

- Vengat 3 years, 10 months ago

it is inside the templates folder

- ahmed.hisham87 3 years, 10 months ago

show me the full path, i have a feeling you have an issue in template files

- Vengat 3 years, 10 months ago

C:\Users\Ahmed\Desktop\Mostiques 4.3\templates\post_form.html

- ahmed.hisham87 3 years, 10 months ago

ok file should be under this folder /templates/score/post_form.html

- Vengat 3 years, 10 months ago

there are no folders inside templates all html, it was working yesterday until we adjusted the slugs

- ahmed.hisham87 3 years, 10 months ago

slug & this one different concept. make sure you are rendering the right URL & template . over all i dont see any issues. may be some typo error somewhere

- Vengat 3 years, 10 months ago

I have reviewed every step but the Error Raised by: score.views.PostDetailView

- ahmed.hisham87 3 years, 10 months ago

how are you calling create view from ur template page?

- Vengat 3 years, 10 months ago

from the navbar.html which is linked to the base
<li class="nav-item ml-5"> <a class="nav-link waves-effect" href="{% url 'score:post-create' %}" >Upload Designs</a> </li>

- ahmed.hisham87 3 years, 10 months ago

ok we are trying to replicate the issue .. we will let you know

- Vengat 3 years, 10 months ago

are you getting score/post_form.html doesnt exists error?

- Vengat 3 years, 10 months ago

No, I didn't get this error:

Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/score/new/ Raised by: score.views.PostDetailView

- ahmed.hisham87 3 years, 10 months ago

check this document Createview. By default, Django create view looks the template in app folder.. So you should create the post_form.html under this directory. score/templates/score/post_form.html

- Vengat 3 years, 10 months ago

how was it working before then ?

- ahmed.hisham87 3 years, 10 months ago

ok then show me , revert everything and try again?

- Vengat 3 years, 10 months ago

did you check the document ?

- Vengat 3 years, 10 months ago

we are not sure how its worked before which is not something defintely wont work

- Vengat 3 years, 10 months ago

if you are still having doubt we can setup a meeting sometime later today

- Vengat 3 years, 10 months ago

last try this, def form_valid(self, form): form.instance.designer = self.request.user return HttpResponseRedirect(self.get_success_url()) try this in your createview

- Vengat 3 years, 10 months ago

still the same error

- ahmed.hisham87 3 years, 10 months ago

is it creating the post or not?. I mean save the data into database or not?

- Vengat 3 years, 10 months ago

or try success_url='/score/'

- Vengat 3 years, 10 months ago

as we are not getting update on this. Hopefully it resolved the issue. So we are closing the ticket from our end.

- Vengat 3 years, 10 months ago


Latest Blogs