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
still it is showing Error Raised by: score.views.PostDetailView
- ahmed.hisham87 4 years, 11 months agothen try thissuccess_url = reverse_lazy('score:score)
import this as well from django.urls import reverse_lazy
it should work
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)
this is the format success_url = reverse_lazy('appname:redirectpage')
Still Page not found (404), the issue is with reaching the page itself not after posting
- ahmed.hisham87 4 years, 11 months agoscore.html, but the issue is can't reach this page: path('new/', PostCreateView.as_view(), name='post-create'),
- ahmed.hisham87 4 years, 11 months agoExactly the create page is showing Page 404 error I can't log in to it
- ahmed.hisham87 4 years, 11 months agoshow me the full path, i have a feeling you have an issue in template files
- VengatC:\Users\Ahmed\Desktop\Mostiques 4.3\templates\post_form.html
- ahmed.hisham87 4 years, 11 months agothere are no folders inside templates all html, it was working yesterday until we adjusted the slugs
- ahmed.hisham87 4 years, 11 months agoslug & 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
- VengatI have reviewed every step but the Error Raised by: score.views.PostDetailView
- ahmed.hisham87 4 years, 11 months agofrom 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>
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 4 years, 11 months agocheck 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
- Vengatwe are not sure how its worked before which is not something defintely wont work
- Vengatif you are still having doubt we can setup a meeting sometime later today
- Vengatlast try this, def form_valid(self, form):
form.instance.designer = self.request.user
return HttpResponseRedirect(self.get_success_url())
try this in your createview
is it creating the post or not?. I mean save the data into database or not?
- Vengatas we are not getting update on this. Hopefully it resolved the issue. So we are closing the ticket from our end.
- Vengat
what is the issue?.. in which view you are getting an error?