Django how get Post details using slug

44 views
Skip to first unread message

Tanni Seriki

unread,
Jul 3, 2020, 3:10:22 PM7/3/20
to Django users
Please can someone put me through on this, post details view either one the function based view or class based view. Thanks
IMG_20200702_182540.jpg
IMG_20200702_182757.jpg
IMG_20200702_182709.jpg

coolguy

unread,
Jul 3, 2020, 8:04:43 PM7/3/20
to Django users
Seems pretty simple unless you have specific question about something.

You have a model which has some fields including Slug field which should be unique i.e. unique=True but your screen shot is not clear..

In views you are overriding the get_queryset() method of base class and assigning it to query variable for the 'q' from http GET request. Then filtering Post data with the values in query object you have just created. Finally returned the revised object_list including this new query context.

In post_detail_views you are retrieving data from Post based on the post slug and assigning it to variable 'f'. Finally you used the render() shortcut to render the retrieved post using a template.

In your urlpatterns you have defined the path for your post_detail which send the post as a slug to your view and in turn view returns the HTTP response.

Tanni Seriki

unread,
Jul 3, 2020, 8:08:26 PM7/3/20
to django...@googlegroups.com
Coolguy
Please can we chat on WhatsApp...
The post details is really giving me headache, please help out 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/d2457503-b332-4f26-8f8a-cda32934419fo%40googlegroups.com.

Tanni Seriki

unread,
Jul 3, 2020, 8:09:01 PM7/3/20
to django...@googlegroups.com
Here is my WhatsApp number
Please I really need to solve this out

coolguy

unread,
Jul 3, 2020, 8:22:04 PM7/3/20
to Django users
I am away at this time but can you send the project urls.py screen shot as well.
To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.

Tanni Seriki

unread,
Jul 3, 2020, 8:50:12 PM7/3/20
to django...@googlegroups.com
Sir here it's all the project, currently my phone camera has fault please help me out.

To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/bd9d5723-7eed-4334-9d6c-de5a076883ebo%40googlegroups.com.
post_details.html
views.py
models.py
urls.py

Tanni Seriki

unread,
Jul 3, 2020, 9:10:14 PM7/3/20
to django...@googlegroups.com
Please let me know if you have seen the file.
Am sorry am sending it to you as file, my phone is malfunctioning

coolguy

unread,
Jul 3, 2020, 10:05:38 PM7/3/20
to Django users
if the new urls is the main url then its not correctly setup. you have to mention the app name

Let assume you are app name 'student' in your project. So the main urls.py will have it as follow:

path('students/',  include(students.urls),

This way you are telling Django to handover all the requests sent in with students/ to students urls.py in students directory. There is where you will handle this and other students related urls.

In urls.py of students app you should have as follow:
path('<slug:post>/', views.post_details_view, name='post_details'),
Message has been deleted

Tanni Seriki

unread,
Jul 4, 2020, 3:39:17 AM7/4/20
to django...@googlegroups.com
Thanks sir, I will make changes.
I really appreciate your time and effort in helping me out with this
Thanks

On Fri, Jul 3, 2020, 11:14 PM coolguy <cooldjango...@gmail.com> wrote:
One more thing. if your view code is indented exactly the way it is in the provided then you need to fix the indentation of 

def post_details_view(request, post):

This should be under class SearchResultsViews()

like this...

class SearchResultsViews(ListView):
    model = Post
    template_name = 'Search.html'

    def get_queryset(self):
        query = self.request.GET.get('q')
        object_list = Post.objects.filter(Q(title__icontains=query))
        return object_list


    def post_details_view(request, post):
        f = get_object_or_404(Post, slug=post)
        return render(request, 'post_details.html', {'f': f})

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

arman hossain

unread,
Jul 4, 2020, 10:25:12 AM7/4/20
to django...@googlegroups.com
slag.png
Reply all
Reply to author
Forward
0 new messages